-
Notifications
You must be signed in to change notification settings - Fork 0
/
error-handler.go
55 lines (43 loc) · 1.15 KB
/
error-handler.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
package helper
import "net/http"
type Error interface {
Message() string
Status() int
Type() string
}
type ErrorResponse struct {
ErrorMessage string `json:"message"`
ErrorStatus int `json:"status"`
ErrorType string `json:"error"`
}
func (e *ErrorResponse) Message() string {
return e.ErrorMessage
}
func (e *ErrorResponse) Status() int {
return e.ErrorStatus
}
func (e *ErrorResponse) Type() string {
return e.ErrorType
}
func NewError(message string, status int, ErrorType string) Error {
return &ErrorResponse{
ErrorMessage: message,
ErrorStatus: status,
ErrorType: ErrorType,
}
}
func BadRequest(message string) Error {
return NewError(message, http.StatusBadRequest, "Bad Request")
}
func Unautorized(message string) Error {
return NewError(message, http.StatusUnauthorized, "Unauthorized")
}
func NotFound(message string) Error {
return NewError(message, http.StatusNotFound, "Not Found")
}
func UnprocessibleEntity(message string) Error {
return NewError(message, http.StatusUnprocessableEntity, "Invalid Request")
}
func InternalServerError(message string) Error {
return NewError(message, http.StatusInternalServerError, "Server Error")
}