-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
107 lines (88 loc) · 2.15 KB
/
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package arpc
import (
"encoding/json"
)
// OKError implements this interface to mark errors as 200
type OKError interface {
OKError()
}
// Error always return 200 status with false ok value
// use this error for validate, precondition failed, etc.
type Error struct {
code string
msg string
err error
}
// OKError implements OKError
func (err *Error) OKError() {}
// Error implements error
func (err *Error) Error() string {
s := err.code
if s != "" {
s += " "
}
return s + err.msg
}
// Unwrap implements errors.Unwarp
func (err *Error) Unwrap() error {
return err.err
}
// MarshalJSON implements json.Marshaler
func (err *Error) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Code string `json:"code,omitempty"`
Message string `json:"message,omitempty"`
}{err.code, err.msg})
}
// Code returns error code
func (err *Error) Code() string {
return err.code
}
// Message returns error message
func (err *Error) Message() string {
return err.msg
}
// NewError creates new Error with message
func NewError(message string) error {
return &Error{msg: message}
}
// NewErrorCode creates new Error with code and message
func NewErrorCode(code, message string) error {
return &Error{code: code, msg: message}
}
func wrapError(err error) error {
return &Error{msg: err.Error(), err: err}
}
// WrapError wraps given error with OKError
func WrapError(err error) error {
if err == nil {
return nil
}
switch err.(type) {
case *Error:
return err
case *ProtocolError:
return err
default:
return wrapError(err)
}
}
// ProtocolError always returns 400 status with false ok value
// only use this error for invalid protocol usages
type ProtocolError struct {
Code string `json:"code,omitempty"`
Message string `json:"message,omitempty"`
}
func NewProtocolError(code, message string) error {
return &ProtocolError{code, message}
}
func (err *ProtocolError) Error() string {
return err.Message
}
// predefined errors
var (
ErrNotFound = NewProtocolError("", "not found")
ErrUnsupported = NewProtocolError("", "unsupported content type")
)
type internalError struct{}
func (internalError) Error() string { return "internal error" }