forked from paulbellamy/mango
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshow_errors.go
More file actions
37 lines (32 loc) · 715 Bytes
/
show_errors.go
File metadata and controls
37 lines (32 loc) · 715 Bytes
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
package mango
import (
"bytes"
"fmt"
"html/template"
)
func ShowErrors(templateString string) Middleware {
if templateString == "" {
templateString = `
<html>
<body>
<p>
{{.Error|html}}
</p>
</body>
</html>
`
}
errorTemplate := template.Must(template.New("error").Parse(templateString))
return func(env Env, app App) (status Status, headers Headers, body Body) {
defer func() {
if err := recover(); err != nil {
buffer := bytes.NewBufferString("")
errorTemplate.Execute(buffer, struct{ Error string }{fmt.Sprintf("%s", err)})
status = 500
headers = Headers{}
body = Body(buffer.String())
}
}()
return app(env)
}
}