-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
255 lines (233 loc) · 7.41 KB
/
errors.go
File metadata and controls
255 lines (233 loc) · 7.41 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package mppx
import (
"errors"
"fmt"
"net/http"
)
// ProblemDetails is an RFC 9457 Problem Details object returned in 402 response bodies.
type ProblemDetails struct {
Type string `json:"type"`
Title string `json:"title"`
Status int `json:"status"`
Detail string `json:"detail"`
ChallengeID string `json:"challengeId,omitempty"`
}
// PaymentError is the base type for all payment-related errors.
// It implements the error interface and serializes to RFC 9457 Problem Details.
type PaymentError struct {
// ErrorType is the RFC 9457 problem type URI.
ErrorType string
// Title is a human-readable summary.
Title string
// HTTPStatus is the HTTP status code (default 402).
HTTPStatus int
// Detail is a human-readable explanation.
Detail string
}
func (e *PaymentError) Error() string {
return e.Detail
}
// ToProblemDetails converts the error to RFC 9457 Problem Details format.
func (e *PaymentError) ToProblemDetails(challengeID string) ProblemDetails {
return ProblemDetails{
Type: e.ErrorType,
Title: e.Title,
Status: e.HTTPStatus,
Detail: e.Detail,
ChallengeID: challengeID,
}
}
// Status returns the HTTP status code for this error (defaults to 402).
func (e *PaymentError) Status() int {
if e.HTTPStatus == 0 {
return http.StatusPaymentRequired
}
return e.HTTPStatus
}
// IsPaymentError reports whether err is or wraps a *PaymentError.
func IsPaymentError(err error) bool {
_, ok := AsPaymentError(err)
return ok
}
// AsPaymentError returns the *PaymentError within err, or (nil, false).
func AsPaymentError(err error) (*PaymentError, bool) {
var pe *PaymentError
if errors.As(err, &pe) {
return pe, true
}
return nil, false
}
// --- Error constructors ---
// ErrMalformedCredential creates a "malformed credential" error (402).
func ErrMalformedCredential(reason string) *PaymentError {
detail := "Credential is malformed."
if reason != "" {
detail = fmt.Sprintf("Credential is malformed: %s.", reason)
}
return &PaymentError{
ErrorType: "https://paymentauth.org/problems/malformed-credential",
Title: "Malformed Credential",
HTTPStatus: http.StatusPaymentRequired,
Detail: detail,
}
}
// ErrInvalidChallenge creates an "invalid challenge" error (402).
func ErrInvalidChallenge(id, reason string) *PaymentError {
idPart := ""
if id != "" {
idPart = fmt.Sprintf(" %q", id)
}
reasonPart := ""
if reason != "" {
reasonPart = ": " + reason
}
return &PaymentError{
ErrorType: "https://paymentauth.org/problems/invalid-challenge",
Title: "Invalid Challenge",
HTTPStatus: http.StatusPaymentRequired,
Detail: fmt.Sprintf("Challenge%s is invalid%s.", idPart, reasonPart),
}
}
// ErrVerificationFailed creates a "verification failed" error (402).
func ErrVerificationFailed(reason string) *PaymentError {
detail := "Payment verification failed."
if reason != "" {
detail = fmt.Sprintf("Payment verification failed: %s.", reason)
}
return &PaymentError{
ErrorType: "https://paymentauth.org/problems/verification-failed",
Title: "Verification Failed",
HTTPStatus: http.StatusPaymentRequired,
Detail: detail,
}
}
// ErrPaymentRequired creates a "payment required" error (402).
func ErrPaymentRequired(description string) *PaymentError {
detail := "Payment is required."
if description != "" {
detail = fmt.Sprintf("Payment is required (%s).", description)
}
return &PaymentError{
ErrorType: "https://paymentauth.org/problems/payment-required",
Title: "Payment Required",
HTTPStatus: http.StatusPaymentRequired,
Detail: detail,
}
}
// ErrPaymentExpired creates a "payment expired" error (402).
func ErrPaymentExpired(expires string) *PaymentError {
detail := "Payment has expired."
if expires != "" {
detail = fmt.Sprintf("Payment expired at %s.", expires)
}
return &PaymentError{
ErrorType: "https://paymentauth.org/problems/payment-expired",
Title: "Payment Expired",
HTTPStatus: http.StatusPaymentRequired,
Detail: detail,
}
}
// ErrInvalidPayload creates an "invalid payload" error (402).
func ErrInvalidPayload(reason string) *PaymentError {
detail := "Credential payload is invalid."
if reason != "" {
detail = fmt.Sprintf("Credential payload is invalid: %s.", reason)
}
return &PaymentError{
ErrorType: "https://paymentauth.org/problems/invalid-payload",
Title: "Invalid Payload",
HTTPStatus: http.StatusPaymentRequired,
Detail: detail,
}
}
// ErrBadRequest creates a "bad request" error (400).
func ErrBadRequest(reason string) *PaymentError {
detail := "Bad request."
if reason != "" {
detail = fmt.Sprintf("Bad request: %s.", reason)
}
return &PaymentError{
ErrorType: "https://paymentauth.org/problems/bad-request",
Title: "Bad Request",
HTTPStatus: http.StatusBadRequest,
Detail: detail,
}
}
// ErrPaymentInsufficient creates a "payment insufficient" error (402).
func ErrPaymentInsufficient(reason string) *PaymentError {
detail := "Payment amount is insufficient."
if reason != "" {
detail = fmt.Sprintf("Payment insufficient: %s.", reason)
}
return &PaymentError{
ErrorType: "https://paymentauth.org/problems/payment-insufficient",
Title: "Payment Insufficient",
HTTPStatus: http.StatusPaymentRequired,
Detail: detail,
}
}
// ErrMethodUnsupported creates a "method unsupported" error (400).
func ErrMethodUnsupported(method string) *PaymentError {
detail := "Payment method is not supported."
if method != "" {
detail = fmt.Sprintf("Payment method %q is not supported.", method)
}
return &PaymentError{
ErrorType: "https://paymentauth.org/problems/method-unsupported",
Title: "Method Unsupported",
HTTPStatus: http.StatusBadRequest,
Detail: detail,
}
}
// ErrInsufficientBalance creates an "insufficient balance" error (402) for streaming sessions.
func ErrInsufficientBalance(reason string) *PaymentError {
detail := "Insufficient balance."
if reason != "" {
detail = fmt.Sprintf("Insufficient balance: %s.", reason)
}
return &PaymentError{
ErrorType: "https://paymentauth.org/problems/session/insufficient-balance",
Title: "Insufficient Balance",
HTTPStatus: http.StatusPaymentRequired,
Detail: detail,
}
}
// ErrInvalidSignature creates an "invalid signature" error (402) for session vouchers.
func ErrInvalidSignature(reason string) *PaymentError {
detail := "Invalid signature."
if reason != "" {
detail = fmt.Sprintf("Invalid signature: %s.", reason)
}
return &PaymentError{
ErrorType: "https://paymentauth.org/problems/session/invalid-signature",
Title: "Invalid Signature",
HTTPStatus: http.StatusPaymentRequired,
Detail: detail,
}
}
// ErrChannelNotFound creates a "channel not found" error (410).
func ErrChannelNotFound(reason string) *PaymentError {
detail := "No channel with this ID exists."
if reason != "" {
detail = fmt.Sprintf("Channel not found: %s.", reason)
}
return &PaymentError{
ErrorType: "https://paymentauth.org/problems/session/channel-not-found",
Title: "Channel Not Found",
HTTPStatus: http.StatusGone,
Detail: detail,
}
}
// ErrChannelClosed creates a "channel closed" error (410).
func ErrChannelClosed(reason string) *PaymentError {
detail := "Channel is closed."
if reason != "" {
detail = fmt.Sprintf("Channel closed: %s.", reason)
}
return &PaymentError{
ErrorType: "https://paymentauth.org/problems/session/channel-finalized",
Title: "Channel Closed",
HTTPStatus: http.StatusGone,
Detail: detail,
}
}