-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
52 lines (43 loc) · 912 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
48
49
50
51
52
package jh
import (
"net/http"
"github.com/pkg/errors"
)
// Error is an error interface that also tracks http status codes
type Error interface {
error
HasStatus
}
// NewError returns an Error with a given http status code
func NewError(msg string, status int) Error {
return &e{
msg: msg,
status: status,
}
}
// HasStatus is an interface that allows a type to expose an http status code.
type HasStatus interface {
Status() int
}
type e struct {
msg string
status int
}
func (e *e) Status() int {
return e.status
}
func (e *e) Error() string {
return e.msg
}
// Wrap behaves as github.com/pkg/errors.Wrap, but preserves http status codes.
func Wrap(err error, msg string) Error {
if err == nil {
return nil
}
status := http.StatusInternalServerError
switch err := err.(type) {
case Error:
status = err.Status()
}
return NewError(errors.Wrap(err, msg).Error(), status)
}