forked from olliecoleman/alloy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
53 lines (43 loc) · 1008 Bytes
/
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
package handlers
import (
"log"
"net/http"
"github.com/olliecoleman/alloy/app/views"
)
type Error interface {
error
Status() int
}
type StatusError struct {
Code int
Err error
}
func (se StatusError) Error() string {
return se.Err.Error()
}
func (se StatusError) Status() int {
return se.Code
}
type Handler func(w http.ResponseWriter, r *http.Request) error
func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if err := h(w, r); err != nil {
switch e := err.(type) {
case StatusError:
log.Printf("HTTP %d - %+v\n", e.Status(), e.Err)
v := views.New(r)
v.RenderError(w, e.Status())
default:
log.Printf("%+v\n", err)
v := views.New(r)
v.RenderError(w, http.StatusInternalServerError)
}
}
}
func ErrorHandler(w http.ResponseWriter, r *http.Request) {
v := views.New(r)
v.RenderError(w, http.StatusInternalServerError)
}
func NotFoundHandler(w http.ResponseWriter, r *http.Request) {
v := views.New(r)
v.RenderError(w, http.StatusNotFound)
}