diff options
author | Unknwon <u@gogs.io> | 2015-10-08 20:36:07 -0400 |
---|---|---|
committer | Unknwon <u@gogs.io> | 2015-10-08 20:36:07 -0400 |
commit | aff49b1c9eaa33f7c530275f2695d6d96699ec5d (patch) | |
tree | c3720601db2e005e57032feb42deac7e347ac713 /modules/middleware | |
parent | b1941f1da18b8c6ca046771c21f21ba793765518 (diff) | |
download | gitea-aff49b1c9eaa33f7c530275f2695d6d96699ec5d.tar.gz gitea-aff49b1c9eaa33f7c530275f2695d6d96699ec5d.zip |
unified API error response
Diffstat (limited to 'modules/middleware')
-rw-r--r-- | modules/middleware/auth.go | 2 | ||||
-rw-r--r-- | modules/middleware/context.go | 9 | ||||
-rw-r--r-- | modules/middleware/repo.go | 9 |
3 files changed, 13 insertions, 7 deletions
diff --git a/modules/middleware/auth.go b/modules/middleware/auth.go index 4f92c8f8e3..be8db35733 100644 --- a/modules/middleware/auth.go +++ b/modules/middleware/auth.go @@ -95,7 +95,7 @@ func Toggle(options *ToggleOptions) macaron.Handler { if !ctx.IsSigned { // Restrict API calls with error message. if auth.IsAPIPath(ctx.Req.URL.Path) { - ctx.HandleAPI(403, "Only signed in user is allowed to call APIs.") + ctx.APIError(403, "", "Only signed in user is allowed to call APIs.") return } diff --git a/modules/middleware/context.go b/modules/middleware/context.go index 141e8ace40..c08f84925e 100644 --- a/modules/middleware/context.go +++ b/modules/middleware/context.go @@ -157,15 +157,22 @@ func (ctx *Context) HandleText(status int, title string) { ctx.RenderData(status, []byte(title)) } -func (ctx *Context) HandleAPI(status int, obj interface{}) { +// APIError logs error with title if status is 500. +func (ctx *Context) APIError(status int, title string, obj interface{}) { var message string if err, ok := obj.(error); ok { message = err.Error() } else { message = obj.(string) } + + if status == 500 { + log.Error(4, "%s: %s", title, message) + } + ctx.JSON(status, map[string]string{ "message": message, + "url": base.DOC_URL, }) } diff --git a/modules/middleware/repo.go b/modules/middleware/repo.go index 1d1723445d..0b519a6bdb 100644 --- a/modules/middleware/repo.go +++ b/modules/middleware/repo.go @@ -14,7 +14,6 @@ import ( "github.com/mssola/user_agent" "github.com/gogits/gogs/models" - "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/git" "github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/setting" @@ -44,7 +43,7 @@ func ApiRepoAssignment() macaron.Handler { if models.IsErrUserNotExist(err) { ctx.Error(404) } else { - ctx.JSON(500, &base.ApiJsonErr{"GetUserByName: " + err.Error(), base.DOC_URL}) + ctx.APIError(500, "GetUserByName", err) } return } @@ -57,17 +56,17 @@ func ApiRepoAssignment() macaron.Handler { if models.IsErrRepoNotExist(err) { ctx.Error(404) } else { - ctx.JSON(500, &base.ApiJsonErr{"GetRepositoryByName: " + err.Error(), base.DOC_URL}) + ctx.APIError(500, "GetRepositoryByName", err) } return } else if err = repo.GetOwner(); err != nil { - ctx.JSON(500, &base.ApiJsonErr{"GetOwner: " + err.Error(), base.DOC_URL}) + ctx.APIError(500, "GetOwner", err) return } mode, err := models.AccessLevel(ctx.User, repo) if err != nil { - ctx.JSON(500, &base.ApiJsonErr{"AccessLevel: " + err.Error(), base.DOC_URL}) + ctx.APIError(500, "AccessLevel", err) return } |