-
Notifications
You must be signed in to change notification settings - Fork 1
/
errors.go
51 lines (42 loc) · 1.34 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
package jopher
import "net/http"
// Error sends a json error message
func Error(w http.ResponseWriter, status int, err error) {
m := M{"error": err.Error()}
Write(w, status, m)
}
// BadRequest return 400 response
func BadRequest(w http.ResponseWriter, err error) {
Error(w, http.StatusBadRequest, err)
}
// Unauthorized return 401 response
func Unauthorized(w http.ResponseWriter, err error) {
Error(w, http.StatusUnauthorized, err)
}
// NotFound return a 404 response
func NotFound(w http.ResponseWriter, err error) {
Error(w, http.StatusNotFound, err)
}
// MethodNotAllowed return 405 response
func MethodNotAllowed(w http.ResponseWriter, err error) {
Error(w, http.StatusMethodNotAllowed, err)
}
// InternalServerError return 505 response
func InternalServerError(w http.ResponseWriter, err error) {
Error(w, http.StatusInternalServerError, err)
}
// HandleError checks if the passed err is not nil,
// if it is: it will write InternalServerError to ResponseWriter
func HandleError(w http.ResponseWriter, err error) {
if err != nil {
InternalServerError(w, err)
}
}
// HandleErrorWithStatus checks if the passed err is not nil,
// if it is: it will write the passed status code with error message to ResponseWriter
func HandleErrorWithStatus(w http.ResponseWriter, status int, err error) {
if err != nil {
Error(w, status, err)
return
}
}