diff options
Diffstat (limited to 'api/context.go')
-rw-r--r-- | api/context.go | 37 |
1 files changed, 20 insertions, 17 deletions
diff --git a/api/context.go b/api/context.go index b6ffb1a29..f47ed1c30 100644 --- a/api/context.go +++ b/api/context.go @@ -31,6 +31,7 @@ type Context struct { siteURL string SessionTokenIndex int64 T goi18n.TranslateFunc + Locale string } type Page struct { @@ -43,6 +44,7 @@ type Page struct { Channel *model.Channel PostID string SessionTokenIndex int64 + Locale string } func ApiAppHandler(h func(*Context, http.ResponseWriter, *http.Request)) http.Handler { @@ -86,7 +88,7 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { l4g.Debug("%v", r.URL.Path) c := &Context{} - c.T = utils.GetTranslations(w, r) + c.T, c.Locale = utils.GetTranslationsAndLocale(w, r) c.RequestId = model.NewId() c.IpAddress = GetIpAddress(r) @@ -166,10 +168,10 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if session == nil || session.IsExpired() { c.RemoveSessionCookie(w, r) - c.Err = model.NewAppError("ServeHTTP", "Invalid or expired session, please login again.", "token="+token) + c.Err = model.NewLocAppError("ServeHTTP", "api.context.session_expired.app_error", nil, "token="+token) c.Err.StatusCode = http.StatusUnauthorized } else if !session.IsOAuth && isTokenFromQueryString { - c.Err = model.NewAppError("ServeHTTP", "Session is not OAuth but token was provided in the query string", "token="+token) + c.Err = model.NewLocAppError("ServeHTTP", "api.context.token_provided.app_error", nil, "token="+token) c.Err.StatusCode = http.StatusUnauthorized } else { c.Session = *session @@ -196,7 +198,7 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if c.Err == nil && h.isUserActivity && token != "" && len(c.Session.UserId) > 0 { go func() { if err := (<-Srv.Store.User().UpdateUserAndSessionActivity(c.Session.UserId, c.Session.Id, model.GetMillis())).Err; err != nil { - l4g.Error("Failed to update LastActivityAt for user_id=%v and session_id=%v, err=%v", c.Session.UserId, c.Session.Id, err) + l4g.Error(utils.T("api.context.last_activity_at.error"), c.Session.UserId, c.Session.Id, err) } }() } @@ -206,6 +208,7 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } if c.Err != nil { + c.Err.Translate(c.T) c.Err.RequestId = c.RequestId c.LogError(c.Err) c.Err.Where = r.URL.Path @@ -251,13 +254,13 @@ func (c *Context) LogAuditWithUserId(userId, extraInfo string) { } func (c *Context) LogError(err *model.AppError) { - l4g.Error("%v:%v code=%v rid=%v uid=%v ip=%v %v [details: %v]", c.Path, err.Where, err.StatusCode, + l4g.Error(utils.T("api.context.log.error"), c.Path, err.Where, err.StatusCode, c.RequestId, c.Session.UserId, c.IpAddress, err.Message, err.DetailedError) } func (c *Context) UserRequired() { if len(c.Session.UserId) == 0 { - c.Err = model.NewAppError("", "Invalid or expired session, please login again.", "UserRequired") + c.Err = model.NewLocAppError("", "api.context.session_expired.app_error", nil, "UserRequired") c.Err.StatusCode = http.StatusUnauthorized return } @@ -265,11 +268,11 @@ func (c *Context) UserRequired() { func (c *Context) SystemAdminRequired() { if len(c.Session.UserId) == 0 { - c.Err = model.NewAppError("", "Invalid or expired session, please login again.", "SystemAdminRequired") + c.Err = model.NewLocAppError("", "api.context.session_expired.app_error", nil, "SystemAdminRequired") c.Err.StatusCode = http.StatusUnauthorized return } else if !c.IsSystemAdmin() { - c.Err = model.NewAppError("", "You do not have the appropriate permissions", "AdminRequired") + c.Err = model.NewLocAppError("", "api.context.permissions.app_error", nil, "AdminRequired") c.Err.StatusCode = http.StatusForbidden return } @@ -287,7 +290,7 @@ func (c *Context) HasPermissionsToUser(userId string, where string) bool { return true } - c.Err = model.NewAppError(where, "You do not have the appropriate permissions", "userId="+userId) + c.Err = model.NewLocAppError(where, "api.context.permissions.app_error", nil, "userId="+userId) c.Err.StatusCode = http.StatusForbidden return false } @@ -302,7 +305,7 @@ func (c *Context) HasPermissionsToTeam(teamId string, where string) bool { return true } - c.Err = model.NewAppError(where, "You do not have the appropriate permissions", "userId="+c.Session.UserId+", teamId="+teamId) + c.Err = model.NewLocAppError(where, "api.context.permissions.app_error", nil, "userId="+c.Session.UserId+", teamId="+teamId) c.Err.StatusCode = http.StatusForbidden return false } @@ -312,7 +315,7 @@ func (c *Context) HasPermissionsToChannel(sc store.StoreChannel, where string) b c.Err = cresult.Err return false } else if cresult.Data.(int64) != 1 { - c.Err = model.NewAppError(where, "You do not have the appropriate permissions", "userId="+c.Session.UserId) + c.Err = model.NewLocAppError(where, "api.context.permissions.app_error", nil, "userId="+c.Session.UserId) c.Err.StatusCode = http.StatusForbidden return false } @@ -325,7 +328,7 @@ func (c *Context) HasSystemAdminPermissions(where string) bool { return true } - c.Err = model.NewAppError(where, "You do not have the appropriate permissions (system)", "userId="+c.Session.UserId) + c.Err = model.NewLocAppError(where, "api.context.system_permissions.app_error", nil, "userId="+c.Session.UserId) c.Err.StatusCode = http.StatusForbidden return false } @@ -373,12 +376,12 @@ func (c *Context) RemoveSessionCookie(w http.ResponseWriter, r *http.Request) { } func (c *Context) SetInvalidParam(where string, name string) { - c.Err = model.NewAppError(where, "Invalid "+name+" parameter", "") + c.Err = model.NewLocAppError(where, "api.context.invalid_param.app_error", map[string]interface{}{"Name": name}, "") c.Err.StatusCode = http.StatusBadRequest } func (c *Context) SetUnknownError(where string, details string) { - c.Err = model.NewAppError(where, "An unknown error has occured. Please contact support.", details) + c.Err = model.NewLocAppError(where, "api.context.unknown.app_error", nil, details) } func (c *Context) setTeamURL(url string, valid bool) { @@ -404,7 +407,7 @@ func (c *Context) GetTeamURL() string { if !c.teamURLValid { c.SetTeamURLFromSession() if !c.teamURLValid { - l4g.Debug("TeamURL accessed when not valid. Team URL should not be used in api functions or those that are team independent") + l4g.Debug(utils.T("api.context.invalid_team_url.debug")) } } return c.teamURL @@ -509,7 +512,7 @@ func RenderWebError(err *model.AppError, w http.ResponseWriter, r *http.Request) } func Handle404(w http.ResponseWriter, r *http.Request) { - err := model.NewAppError("Handle404", "Sorry, we could not find the page.", "") + err := model.NewLocAppError("Handle404", "api.context.404.app_error", nil, "") err.StatusCode = http.StatusNotFound l4g.Error("%v: code=404 ip=%v", r.URL.Path, GetIpAddress(r)) RenderWebError(err, w, r) @@ -523,7 +526,7 @@ func GetSession(token string) *model.Session { if session == nil { if sessionResult := <-Srv.Store.Session().Get(token); sessionResult.Err != nil { - l4g.Error("Invalid session token=" + token + ", err=" + sessionResult.Err.DetailedError) + l4g.Error(utils.T("api.context.invalid_token.error"), token, sessionResult.Err.DetailedError) } else { session = sessionResult.Data.(*model.Session) |