Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return json on 500 error from API #11574

Merged
merged 10 commits into from
May 28, 2020
Prev Previous commit
Next Next commit
return 500 error msg only if not Production mode
  • Loading branch information
6543 committed May 26, 2020
commit 8467b2cee674ad205b452780ca88abb1b27643c8
12 changes: 11 additions & 1 deletion modules/context/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ type APIRedirect struct{}
// 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 {
Expand All @@ -77,6 +78,10 @@ func (ctx *APIContext) Error(status int, title string, obj interface{}) {

if status == http.StatusInternalServerError {
log.ErrorWithSkip(1, "%s: %s", title, message)

if macaron.Env == macaron.PROD {
message = ""
}
}

ctx.JSON(status, APIError{
Expand All @@ -90,8 +95,13 @@ func (ctx *APIContext) Error(status int, title string, obj interface{}) {
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: err.Error(),
Message: message,
URL: setting.API.SwaggerURL,
})
}
Expand Down