forked from go-catupiry/catu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
responses.go
71 lines (56 loc) · 1.28 KB
/
responses.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package bolo
import (
"fmt"
"github.com/labstack/echo/v4"
)
type BaseListReponse struct {
Meta BaseMetaResponse `json:"meta"`
}
type BaseMetaResponse struct {
Count int64 `json:"count"`
}
type BaseErrorResponse struct {
Messages []BaseErrorResponseMessage `json:"messages"`
}
type BaseErrorResponseMessage struct {
Status string `json:"status"`
Code int `json:"code"`
Message string `json:"message"`
}
type EmptyResponse struct{}
func ParseHTTPErrorToResponse(ctx *RequestContext, he HTTPErrorInterface) *BaseErrorResponse {
respData := BaseErrorResponse{
Messages: []BaseErrorResponseMessage{
{
Status: ParseHTTPCodeToStatus(he.GetCode()),
Code: he.GetCode(),
Message: fmt.Sprintf("%v", he.GetMessage()),
},
},
}
return &respData
}
func ParseEchoHTTPErrorToResponse(ctx *RequestContext, he *echo.HTTPError) *BaseErrorResponse {
respData := BaseErrorResponse{
Messages: []BaseErrorResponseMessage{
{
Status: ParseHTTPCodeToStatus(he.Code),
Code: he.Code,
Message: he.Error(),
},
},
}
return &respData
}
func ParseHTTPCodeToStatus(status int) string {
if status < 300 {
return "success"
}
if status >= 300 && status < 400 {
return "success"
}
if status >= 400 && status < 500 {
return "warning"
}
return "danger"
}