-
Notifications
You must be signed in to change notification settings - Fork 4
/
errorhandler.go
87 lines (79 loc) · 2.12 KB
/
errorhandler.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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"github.com/shurcooL/httperror"
)
type errorHandler struct {
handler func(w http.ResponseWriter, req *http.Request) error
}
func (h errorHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
rw := &headerResponseWriter{ResponseWriter: w}
err := h.handler(rw, req)
if err == nil {
// Do nothing.
return
}
if err != nil && rw.WroteHeader {
// The header has already been written, so it's too late to send
// a different status code. Just log the error and move on.
log.Println(err)
return
}
if err, ok := httperror.IsMethod(err); ok {
httperror.HandleMethod(w, err)
return
}
if err, ok := httperror.IsRedirect(err); ok {
http.Redirect(w, req, err.URL, http.StatusSeeOther)
return
}
if err, ok := httperror.IsBadRequest(err); ok {
httperror.HandleBadRequest(w, err)
return
}
if err, ok := httperror.IsHTTP(err); ok {
code := err.Code
http.Error(w, fmt.Sprintf("%d %s", code, http.StatusText(code))+"\n\n"+err.Error(), code)
return
}
if err, ok := httperror.IsJSONResponse(err); ok {
w.Header().Set("Content-Type", "application/json")
jw := json.NewEncoder(w)
jw.SetIndent("", "\t")
err := jw.Encode(err.V)
if err != nil {
log.Println("error encoding JSONResponse:", err)
}
return
}
if os.IsNotExist(err) {
log.Println(err)
http.Error(w, "404 Not Found\n\n"+err.Error(), http.StatusNotFound)
return
}
if os.IsPermission(err) {
log.Println(err)
http.Error(w, "403 Forbidden\n\n"+err.Error(), http.StatusForbidden)
return
}
log.Println(err)
http.Error(w, "500 Internal Server Error\n\n"+err.Error(), http.StatusInternalServerError)
}
// headerResponseWriter wraps a real http.ResponseWriter and captures
// whether or not the header has been written.
type headerResponseWriter struct {
http.ResponseWriter
WroteHeader bool // Write or WriteHeader was called.
}
func (rw *headerResponseWriter) Write(p []byte) (n int, err error) {
rw.WroteHeader = true
return rw.ResponseWriter.Write(p)
}
func (rw *headerResponseWriter) WriteHeader(code int) {
rw.WroteHeader = true
rw.ResponseWriter.WriteHeader(code)
}