forked from extrame/goblet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
44 lines (39 loc) · 1.06 KB
/
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
package goblet
import (
"errors"
"fmt"
"log"
"net/http"
"runtime/debug"
"github.com/extrame/go-random"
"github.com/bjxujiang/goblet/config"
"github.com/sirupsen/logrus"
)
func (s *Server) wrapError(w http.ResponseWriter, err interface{}, withStack bool) {
if withStack {
w.WriteHeader(500)
}
if s.Env() == config.ProductEnv {
errKey := gorandom.RandomNumeric(10)
logrus.WithField("error", err).WithField("key", errKey).Errorf("Error(%T) Happened\n", err)
if withStack {
log.Print(string(debug.Stack()))
}
html := fmt.Sprintf(`<body><h4>Internal Error(%s)</h4><br/>The Random Key is %s</body>`, errKey, errKey)
w.Write([]byte(html))
} else {
w.Write([]byte("<body><h4>"))
w.Write([]byte(fmt.Sprintf("%T,%v", err, err)))
w.Write([]byte("</h4>"))
if withStack {
w.Write([]byte("<pre>"))
w.Write([]byte(debug.Stack()))
w.Write([]byte("</pre>"))
}
w.Write([]byte("</body>"))
}
}
var Interrupted = errors.New("interrupted error")
func (ctx *Context) WrapError(err error, info string) error {
return fmt.Errorf("[%s]err:%s", info, err)
}