Skip to content

Commit

Permalink
fix(utils): add new function ApiOutputAdvancedErrorWithCustomCode (#8063
Browse files Browse the repository at this point in the history
)
  • Loading branch information
d4x1 authored Sep 18, 2024
1 parent f0ad6f5 commit 65e6256
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions backend/server/api/shared/api_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 65e6256

Please sign in to comment.