forked from ahmadrezamontazerolghaem/Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttperr.go
77 lines (59 loc) · 1.25 KB
/
httperr.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
package httperr
import (
"fmt"
"runtime"
"strings"
"github.com/convox/rack/Godeps/_workspace/src/github.com/stvp/rollbar"
)
const ErrorHandlerSkipLines = 7
type Error struct {
code int
err error
stack rollbar.Stack
trace []string
}
func New(code int, err error) *Error {
if err == nil {
return nil
}
e := &Error{
code: code,
err: err,
stack: rollbar.BuildStack(3),
trace: errorTrace(),
}
return e
}
func Server(err error) *Error {
return New(500, err)
}
func Errorf(code int, format string, args ...interface{}) *Error {
return New(code, fmt.Errorf(format, args...))
}
func (e *Error) Code() int {
return e.code
}
func (e *Error) Error() string {
return e.err.Error()
}
func (e *Error) Save() error {
rollbar.ErrorWithStack(rollbar.ERR, e.err, e.stack)
return nil
}
func (e *Error) Trace() []string {
return e.trace
}
func (e *Error) Server() bool {
return e.code >= 500 && e.code < 600
}
func (e *Error) User() bool {
return e.code >= 400 && e.code < 500
}
func errorTrace() []string {
buffer := make([]byte, 1024*1024)
size := runtime.Stack(buffer, false)
trace := strings.Split(string(buffer[0:size]), "\n")
// skip lines associated with error handler
skipped := trace[ErrorHandlerSkipLines:]
return skipped
}