forked from razonyang/clevergo-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
47 lines (39 loc) · 961 Bytes
/
error.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
package core
import (
"net/http"
"github.com/clevergo/clevergo"
)
type ErrorHandler struct {
app *Application
viewName string
}
func NewErrorHandler(app *Application) *ErrorHandler {
return &ErrorHandler{
app: app,
viewName: "site/error",
}
}
func (eh *ErrorHandler) Handle(ctx *clevergo.Context, err error) {
eh.app.logger.Errorln(err)
var errinfo map[string]interface{}
switch e := err.(type) {
case clevergo.StatusError:
errinfo = map[string]interface{}{
"code": e.Status(),
"message": e.Error(),
}
default:
errinfo = map[string]interface{}{
"code": http.StatusInternalServerError,
"message": http.StatusText(http.StatusInternalServerError),
}
}
if err := ctx.Render(http.StatusInternalServerError, eh.viewName, ViewData{
"error": errinfo,
}); err != nil {
ctx.Error(err.Error(), http.StatusInternalServerError)
}
}
func (eh *ErrorHandler) Error(ctx *clevergo.Context) error {
return nil
}