Repository navigation
Expand file tree
/
Copy pathresponse_error.go
More file actions
160 lines (141 loc) · 4.51 KB
/
Copy pathresponse_error.go
File metadata and controls
160 lines (141 loc) · 4.51 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package api
import (
"net/http"
"github.com/jgolang/api/core"
"github.com/jgolang/errors"
)
var (
// DefaultErrorTitle default error title.
DefaultErrorTitle = "Error response!"
// DefaultErrorMessage default error message.
DefaultErrorMessage = "The service has not completed the operation!"
// ErrorType error response type the value is "error".
ErrorType core.ResponseType = "error"
// InternalServerTitle internal server error title.
InternalServerTitle = "Sorry!"
// InternalServerMessage internal server error message.
InternalServerMessage = "An error has occurred please try again later."
// UnauthorizedTitle unauthorized error title.
UnauthorizedTitle = "Unauthorized!"
// UnauthorizedMessage unauthorized error message.
UnauthorizedMessage = "User not authorized."
)
// Error error response type the value is "error".
type Error core.ResponseData
// Write response error in screen.
func (err Error) Write(w http.ResponseWriter, r *http.Request) {
err.EventID = getEventID(r)
err.ResponseType = ErrorType
if err.Title == "" {
err.Title = DefaultErrorTitle
}
if err.Message == "" {
err.Message = DefaultErrorMessage
}
if err.HTTPStatusCode == 0 {
err.HTTPStatusCode = http.StatusBadRequest
}
if err.ResponseCode == "" {
err.ResponseCode = ResponseCodes.DefaultError
}
instanceFrom(r).core.Write(core.ResponseData(err), w)
}
// getErrCode returns the code and public message of the first coded error in
// errs: for each error, the first jgolang/errors error in its chain (also
// behind wrappers such as fmt.Errorf) whose code is not nil. A nil element in
// errs ends the search. Without a code it returns empty values, and the caller
// applies its defaults.
func getErrCode(errs ...error) (code core.ResponseCode, msg string) {
for _, e := range errs {
if e == nil {
return "", ""
}
if coder := errors.CodeOf(e); coder != nil {
return core.ResponseCode(coder.Str()), coder.Msg()
}
}
return "", ""
}
// Error400 returns a new HTTP Bad Request error code. Its response code and
// message come from the first coded error in errs (see getErrCode); without
// one they stay empty and Write applies the default code and message.
func Error400(errs ...error) Error {
code, msg := getErrCode(errs...)
return Error{
Message: msg,
ResponseCode: code,
}
}
// ErrorWithMsg return a new HTTP Bad Request with custom message. Its response
// code comes from the first coded error in errs (see getErrCode).
func ErrorWithMsg(msg string, errs ...error) Error {
code, _ := getErrCode(errs...)
return Error{
Message: msg,
ResponseCode: code,
}
}
// Error401 returns a new HTTP Unauthorized error cod.
// and unauthorized error defalut title and default message.
func Error401() Error {
return Error{
Title: UnauthorizedTitle,
Message: UnauthorizedMessage,
ResponseCode: ResponseCodes.Unauthorized,
HTTPStatusCode: http.StatusUnauthorized,
}
}
// Error403 returns a new HTTP Forbiden error code
// and unauthorized error defalut title and default message.
func Error403() Error {
return Error{
Title: UnauthorizedTitle,
Message: UnauthorizedMessage,
ResponseCode: ResponseCodes.RestrictResource,
HTTPStatusCode: http.StatusForbidden,
}
}
// Error500 returns a new HTTP Internal Server Error code.
// and internal server error default title and default mesage. The code and
// message come from the first coded error in errs (see getErrCode) when there
// is one.
func Error500(errs ...error) Error {
code, msg := getErrCode(errs...)
if code == "" {
code = internalServerErrorCode()
}
if msg == "" {
msg = InternalServerMessage
}
return Error{
Title: InternalServerTitle,
Message: msg,
ResponseCode: code,
HTTPStatusCode: http.StatusInternalServerError,
}
}
// Error501 returns a new HTTP 501 Not Implemented error with the internal
// server error default title and message. The code and message come from the
// first coded error in errs (see getErrCode) when there is one. Before v0.10.0
// it answered 503.
func Error501(errs ...error) Error {
code, msg := getErrCode(errs...)
if code == "" {
code = ResponseCodes.DefaultError
}
if msg == "" {
msg = InternalServerMessage
}
return Error{
Title: InternalServerTitle,
Message: msg,
ResponseCode: code,
HTTPStatusCode: http.StatusNotImplemented,
}
}
// Error500WithMsg returns a new HTTP internal server error code with custom message.
func Error500WithMsg(msg string, errs ...error) Error {
err := Error500(errs...)
err.Message = msg
return err
}