-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
58 lines (53 loc) · 1.24 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
package web
import (
"io/fs"
"net/http"
)
// ErrorHandler captures 404 and 500 errors and serves /404.html or /500.html from the file system.
func ErrorHandler(h http.Handler, fsys fs.FS) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
writer := &responseWriter{
ResponseWriter: w,
fsys: fsys,
}
h.ServeHTTP(writer, r)
})
}
type responseWriter struct {
http.ResponseWriter
fsys fs.FS
noWrite bool
err error
}
func (w *responseWriter) Write(b []byte) (int, error) {
if w.noWrite {
return len(b), w.err
}
return w.ResponseWriter.Write(b)
}
func (w *responseWriter) WriteHeader(statusCode int) {
var (
b []byte
err error
file string
)
if statusCode == http.StatusNotFound {
file = "404.html"
} else if statusCode == http.StatusInternalServerError {
file = "500.html"
}
if file != "" {
// special processing of response
b, err = fs.ReadFile(w.fsys, file)
if err == nil {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Header().Del("X-Content-Type-Options")
w.ResponseWriter.WriteHeader(statusCode)
w.noWrite = true
_, w.err = w.ResponseWriter.Write(b)
return
}
}
// normal processing
w.ResponseWriter.WriteHeader(statusCode)
}