MCPcopy
hub / github.com/apache/devlake / ApiOutputErrorWithCustomCode

Function ApiOutputErrorWithCustomCode

backend/server/api/shared/api_output.go:48–67  ·  view source on GitHub ↗

ApiOutputErrorWithCustomCode writes a JSON error message to the HTTP response body

(c *gin.Context, code int, err error)

Source from the content-addressed store, hash-verified

46
47// ApiOutputErrorWithCustomCode writes a JSON error message to the HTTP response body
48func ApiOutputErrorWithCustomCode(c *gin.Context, code int, err error) {
49 if e, ok := err.(errors.Error); ok {
50 logruslog.Global.Error(err, "HTTP %d error", e.GetType().GetHttpCode())
51 messages := e.Messages()
52 c.JSON(e.GetType().GetHttpCode(), &ApiBody{
53 Success: false,
54 Message: e.Error(),
55 Code: code,
56 Causes: messages.Causes(),
57 })
58 } else {
59 logruslog.Global.Error(err, "HTTP %d error (native)", http.StatusInternalServerError)
60 c.JSON(http.StatusInternalServerError, &ApiBody{
61 Success: false,
62 Code: code,
63 Message: err.Error(),
64 })
65 }
66 c.Writer.Header().Set("Content-Type", "application/json")
67}
68
69// ApiOutputAdvancedErrorWithCustomCode writes a JSON error message to the HTTP response body
70func ApiOutputAdvancedErrorWithCustomCode(c *gin.Context, httpStatusCode, customBusinessCode int, err error) {

Callers

nothing calls this directly

Calls 5

GetHttpCodeMethod · 0.80
CausesMethod · 0.80
ErrorMethod · 0.65
GetTypeMethod · 0.65
MessagesMethod · 0.65

Tested by

no test coverage detected