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

lynwee pushed a commit to branch release-v1.0
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git


The following commit(s) were added to refs/heads/release-v1.0 by this push:
     new 17a535970 fix(utils): add new function 
ApiOutputAdvancedErrorWithCustomCode (#8063) (#8064)
17a535970 is described below

commit 17a5359708def78d3fd518b3fcb7bf7af0d73caf
Author: github-actions[bot] 
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Wed Sep 18 14:04:33 2024 +0800

    fix(utils): add new function ApiOutputAdvancedErrorWithCustomCode (#8063) 
(#8064)
    
    Co-authored-by: Lynwee <[email protected]>
---
 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