This repository has been archived by the owner on Oct 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patherrors.go
108 lines (93 loc) · 2.05 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
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
package kwiscale
import (
"errors"
"fmt"
"text/template"
)
var (
// ErrNotFound error type.
ErrNotFound = errors.New("Not found")
// ErrNotImplemented error type.
ErrNotImplemented = errors.New("Not implemented")
// ErrInternalError for internal error.
ErrInternalError = errors.New("Internal server error")
)
// HTTPErrorHandler interface.
type HTTPErrorHandler interface {
// Error returns the error.
GetError() error
// Details returns some detail inteface.
Details() interface{}
// Status returns the http status code.
Status() int
setStatus(int)
setError(error)
setDetails(interface{})
}
// ErrorHandler is a basic error handler that
// displays error in a basic webpage.
type ErrorHandler struct {
RequestHandler
status int
err error
details interface{}
}
func (dh *ErrorHandler) setStatus(s int) {
dh.status = s
}
func (dh *ErrorHandler) setError(err error) {
dh.err = err
}
func (dh *ErrorHandler) setDetails(d interface{}) {
dh.details = d
}
// GetError returns error that was set by handlers.
func (dh *ErrorHandler) GetError() error {
return dh.err
}
// Details returns details or nil if none.
func (dh *ErrorHandler) Details() interface{} {
return dh.details
}
// Status returns the error HTTP Status set by handlers.
func (dh *ErrorHandler) Status() int {
return dh.status
}
// Get shows a standard error in HTML.
func (dh *ErrorHandler) Get() {
tpl := `<!doctype html>
<html>
<head>
<title>ERROR {{.Status}}</title>
<style>
html, body {
font-family: Sans, sans-serif;
}
main {
margin: auto;
width: 80%;
border: 2px solid #880000;
padding: 2em;
}
</style>
</head>
<body>
<main>
<h1>ERROR {{ .Status}}</h1>
<p>{{ .Error }}</p>
<pre>{{ range .Details }}{{ . }}{{ end }}</pre>
</main>
</body>
</html>`
t, err := template.New("error").Parse(tpl)
if err != nil {
fmt.Println(err)
return
}
dh.Response().WriteHeader(dh.Status())
t.Execute(dh.Response(), map[string]interface{}{
"Status": dh.Status(),
"Error": dh.GetError(),
"Details": dh.Details(),
})
}