This is an automated email from the ASF dual-hosted git repository.

abeizn pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git


The following commit(s) were added to refs/heads/main by this push:
     new 65e6256c6 fix(utils): add new function 
ApiOutputAdvancedErrorWithCustomCode (#8063)
65e6256c6 is described below

commit 65e6256c6ca3c17bb5b32aa7c4d2280e3c1f36e8
Author: Lynwee <[email protected]>
AuthorDate: Wed Sep 18 13:40:14 2024 +0800

    fix(utils): add new function ApiOutputAdvancedErrorWithCustomCode (#8063)
---
 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 {

Reply via email to