-
Notifications
You must be signed in to change notification settings - Fork 0
/
paddleerr.go
65 lines (50 loc) · 1.64 KB
/
paddleerr.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
// Code generated by the Paddle SDK Generator; DO NOT EDIT.
package paddleerr
import (
"fmt"
"strings"
)
type ErrorType string
type Error struct {
Status int `json:"-"`
Type ErrorType `json:"type"`
Code string `json:"code"`
Detail string `json:"detail"`
DocumentationURL string `json:"documentation_url"`
Extra map[string]any `json:"extra"`
Errors []ValidationError `json:"errors,omitempty"`
}
type ValidationError struct {
Field string `json:"field"`
Message string `json:"message"`
}
const (
ErrorTypeRequestError ErrorType = "request_error"
ErrorTypeAPIError ErrorType = "api_error"
)
// Error conforms to the errors interface.
// It returns a string representation of the error, which contains the type and
// code of the error, and the detail if it exists.
// If the documentation URL exists, it will be appended to the end of the string.
func (e *Error) Error() string {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("%s.%s", e.Type, e.Code))
if e.Detail != "" {
sb.WriteString(fmt.Sprintf(": %s", e.Detail))
}
if e.DocumentationURL != "" {
sb.WriteString(fmt.Sprintf(" (%s)", e.DocumentationURL))
}
for _, e := range e.Errors {
sb.WriteString(fmt.Sprintf("\n - %s: %s", e.Field, e.Message))
}
return sb.String()
}
// Is returns true if the target error is a *Error and the error type and code match.
func (e *Error) Is(target error) bool {
targetAsserted, targetOk := target.(*Error)
if !targetOk {
return false
}
return (e.Code == targetAsserted.Code && e.Type == targetAsserted.Type)
}