Skip to content

Commit 04aef2e

Browse files
authored
Dev fix tapd (#7339)
* fix(tapd): change iteration_id's type from uint64 to int64 * fix(tapd): fix test * feat(backend): add code field in resp body
1 parent 10f8358 commit 04aef2e

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

backend/server/api/shared/api_output.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
const BadRequestBody = "bad request body format"
3232

3333
type TypedApiBody[T any] struct {
34+
Code int `json:"code"`
3435
Success bool `json:"success"`
3536
Message string `json:"message"`
3637
Causes []string `json:"causes"`
@@ -44,6 +45,28 @@ type ResponsePipelines struct {
4445
Pipelines []*models.Pipeline `json:"pipelines"`
4546
}
4647

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

0 commit comments

Comments
 (0)