diff options
Diffstat (limited to 'model/utils.go')
-rw-r--r-- | model/utils.go | 40 |
1 files changed, 32 insertions, 8 deletions
diff --git a/model/utils.go b/model/utils.go index 617c95efd..a18ca760e 100644 --- a/model/utils.go +++ b/model/utils.go @@ -9,13 +9,15 @@ import ( "encoding/base32" "encoding/json" "fmt" - "github.com/pborman/uuid" "io" "net/mail" "net/url" "regexp" "strings" "time" + + goi18n "github.com/nicksnyder/go-i18n/i18n" + "github.com/pborman/uuid" ) type StringInterface map[string]interface{} @@ -23,20 +25,31 @@ type StringMap map[string]string type StringArray []string type EncryptStringMap map[string]string -// AppError is returned for any http response that's not in the 200 range. type AppError struct { - Message string `json:"message"` // Message to be display to the end user without debugging information - DetailedError string `json:"detailed_error"` // Internal error string to help the developer - RequestId string `json:"request_id"` // The RequestId that's also set in the header - StatusCode int `json:"status_code"` // The http status code - Where string `json:"-"` // The function where it happened in the form of Struct.Func - IsOAuth bool `json:"is_oauth"` // Whether the error is OAuth specific + Id string `json:"id"` + Message string `json:"message"` // Message to be display to the end user without debugging information + DetailedError string `json:"detailed_error"` // Internal error string to help the developer + RequestId string `json:"request_id"` // The RequestId that's also set in the header + StatusCode int `json:"status_code"` // The http status code + Where string `json:"-"` // The function where it happened in the form of Struct.Func + IsOAuth bool `json:"is_oauth"` // Whether the error is OAuth specific + params map[string]interface{} `json:"-"` } func (er *AppError) Error() string { return er.Where + ": " + er.Message + ", " + er.DetailedError } +func (er *AppError) Translate(T goi18n.TranslateFunc) { + if len(er.Message) == 0 { + if er.params == nil { + er.Message = T(er.Id) + } else { + er.Message = T(er.Id, er.params) + } + } +} + func (er *AppError) ToJson() string { b, err := json.Marshal(er) if err != nil { @@ -68,6 +81,17 @@ func NewAppError(where string, message string, details string) *AppError { return ap } +func NewLocAppError(where string, id string, params map[string]interface{}, details string) *AppError { + ap := &AppError{} + ap.Id = id + ap.params = params + ap.Where = where + ap.DetailedError = details + ap.StatusCode = 500 + ap.IsOAuth = false + return ap +} + var encoding = base32.NewEncoding("ybndrfg8ejkmcpqxot1uwisza345h769") // NewId is a globally unique identifier. It is a [A-Z0-9] string 26 |