-
Notifications
You must be signed in to change notification settings - Fork 1
/
errors.go
122 lines (108 loc) · 2.3 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
113
114
115
116
117
118
119
120
121
122
package common
import (
"errors"
"strconv"
"github.com/gofiber/fiber/v2"
"gorm.io/gorm"
)
type HttpError struct {
Code int `json:"code,omitempty"`
Message string `json:"message,omitempty"`
Detail *ErrorDetail `json:"detail,omitempty"`
}
func (e *HttpError) Error() string {
return e.Message
}
func BadRequest(messages ...string) *HttpError {
message := "Bad Request"
if len(messages) > 0 {
message = messages[0]
}
return &HttpError{
Code: 400,
Message: message,
}
}
func Unauthorized(messages ...string) *HttpError {
message := "Invalid JWT Token"
if len(messages) > 0 {
message = messages[0]
}
return &HttpError{
Code: 401,
Message: message,
}
}
func Forbidden(messages ...string) *HttpError {
message := "Forbidden"
if len(messages) > 0 {
message = messages[0]
}
return &HttpError{
Code: 403,
Message: message,
}
}
func NotFound(messages ...string) *HttpError {
message := "Not Found"
if len(messages) > 0 {
message = messages[0]
}
return &HttpError{
Code: 404,
Message: message,
}
}
func InternalServerError(messages ...string) *HttpError {
message := "Internal Server Error"
if len(messages) > 0 {
message = messages[0]
}
return &HttpError{
Code: 500,
Message: message,
}
}
func ErrorHandler(ctx *fiber.Ctx, err error) error {
if err == nil {
return nil
}
httpError := HttpError{
Code: 500,
Message: err.Error(),
}
if errors.Is(err, gorm.ErrRecordNotFound) {
httpError.Code = 404
} else {
switch e := err.(type) {
case *HttpError:
httpError = *e
case *fiber.Error:
httpError.Code = e.Code
case *ErrorDetail:
httpError.Code = 400
httpError.Detail = e
case fiber.MultiError:
httpError.Code = 400
httpError.Message = ""
for _, err = range e {
httpError.Message += err.Error() + "\n"
}
}
}
// parse status code
// when status code is 400xxx to 599xxx, use leading 3 numbers instead
// else use 500
statusCode := httpError.Code
statusCodeString := strconv.Itoa(statusCode)
if len(statusCodeString) > 3 {
statusCodeString = statusCodeString[:3]
newStatusCode, err := strconv.Atoi(statusCodeString)
if err == nil && newStatusCode >= 400 && newStatusCode < 600 {
statusCode = newStatusCode
} else {
statusCode = 500
}
}
return ctx.Status(statusCode).JSON(&httpError)
}