/
error.go
46 lines (36 loc) · 890 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
package usecase
import "github.com/swaggest/usecase/status"
// Error is an error with contextual information.
type Error struct {
AppCode int
StatusCode status.Code
Value error
Context map[string]interface{}
}
// Error returns error message.
func (e Error) Error() string {
return e.Unwrap().Error()
}
// Fields exposes structured context of error.
func (e Error) Fields() map[string]interface{} {
return e.Context
}
// AppErrCode returns application level error code.
func (e Error) AppErrCode() int {
return e.AppCode
}
// Status returns status code of error.
func (e Error) Status() status.Code {
return e.StatusCode
}
// Unwrap returns parent error.
func (e Error) Unwrap() error {
if e.StatusCode != 0 {
return status.Wrap(e.Value, e.StatusCode)
}
return e.Value
}
type sentinelError string
func (s sentinelError) Error() string {
return string(s)
}