diff options
author | Unknwon <u@gogs.io> | 2016-03-13 18:49:16 -0400 |
---|---|---|
committer | Unknwon <u@gogs.io> | 2016-03-13 18:49:16 -0400 |
commit | dd6faf7f9bd0a1dbf986e124ea0f4db249e1da48 (patch) | |
tree | 7f9e4107eb961712a56160ef544143eee3771653 /modules/context | |
parent | db4da7beecd6a8f65bfa264ba18a8cb12303921f (diff) | |
download | gitea-dd6faf7f9bd0a1dbf986e124ea0f4db249e1da48.tar.gz gitea-dd6faf7f9bd0a1dbf986e124ea0f4db249e1da48.zip |
Convert all API handers to use *context.APIContext
Diffstat (limited to 'modules/context')
-rw-r--r-- | modules/context/api.go | 23 | ||||
-rw-r--r-- | modules/context/auth.go | 4 | ||||
-rw-r--r-- | modules/context/context.go | 19 |
3 files changed, 26 insertions, 20 deletions
diff --git a/modules/context/api.go b/modules/context/api.go index 0c0cce8cab..44232d71a6 100644 --- a/modules/context/api.go +++ b/modules/context/api.go @@ -6,12 +6,35 @@ package context import ( "gopkg.in/macaron.v1" + + "github.com/gogits/gogs/modules/base" + "github.com/gogits/gogs/modules/log" ) type APIContext struct { *Context } +// Error responses error message to client with given message. +// If status is 500, also it prints error to log. +func (ctx *APIContext) Error(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, + }) +} + func APIContexter() macaron.Handler { return func(c *Context) { ctx := &APIContext{ diff --git a/modules/context/auth.go b/modules/context/auth.go index 41c8b9eb01..422869db2d 100644 --- a/modules/context/auth.go +++ b/modules/context/auth.go @@ -52,7 +52,9 @@ func Toggle(options *ToggleOptions) macaron.Handler { if !ctx.IsSigned { // Restrict API calls with error message. if auth.IsAPIPath(ctx.Req.URL.Path) { - ctx.APIError(403, "", "Only signed in user is allowed to call APIs.") + ctx.JSON(403, map[string]string{ + "message": "Only signed in user is allowed to call APIs.", + }) return } diff --git a/modules/context/context.go b/modules/context/context.go index faff52f2c8..348de32570 100644 --- a/modules/context/context.go +++ b/modules/context/context.go @@ -112,25 +112,6 @@ func (ctx *Context) HandleText(status int, title string) { ctx.PlainText(status, []byte(title)) } -// 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, - }) -} - func (ctx *Context) ServeContent(name string, r io.ReadSeeker, params ...interface{}) { modtime := time.Now() for _, p := range params { |