-
Notifications
You must be signed in to change notification settings - Fork 0
/
errorhandler.go
100 lines (86 loc) · 2.99 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
88
89
90
91
92
93
94
95
96
97
98
99
100
package httperror
import (
"bytes"
"encoding/json"
"mime"
"net/http"
"strconv"
)
// const contentTypeHTML = "text/html"
const (
contentTypeTextPlain = "text/plain"
contentTypeText = "text"
contentTypeJSON = "application/json"
)
// ErrorHandler handles an error.
type ErrorHandler = func(http.ResponseWriter, error)
// DefaultErrorHandler writes a reasonable default error response, using the status
// code from the error if it can be extracted (see [StatusCode]), or 500 by
// default, using the content type from from w.Header(), or text/html by
// default, and using any public message (see [PublicErrorf] and [Public].)
func DefaultErrorHandler(w http.ResponseWriter, e error) {
s := StatusCode(e)
w.WriteHeader(s)
var b bytes.Buffer
b.WriteString(http.StatusText(s))
if s := PublicMessage(e); s != "" {
b.WriteString(": ")
b.WriteString(s)
}
WriteResponse(w, s, b.Bytes())
}
// WriteResponse writes a reasonable default error response given the status
// code and optional error message. The default error handler
// [DefaultErrorHandler] calls this method after extracting the status code and any
// public error message.
func WriteResponse(w http.ResponseWriter, s int, m []byte) {
contentType := responseContentType(w)
switch contentType {
case contentTypeJSON:
writeJsonErrorBody(w, s, m)
case contentTypeTextPlain:
writePlainTextErrorBody(w, s, m)
case contentTypeText:
writePlainTextErrorBody(w, s, m)
default:
writeHtmlErrorBody(w, s, m)
}
}
func writeHtmlErrorBody(w http.ResponseWriter, s int, m []byte) {
_, _ = w.Write([]byte(`<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>`))
_, _ = w.Write([]byte(`Error `))
_, _ = w.Write([]byte(strconv.Itoa(s)))
_, _ = w.Write([]byte(`</title></head><body>`))
_, _ = w.Write([]byte(m))
_, _ = w.Write([]byte("</body></html>\n"))
}
func writePlainTextErrorBody(w http.ResponseWriter, s int, m []byte) {
_, _ = w.Write([]byte(strconv.Itoa(s)))
_, _ = w.Write([]byte(` `))
_, _ = w.Write([]byte(m))
_, _ = w.Write([]byte("\n"))
}
// jsonError prints an error using general guidelines from
// https://github.com/omniti-labs/jsend
func writeJsonErrorBody(w http.ResponseWriter, s int, m []byte) {
response := jsonhttperror{Status: "error", Message: string(m), Code: s}
json, _ := json.Marshal(response) // No error handling for error handling
_, _ = w.Write(json)
_, _ = w.Write([]byte("\n"))
}
type jsonhttperror struct {
Status string `json:"status"`
Message string `json:"message,omitempty"`
Code int `json:"code,omitempty"`
}
// responseContentType extracts the content type from the response writer, if
// the Content-Type header has been set. It does *not* return the entire
// content type header -- only the media type part (e.g. "text/html" but not
// "text/html; charset=UTF-8").
func responseContentType(w http.ResponseWriter) string {
var contentType string
if cts, ok := w.Header()["Content-Type"]; ok {
contentType, _, _ = mime.ParseMediaType(cts[0])
}
return contentType
}