/
errors.go
94 lines (82 loc) · 1.45 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
package greyhound
import (
"html/template"
"net/http"
)
type phpError struct {
ErrorType string
Text string
}
var tmpl = template.Must(template.New("").Parse(`
{{define "interpreterError"}}
<h1>Error</h1>
<div class="error">
<p><code>{{.}}</code></p>
</div>
{{end}}
{{define "timeoutError"}}
<h1>Timeout error</h1>
<div class="error">
<p>Waited {{.}} and received no response</p>
</div>
{{end}}
{{define "otherError"}}
<h1>Other error</h1>
<div class="error">
<p>{{.}}</p>
</div>
{{end}}
<!doctype html>
<html>
<head>
<title>Error</title>
<style>
html {
padding: 0;
}
body {
margin: 0;
color: black;
background: white;
font-family: sans-serif;
}
.container {
max-width: 700px;
background: #ddd;
margin: 0 auto;
padding: 20px;
}
h1 {
font-weight: normal;
}
.error {
overflow-x: auto;
background: #fdd;
}
</style>
</head>
<body>
<div class="container">
{{if eq .ErrorType "interpreterError"}}
{{template "interpreterError" .Text}}
{{else if eq .ErrorType "timeoutError"}}
{{template "timeoutError" .Text}}
{{else}}
{{template "otherError" .Text}}
{{end}}
</div>
</body>
</html>
`))
// Render the error template
func renderError(w http.ResponseWriter, t string, s string) {
w.WriteHeader(http.StatusInternalServerError)
e := phpError{
ErrorType: t,
Text: s,
}
err := tmpl.Execute(w, e)
if err != nil {
panic(err)
}
}