From 6622cea91d000f949d0cfbecc597fa9b50f4f5cc Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sun, 24 May 2020 16:17:51 +0200 Subject: [PATCH 1/3] add API specific InternalServerError() --- modules/context/api.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/modules/context/api.go b/modules/context/api.go index 4e189ce07767b..8005036c8c451 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,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 From 94d3874e70356c41a47eb4c685e18a4e60a7f90b Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Tue, 26 May 2020 11:45:40 +0200 Subject: [PATCH 2/3] return 500 error msg only if not Production mode --- modules/context/api.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/modules/context/api.go b/modules/context/api.go index 8005036c8c451..b8d4250fa9567 100644 --- a/modules/context/api.go +++ b/modules/context/api.go @@ -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 { @@ -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{ @@ -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, }) } From 53dd28b2af2def8d7f41c8c6b7884c754deb2fc8 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 28 May 2020 19:29:21 +0200 Subject: [PATCH 3/3] rm unnessesary change --- modules/context/api.go | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/context/api.go b/modules/context/api.go index b8d4250fa9567..5d91ac49a3609 100644 --- a/modules/context/api.go +++ b/modules/context/api.go @@ -69,7 +69,6 @@ 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 {