From 360083a4860b44134bc8125f08f8501d95f84606 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sun, 24 May 2020 16:17:51 +0200 Subject: [PATCH] add API specific InternalServerError() --- modules/context/api.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/modules/context/api.go b/modules/context/api.go index 4e189ce077..fdb527ca66 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,7 +65,7 @@ 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 @@ -74,8 +75,8 @@ func (ctx *APIContext) Error(status int, title string, obj interface{}) { message = obj.(string) } - 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,17 @@ 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) + + ctx.JSON(http.StatusInternalServerError, APIError{ + Message: err.Error(), + URL: setting.API.SwaggerURL, + }) +} + func genAPILinks(curURL *url.URL, total, pageSize, curPage int) []string { page := NewPagination(total, pageSize, curPage, 0) paginater := page.Paginater