generated from dzungtran/echo-rest-api
/
errors.go
112 lines (91 loc) · 2.33 KB
/
errors.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package utils
import (
"net/http"
cueErrors "cuelang.org/go/cue/errors"
"github.com/go-playground/validator/v10"
"github.com/labstack/echo/v4"
"github.com/lib/pq"
)
type Error struct {
Errors map[string]interface{} `json:"errors"`
}
func (e Error) Error() string {
return e.Errors["message"].(string)
}
func (e Error) RawError() error {
return e.Errors["raw"].(error)
}
// NewError creates a new error response
func NewError(err error, msg string) Error {
e := Error{}
e.Errors = make(map[string]interface{})
e.Errors["raw"] = err
switch v := err.(type) {
case *echo.HTTPError:
e.Errors["message"] = v.Message
default:
if msg != "" {
e.Errors["message"] = msg
} else {
e.Errors["message"] = v.Error()
}
}
return e
}
type invalidField struct {
Field string `json:"field"`
Error string `json:"error"`
}
// NewValidationError creates a new error response representing a data validation error (HTTP 400)
func NewValidationError(errs validator.ValidationErrors) Error {
e := Error{}
e.Errors = make(map[string]interface{})
var details []invalidField
for _, field := range errs {
details = append(details, invalidField{
Field: field.Field(),
Error: field.Error(),
})
}
e.Errors["message"] = "there is some problem with the data you submitted"
e.Errors["details"] = details
return e
}
// NewAccessForbiddenError creates a new error response representing an authorization failure (HTTP 403)
func NewAccessForbiddenError() Error {
e := Error{}
e.Errors = make(map[string]interface{})
e.Errors["message"] = "access forbidden"
return e
}
// NewNotFoundError creates a new error response representing a resource-not-found error (HTTP 404)
func NewNotFoundError() error {
e := Error{}
e.Errors = make(map[string]interface{})
e.Errors["message"] = "resource not found"
return e
}
func IsCueError(err error) bool {
_, ok := err.(cueErrors.Error)
return ok
}
func IsDuplicatedError(err error) bool {
switch err.(type) {
case *pq.Error:
return err.(pq.Error).Code == "23505"
}
return false
}
func GetHttpStatusCodeByErrorType(err error, defaultCode int) int {
switch err.(type) {
case *pq.Error:
return http.StatusInternalServerError
case cueErrors.Error:
case validator.ValidationErrors:
return http.StatusBadRequest
}
if defaultCode > 0 {
return defaultCode
}
return http.StatusInternalServerError
}