-
Notifications
You must be signed in to change notification settings - Fork 8
/
errors.go
92 lines (75 loc) · 2.42 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
package yarf
import (
"net/http"
)
// YError is the interface used to handle error responses inside the framework.
type YError interface {
Code() int // HTTP response code for this error
ID() int // Error code ID.
Msg() string // Error description
Body() string // Error body content to be returned to the client if needed.
}
// CustomError is the standard error response format used through the framework.
// Implements Error and YError interfaces
type CustomError struct {
HTTPCode int // HTTP status code to be used as this error response.
ErrorCode int // Internal YARF error code for further reference.
ErrorMsg string // YARF error message.
ErrorBody string // Error content to be rendered to the client response.
}
// Implements the error interface returning the ErrorMsg value of each error.
func (e *CustomError) Error() string {
return e.ErrorMsg
}
// Code returns the error's HTTP code to be used in the response.
func (e *CustomError) Code() int {
return e.HTTPCode
}
// ID returns the error's ID for further reference.
func (e *CustomError) ID() int {
return e.ErrorCode
}
// Msg returns the error's message, used to implement the Error interface.
func (e *CustomError) Msg() string {
return e.ErrorMsg
}
// Body returns the error's content body, if needed, to be returned in the HTTP response.
func (e *CustomError) Body() string {
return e.ErrorBody
}
// UnexpectedError is used when the origin of the error can't be discovered
type UnexpectedError struct {
CustomError
}
// ErrorUnexpected creates UnexpectedError
func ErrorUnexpected() *UnexpectedError {
e := new(UnexpectedError)
e.HTTPCode = http.StatusInternalServerError
e.ErrorCode = 0
e.ErrorMsg = "Unexpected error"
return e
}
// MethodNotImplementedError is used to communicate that a specific HTTP method isn't implemented by a resource.
type MethodNotImplementedError struct {
CustomError
}
// ErrorMethodNotImplemented creates MethodNotImplementedError
func ErrorMethodNotImplemented() *MethodNotImplementedError {
e := new(MethodNotImplementedError)
e.HTTPCode = http.StatusMethodNotAllowed
e.ErrorCode = 1
e.ErrorMsg = "Method not implemented"
return e
}
// NotFoundError is the HTTP 404 error equivalent.
type NotFoundError struct {
CustomError
}
// ErrorNotFound creates NotFoundError
func ErrorNotFound() *NotFoundError {
e := new(NotFoundError)
e.HTTPCode = http.StatusNotFound
e.ErrorCode = 2
e.ErrorMsg = "Not found"
return e
}