-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
46 lines (37 loc) · 911 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 bones
import (
"errors"
"fmt"
)
const (
// ErrorCodeInternal is an internal error code.
ErrorCodeInternal = "internal"
)
var _ = ErrorCode
// Error represents an error within the context of go-bones service.
type Error struct {
// Code is a machine-readable code.
Code string `json:"code"`
// Message is a human-readable message.
Message string `json:"message"`
// Inner is a wrapped error that is never shown to API consumers.
Inner error `json:"-"`
}
func (e Error) Error() string {
if e.Inner != nil {
return fmt.Sprintf("%s %s: %v", e.Code, e.Message, e.Inner)
}
return fmt.Sprintf("%s %s", e.Code, e.Message)
}
// Unwrap the error returning the error's reason.
func (e Error) Unwrap() error {
return e.Inner
}
// ErrorCode returns the code of the error, if available.
func ErrorCode(err error) string {
var e Error
if errors.As(err, &e) {
return e.Code
}
return ""
}