/
error_writer.go
123 lines (101 loc) · 2.69 KB
/
error_writer.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
package errorwriter
import (
"bytes"
"fmt"
"html/template"
"net/http"
"os"
"code.cloudfoundry.org/gorouter/logger"
"github.com/uber-go/zap"
)
type ErrorWriter interface {
WriteError(
rw http.ResponseWriter,
code int,
message string,
logger logger.Logger,
)
}
type plaintextErrorWriter struct{}
func NewPlaintextErrorWriter() ErrorWriter {
return &plaintextErrorWriter{}
}
// WriteStatus attempts to template an error message.
func (ew *plaintextErrorWriter) WriteError(
rw http.ResponseWriter,
code int,
message string,
logger logger.Logger,
) {
body := fmt.Sprintf("%d %s: %s", code, http.StatusText(code), message)
if code != http.StatusNotFound {
logger.Info("status", zap.String("body", body))
}
if code > 299 {
rw.Header().Del("Connection")
}
rw.Header().Set("Content-Type", "text/plain; charset=utf-8")
rw.Header().Set("X-Content-Type-Options", "nosniff")
rw.WriteHeader(code)
fmt.Fprintln(rw, body)
}
type htmlErrorWriter struct {
tpl *template.Template
}
type htmlErrorWriterContext struct {
Status int
StatusText string
Message string
Header http.Header
}
func NewHTMLErrorWriterFromFile(path string) (ErrorWriter, error) {
ew := &htmlErrorWriter{}
bytes, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("Could not read HTML error template file: %s", err)
}
tpl, err := template.New("error-message").Parse(string(bytes))
if err != nil {
return nil, err
}
ew.tpl = tpl
return ew, nil
}
// WriteStatus attempts to template an error message.
// If the template cannot be rendered then text will be sent instead
// and the error will be returned even though the response has been sent
func (ew *htmlErrorWriter) WriteError(
rw http.ResponseWriter,
code int,
message string,
logger logger.Logger,
) {
body := fmt.Sprintf("%d %s: %s", code, http.StatusText(code), message)
if code != http.StatusNotFound {
logger.Info("status", zap.String("body", body))
}
if code > 299 {
rw.Header().Del("Connection")
}
tplContext := htmlErrorWriterContext{
Status: code,
StatusText: http.StatusText(code),
Message: message,
Header: rw.Header(),
}
rw.Header().Set("Content-Type", "text/html; charset=utf-8")
var respBytes []byte
var rendered bytes.Buffer
if err := ew.tpl.Execute(&rendered, &tplContext); err != nil {
logger.Error("render-error-failed", zap.Error(err))
rw.Header().Set("Content-Type", "text/plain; charset=utf-8")
rw.Header().Set("X-Content-Type-Options", "nosniff")
respBytes = []byte(body)
} else {
rw.Header().Set("Content-Type", "text/html; charset=utf-8")
rw.Header().Set("X-Content-Type-Options", "nosniff")
respBytes = rendered.Bytes()
}
rw.WriteHeader(code)
rw.Write(respBytes)
}