This is an automated email from the ASF dual-hosted git repository. lynwee pushed a commit to branch dev-fix-tapd in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git
commit e3c2962162b93c5280cc9304684f9aef177487da Author: d4x1 <[email protected]> AuthorDate: Wed Apr 17 13:32:48 2024 +0800 feat(backend): add code field in resp body --- 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 {
