forked from gobuffalo/buffalo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
not_found.go
96 lines (92 loc) · 1.78 KB
/
not_found.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
package buffalo
import (
"encoding/json"
"net/http"
"github.com/gobuffalo/velvet"
"github.com/pkg/errors"
)
// NotFoundHandler is the default ErrorHandler for 404 responses.
// In development mode it attempts to return useful debugging
// information. In production it defaults to use the http.NotFound
// handler.
func NotFoundHandler(status int, err error, c Context) error {
env := c.Get("env")
req := c.Request()
res := c.Response()
if env != nil && env.(string) == "production" {
http.NotFound(res, req)
return nil
}
data := map[string]interface{}{
"routes": c.Get("routes"),
"method": req.Method,
"path": req.URL.String(),
"error": err.Error(),
}
ct := req.Header.Get("Content-Type")
if ct == "application/json" {
res.WriteHeader(404)
return json.NewEncoder(res).Encode(data)
}
ctx := velvet.NewContextWith(data)
t, err := velvet.Render(htmlNotFound, ctx)
if err != nil {
return errors.WithStack(err)
}
res.WriteHeader(404)
_, err = res.Write([]byte(t))
return err
}
var htmlNotFound = `
<html>
<head>
<title>404 PAGE NOT FOUND</title>
<style>
body {
font-family: helvetica;
}
table {
width: 100%;
}
th {
text-align: left;
}
tr:nth-child(even) {
background-color: #dddddd;
}
td {
margin: 0px;
padding: 10px;
}
</style>
</head>
<body>
<h1>404 Page Not Found!</h1>
<h3>Could not find path <code>[{{method}}] {{path}}</code></h3>
<hr>
<table id="buffalo-routes-table">
<thead>
<tr>
<th>METHOD</th>
<th>PATH</th>
<th>HANDLER</th>
</tr>
</thead>
<tbody>
{{#each routes as |route|}}
<tr>
<td>{{route.Method}}</td>
<td>{{route.Path}}</td>
<td><code>{{route.HandlerName}}</code></td>
</tr>
{{/each}}
</tbody>
</table>
{{#if error}}
<hr>
<h2>Error</h2>
<pre>{{error}}</pre>
{{/if}}
</body>
</html>
`