-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy patherrors.go
More file actions
92 lines (79 loc) · 1.99 KB
/
errors.go
File metadata and controls
92 lines (79 loc) · 1.99 KB
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
package verifactu
import (
"errors"
"strings"
)
// Standard gateway error responses
var (
ErrConnection = newError("connection")
ErrValidation = newError("validation")
ErrDuplicate = newError("duplicate")
ErrWarning = newError("warning")
)
// Standard error responses.
var (
ErrNotSpanish = ErrValidation.WithMessage("only spanish invoices are supported")
ErrAlreadyProcessed = ErrValidation.WithMessage("already processed")
ErrOnlyInvoices = ErrValidation.WithMessage("only invoices are supported")
ErrOnlyStatuses = ErrValidation.WithMessage("only bill status documents are supported")
)
// Error allows for structured responses from the gateway to be able to
// response codes and messages.
type Error struct {
key string
code string
message string
cause error
}
// Error produces a human readable error message.
func (e *Error) Error() string {
out := []string{e.key}
if e.code != "" {
out = append(out, e.code)
}
if e.message != "" {
out = append(out, e.message)
}
return strings.Join(out, ": ")
}
// Key returns the key for the error.
func (e *Error) Key() string {
return e.key
}
// Message returns the human message for the error.
func (e *Error) Message() string {
return e.message
}
// Code returns the code provided by the remote service.
func (e *Error) Code() string {
return e.code
}
func newError(key string) *Error {
return &Error{key: key}
}
// WithCode duplicates and adds the code to the error.
func (e *Error) WithCode(code string) *Error {
e = e.clone()
e.code = code
return e
}
// WithMessage duplicates and adds the message to the error.
func (e *Error) WithMessage(msg string) *Error {
e = e.clone()
e.message = msg
return e
}
func (e *Error) clone() *Error {
ne := new(Error)
*ne = *e
return ne
}
// Is checks to see if the target error is the same as the current one
// or forms part of the chain.
func (e *Error) Is(target error) bool {
t, ok := target.(*Error)
if !ok {
return errors.Is(e.cause, target)
}
return e.key == t.key
}