-
Notifications
You must be signed in to change notification settings - Fork 4
/
error.go
59 lines (47 loc) · 974 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
53
54
55
56
57
58
59
package httperror
import (
"context"
"fmt"
)
type Error interface {
Ctx() context.Context
IsHttpError() bool
}
// this also implements the error interface
type Impl struct {
ctx context.Context
err error
status int
}
func New(ctx context.Context, message string, status int) error {
return &Impl{
ctx: ctx,
err: fmt.Errorf(message),
status: status,
}
}
func Wrap(ctx context.Context, message string, status int, err error) error {
wrappedError := fmt.Errorf("%s: %w", message, err)
return &Impl{
ctx: ctx,
err: wrappedError,
status: status,
}
}
func (e *Impl) Error() string {
return e.err.Error()
}
func (e *Impl) Ctx() context.Context {
return e.ctx
}
func (e *Impl) Status() int {
return e.status
}
// the presence of this method makes the interface unique and thus recognizable by a simple type check
func (e *Impl) IsHttpError() bool {
return true
}
func Is(err error) bool {
_, ok := err.(Error)
return ok
}