1
0
Fork 0
forked from forgejo/forgejo

unified API error response

This commit is contained in:
Unknwon 2015-10-08 20:36:07 -04:00
parent b1941f1da1
commit aff49b1c9e
13 changed files with 61 additions and 67 deletions

View file

@ -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
}

View file

@ -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,
})
}

View file

@ -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
}