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 1/5] 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 4e189ce07767b..fdb527ca66498 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 From 9f59afe0768f42b3044651db4608f7bc701b627b Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sun, 24 May 2020 16:25:09 +0200 Subject: [PATCH 2/5] why not ... Co-authored-by: zeripath --- modules/context/api.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/context/api.go b/modules/context/api.go index fdb527ca66498..8005036c8c451 100644 --- a/modules/context/api.go +++ b/modules/context/api.go @@ -72,7 +72,7 @@ func (ctx *APIContext) Error(status int, title string, obj interface{}) { if err, ok := obj.(error); ok { message = err.Error() } else { - message = obj.(string) + message = fmt.Sprintf("%s", obj) } if status == http.StatusInternalServerError { From 8467b2cee674ad205b452780ca88abb1b27643c8 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Tue, 26 May 2020 11:45:40 +0200 Subject: [PATCH 3/5] 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 6f3206759e77738962c5f97b4629ca0faa6ce7ee Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Wed, 27 May 2020 11:49:42 +0200 Subject: [PATCH 4/5] Revert "return 500 error msg only if not Production mode" This reverts commit 8467b2cee674ad205b452780ca88abb1b27643c8. --- modules/context/api.go | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/modules/context/api.go b/modules/context/api.go index b8d4250fa9567..8005036c8c451 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 { @@ -78,10 +77,6 @@ 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{ @@ -95,13 +90,8 @@ 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: message, + Message: err.Error(), URL: setting.API.SwaggerURL, }) } From 497f22689e1244dd6e2d294ace0ad08a09fa4e12 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Wed, 27 May 2020 11:59:51 +0200 Subject: [PATCH 5/5] InternalServerError --- modules/context/api.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/modules/context/api.go b/modules/context/api.go index 8005036c8c451..963c5c14f30c3 100644 --- a/modules/context/api.go +++ b/modules/context/api.go @@ -90,8 +90,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, }) }