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 783d4af06 Dev fix tapd (#7339) (#7340)
783d4af06 is described below

commit 783d4af06f3a9d1c5562ee22fcf8f2acff66472e
Author: github-actions[bot] 
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Wed Apr 17 13:53:32 2024 +0800

    Dev fix tapd (#7339) (#7340)
    
    * fix(tapd): change iteration_id's type from uint64 to int64
    
    * fix(tapd): fix test
    
    * feat(backend): add code field in resp body
    
    Co-authored-by: Lynwee <[email protected]>
---
 backend/server/api/shared/api_output.go | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/backend/server/api/shared/api_output.go 
b/backend/server/api/shared/api_output.go
index fb8cace6f..debbe0a0e 100644
--- a/backend/server/api/shared/api_output.go
+++ b/backend/server/api/shared/api_output.go
@@ -31,6 +31,7 @@ import (
 const BadRequestBody = "bad request body format"
 
 type TypedApiBody[T any] struct {
+       Code    int      `json:"code"`
        Success bool     `json:"success"`
        Message string   `json:"message"`
        Causes  []string `json:"causes"`
@@ -44,6 +45,28 @@ type ResponsePipelines struct {
        Pipelines []*models.Pipeline `json:"pipelines"`
 }
 
+// ApiOutputErrorWithCustomCode writes a JSON error message to the HTTP 
response body
+func ApiOutputErrorWithCustomCode(c *gin.Context, code 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:    code,
+                       Causes:  messages.Causes(),
+               })
+       } else {
+               logruslog.Global.Error(err, "HTTP %d error (native)", 
http.StatusInternalServerError)
+               c.JSON(http.StatusInternalServerError, &ApiBody{
+                       Success: false,
+                       Code:    code,
+                       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