-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
201 lines (157 loc) · 5.92 KB
/
error.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package mstdn
import (
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/reiver/go-opt"
)
func errorJSON(writer io.Writer, statusCode int, msg ...interface{}) {
if nil == writer {
return
}
// short error message.
var short string
{
var shortErrorMessage opt.Optional[string]
shortErrorMessage.WhenNothing(func(){
if 1 <= len(msg) {
shortErrorMessage = opt.Something(fmt.Sprint(msg[0]))
}
})
shortErrorMessage.WhenNothing(func(){
shortErrorMessage = opt.Something(http.StatusText(statusCode))
})
shortErrorMessage.WhenSomething(func(value string){
short = value
})
}
var long opt.Optional[string]
{
if 2 <= len(msg) {
long = opt.Something(fmt.Sprint(msg[1:]...))
}
}
var array [512]byte
var buffer []byte = array[:0]
{
buffer = append(buffer, "{"...)
buffer = append(buffer, `"error":`...)
{
// This should never return an error since we are passing a string.
marshaled, _ := json.Marshal(short)
buffer = append(buffer, marshaled...)
}
long.WhenSomething(func(value string){
buffer = append(buffer, `,"error_description":`...)
// This should never return an error since we are passing a string.
marshaled, _ := json.Marshal(value)
buffer = append(buffer, marshaled...)
})
buffer = append(buffer, "}"...)
}
writer.Write(buffer)
}
// Error responds to the request with the specified error message and HTTP code.
// The response will be in JSON (application/json; charset=utf-8).
//
// For example:
//
// var statusCode int = http.StatusUnauthorized
// var shortErrorMessage = http.StatusText(http.StatusUnauthorized)
// var longErrorMessage = "Access Denied!!! — you can keep on knocking but you can't come in"
//
// mstdn.Error(resp, statusCode, shortErrorMessage, longErrorMessage)
//
// Responds with:
//
// {
// "error":"Unauthorized",
// "error_description":"Access Defined!!!! — you can keep on knocking but you can't come in"
// }
func Error(resp http.ResponseWriter, statusCode int, msg ...interface{}) {
if nil == resp {
return
}
resp.WriteHeader(statusCode)
resp.Header().Add("Content-Type", "application/json; charset=utf-8")
errorJSON(resp, statusCode, msg...)
}
// BadRequest replies to the request with an HTTP 400 Bad Request.
// The response will be in JSON (application/json; charset=utf-8).
func BadRequest(resp http.ResponseWriter, msg ...interface{}) {
const statusCode int = http.StatusBadRequest
Error(resp, statusCode, msg...)
}
// Unauthorized replies to the request with an HTTP 401 Unauthorized.
// The response will be in JSON (application/json; charset=utf-8).
func Unauthorized(resp http.ResponseWriter, msg ...interface{}) {
const statusCode int = http.StatusUnauthorized
Error(resp, statusCode, msg...)
}
// PaymentRequired replies to the request with an HTTP 402 Payment Required
// The response will be in JSON (application/json; charset=utf-8).
func PaymentRequired(resp http.ResponseWriter, msg ...interface{}) {
const statusCode int = http.StatusPaymentRequired
Error(resp, statusCode, msg...)
}
// Forbidden replies to the request with an HTTP 403 Forbidden.
// The response will be in JSON (application/json; charset=utf-8).
func Forbidden(resp http.ResponseWriter, msg ...interface{}) {
const statusCode int = http.StatusForbidden
Error(resp, statusCode, msg...)
}
// NotFound replies to the request with an HTTP 404 Not Found.
// The response will be in JSON (application/json; charset=utf-8).
func NotFound(resp http.ResponseWriter, msg ...interface{}) {
const statusCode int = http.StatusNotFound
Error(resp, statusCode, msg...)
}
// MethodNotAllowed replies to the request with an HTTP 405 Method Not Allowed.
// The response will be in JSON (application/json; charset=utf-8).
func MethodNotAllowed(resp http.ResponseWriter, msg ...interface{}) {
const statusCode int = http.StatusMethodNotAllowed
Error(resp, statusCode, msg...)
}
// RequestTimeout replies to the request with an HTTP 408 Request Timeout.
// The response will be in JSON (application/json; charset=utf-8).
func RequestTimeout(resp http.ResponseWriter, msg ...interface{}) {
const statusCode int = http.StatusRequestTimeout
Error(resp, statusCode, msg...)
}
// UnprocessableEntity replies to the request with an HTTP 422 Unprocessable Entity.
// The response will be in JSON (application/json; charset=utf-8).
func UnprocessableEntity(resp http.ResponseWriter, msg ...interface{}) {
const statusCode int = http.StatusUnprocessableEntity
Error(resp, statusCode, msg...)
}
// TooManyRequests replies to the request with an HTTP 429 Too Many Requests.
// The response will be in JSON (application/json; charset=utf-8).
func TooManyRequests (resp http.ResponseWriter, msg ...interface{}) {
const statusCode int = http.StatusTooManyRequests
Error(resp, statusCode, msg...)
}
// InternalServerError replies to the request with an HTTP 500 Internal Server Error.
// The response will be in JSON (application/json; charset=utf-8).
func InternalServerError(resp http.ResponseWriter, msg ...interface{}) {
const statusCode int = http.StatusInternalServerError
Error(resp, statusCode, msg...)
}
// BadGateway replies to the request with an HTTP 502 Bad Gateway.
// The response will be in JSON (application/json; charset=utf-8).
func BadGateway(resp http.ResponseWriter, msg ...interface{}) {
const statusCode int = http.StatusBadGateway
Error(resp, statusCode, msg...)
}
// ServiceUnavailable replies to the request with an HTTP 503 Service Unavailable.
// The response will be in JSON (application/json; charset=utf-8).
func ServiceUnavailable(resp http.ResponseWriter, msg ...interface{}) {
const statusCode int = http.StatusServiceUnavailable
Error(resp, statusCode, msg...)
}
// Gateway Timeout replies to the request with an HTTP 504 Gateway Timeout.
// The response will be in JSON (application/json; charset=utf-8).
func GatewayTimeout(resp http.ResponseWriter, msg ...interface{}) {
const statusCode int = http.StatusGatewayTimeout
Error(resp, statusCode, msg...)
}