diff options
author | 6543 <6543@obermui.de> | 2020-05-28 18:58:11 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-28 13:58:11 -0300 |
commit | 9f5576980411385a21668ce5930e53b89be67a5e (patch) | |
tree | a95a5ba87b4ca0d63f598a0c0c5c614a63a66906 /modules/context/api.go | |
parent | 3aedc795c4be04254e3400ea1ee85e1fd48ed55a (diff) | |
download | gitea-9f5576980411385a21668ce5930e53b89be67a5e.tar.gz gitea-9f5576980411385a21668ce5930e53b89be67a5e.zip |
Return json on 500 error from API (#11574)
* add API specific InternalServerError()
Co-authored-by: zeripath <art27@cantab.net>
* return 500 error msg only if not Production mode
* Revert "return 500 error msg only if not Production mode"
This reverts commit 8467b2cee674ad205b452780ca88abb1b27643c8.
* InternalServerError
Co-authored-by: zeripath <art27@cantab.net>
Diffstat (limited to 'modules/context/api.go')
-rw-r--r-- | modules/context/api.go | 25 |
1 files changed, 21 insertions, 4 deletions
diff --git a/modules/context/api.go b/modules/context/api.go index 4e189ce077..963c5c14f3 100644 --- a/modules/context/api.go +++ b/modules/context/api.go @@ -7,6 +7,7 @@ package context import ( "fmt" + "net/http" "net/url" "strings" @@ -64,18 +65,18 @@ type APINotFound struct{} // swagger:response redirect type APIRedirect struct{} -// Error responses error message to client with given message. +// Error responds with an error message to client with given obj as the 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) + message = fmt.Sprintf("%s", obj) } - if status == 500 { - log.Error("%s: %s", title, message) + if status == http.StatusInternalServerError { + log.ErrorWithSkip(1, "%s: %s", title, message) } ctx.JSON(status, APIError{ @@ -84,6 +85,22 @@ func (ctx *APIContext) Error(status int, title string, obj interface{}) { }) } +// InternalServerError responds with an error message to the client with the error as a message +// and the file and line of the caller. +func (ctx *APIContext) InternalServerError(err error) { + log.ErrorWithSkip(1, "InternalServerError: %v", err) + + var message string + if macaron.Env != macaron.PROD { + message = err.Error() + } + + ctx.JSON(http.StatusInternalServerError, APIError{ + Message: message, + URL: setting.API.SwaggerURL, + }) +} + func genAPILinks(curURL *url.URL, total, pageSize, curPage int) []string { page := NewPagination(total, pageSize, curPage, 0) paginater := page.Paginater |