forked from rcrowley/go-tigertonic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnot_found.go
35 lines (32 loc) · 827 Bytes
/
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
package tigertonic
import (
"encoding/json"
"fmt"
"log"
"net/http"
)
// NotFoundHandler responds 404 to every request, possibly with a JSON body.
type NotFoundHandler struct{}
func (NotFoundHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
description := fmt.Sprintf("%s %s not found", r.Method, r.URL.Path)
if acceptJSON(r) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusNotFound)
var e string
if SnakeCaseHTTPEquivErrors {
e = "not_found"
} else {
e = "tigertonic.NotFound"
}
if err := json.NewEncoder(w).Encode(map[string]string{
"description": description,
"error": e,
}); nil != err {
log.Println(err)
}
} else {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusNotFound)
fmt.Fprint(w, description)
}
}