You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devlake.apache.org by ly...@apache.org on 2024/09/18 04:57:09 UTC
(incubator-devlake) 01/01: fix(utils): add new function ApiOutputAdvancedErrorWithCustomCode
This is an automated email from the ASF dual-hosted git repository.
lynwee pushed a commit to branch dev-10
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git
commit efe25e4fbb9c6c130a532e3beb08d57a6e6a09ba
Author: d4x1 <15...@qq.com>
AuthorDate: Wed Sep 18 12:56:56 2024 +0800
fix(utils): add new function ApiOutputAdvancedErrorWithCustomCode
---
backend/server/api/shared/api_output.go | 22 ++++++++++++++++++++++
1 file changed, 22 insertions(+)
diff --git a/backend/server/api/shared/api_output.go b/backend/server/api/shared/api_output.go
index debbe0a0e..b213f91e9 100644
--- a/backend/server/api/shared/api_output.go
+++ b/backend/server/api/shared/api_output.go
@@ -67,6 +67,28 @@ func ApiOutputErrorWithCustomCode(c *gin.Context, code int, err error) {
c.Writer.Header().Set("Content-Type", "application/json")
}
+// ApiOutputAdvancedErrorWithCustomCode writes a JSON error message to the HTTP response body
+func ApiOutputAdvancedErrorWithCustomCode(c *gin.Context, httpStatusCode, customBusinessCode int, err error) {
+ if e, ok := err.(errors.Error); ok {
+ logruslog.Global.Error(err, "HTTP %d error", e.GetType().GetHttpCode())
+ messages := e.Messages()
+ c.JSON(e.GetType().GetHttpCode(), &ApiBody{
+ Success: false,
+ Message: e.Error(),
+ Code: customBusinessCode,
+ Causes: messages.Causes(),
+ })
+ } else {
+ logruslog.Global.Error(err, "HTTP %d error (native)", http.StatusInternalServerError)
+ c.JSON(httpStatusCode, &ApiBody{
+ Success: false,
+ Code: customBusinessCode,
+ Message: err.Error(),
+ })
+ }
+ c.Writer.Header().Set("Content-Type", "application/json")
+}
+
// ApiOutputError writes a JSON error message to the HTTP response body
func ApiOutputError(c *gin.Context, err error) {
if e, ok := err.(errors.Error); ok {