-
Notifications
You must be signed in to change notification settings - Fork 3
/
error.go
73 lines (60 loc) · 1.74 KB
/
error.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
package torque
import (
_ "embed"
"encoding/json"
"errors"
"html/template"
"net/http"
)
var (
errNotImplemented = errors.New("method not implemented for route")
)
type errReload struct{ err error }
func (e errReload) Error() string {
if e.err == nil {
return "nil"
} else {
return e.err.Error()
}
}
// ReloadWithError can be returned from an Action and tells torque to re-render
// the page with the given error attached to the request context.
//
// Hint: Get the error with the UseError hook in the Loader and add some error
// state to the resulting ViewModel.
func ReloadWithError(err error) error {
return &errReload{err}
}
var (
//go:embed error.tmpl.html
errorPageHtml string
errorPageTemplate = template.Must(template.New("error").Parse(errorPageHtml))
)
// errResponse is the data structure used to render an error to the response body.
type errResponse struct {
Error error
StackTrace string
}
func writeErrorResponse(wr http.ResponseWriter, req *http.Request, err error, stack []byte) error {
var mode = UseMode(req.Context())
// in development mode, write detailed error reports to the response
if mode == ModeDevelopment {
defer wr.WriteHeader(http.StatusInternalServerError)
var res = errResponse{
Error: err,
StackTrace: string(stack),
}
switch req.Header.Get("Accept") {
case "application/json":
return json.NewEncoder(wr).Encode(&res)
case "text/html":
return errorPageTemplate.Execute(wr, &res)
}
http.Error(wr, err.Error(), http.StatusInternalServerError)
return nil
}
// in production mode, write the Go error message to the response
// and return a 500 status code -- perhaps this could be improved
http.Error(wr, err.Error(), http.StatusInternalServerError)
return nil
}