forked from x402-foundation/x402
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
131 lines (118 loc) · 3.89 KB
/
errors.go
File metadata and controls
131 lines (118 loc) · 3.89 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
package x402
import "fmt"
// PaymentError represents a payment-specific error
type PaymentError struct {
Code string `json:"code"`
Message string `json:"message"`
Details map[string]interface{} `json:"details,omitempty"`
}
func (e *PaymentError) Error() string {
return fmt.Sprintf("%s: %s", e.Code, e.Message)
}
// Common error codes
const (
ErrCodeInvalidPayment = "invalid_payment"
ErrCodePaymentRequired = "payment_required"
ErrCodeInsufficientFunds = "insufficient_funds"
ErrCodeNetworkMismatch = "network_mismatch"
ErrCodeSchemeMismatch = "scheme_mismatch"
ErrCodeSignatureInvalid = "signature_invalid"
ErrCodePaymentExpired = "payment_expired"
ErrCodeSettlementFailed = "settlement_failed"
ErrCodeUnsupportedScheme = "unsupported_scheme"
ErrCodeUnsupportedNetwork = "unsupported_network"
)
// Facilitator error constants
const (
ErrInvalidVersion = "invalid_version"
ErrInvalidV1Payload = "invalid_v1_payload"
ErrInvalidV1Requirements = "invalid_v1_requirements"
ErrInvalidV2Payload = "invalid_v2_payload"
ErrInvalidV2Requirements = "invalid_v2_requirements"
ErrNoFacilitatorForNetwork = "no_facilitator_for_network"
ErrInvalidResponse = "invalid_response"
)
// Server error constants
const (
ErrFailedToMarshalPayload = "failed_to_marshal_payload"
ErrFailedToMarshalRequirements = "failed_to_marshal_requirements"
)
// NewPaymentError creates a new payment error
func NewPaymentError(code, message string, details map[string]interface{}) *PaymentError {
return &PaymentError{
Code: code,
Message: message,
Details: details,
}
}
// VerifyError represents a payment verification failure
// All verification failures (business logic and system errors) are returned as errors
type VerifyError struct {
InvalidReason string // Error reason/code (e.g., "insufficient_balance", "invalid_signature")
Payer string // Payer address (if known)
InvalidMessage string // Optional invalid message details
}
// Error implements the error interface
func (e *VerifyError) Error() string {
if e.InvalidMessage != "" {
return fmt.Sprintf("%s: %s", e.InvalidReason, e.InvalidMessage)
}
return e.InvalidReason
}
// NewVerifyError creates a new verification error
//
// Args:
//
// reason: Error reason/code
// payer: Payer address (empty string if unknown)
// network: Network identifier (empty string if unknown)
// message: Optional invalid message details
//
// Returns:
//
// *VerifyError
func NewVerifyError(reason string, payer string, message string) *VerifyError {
return &VerifyError{
InvalidReason: reason,
Payer: payer,
InvalidMessage: message,
}
}
// SettleError represents a payment settlement failure
// All settlement failures (business logic and system errors) are returned as errors
type SettleError struct {
ErrorReason string // Error reason/code (e.g., "transaction_failed", "insufficient_balance")
Payer string // Payer address (if known)
Network Network // Network identifier
Transaction string // Transaction hash (if settlement was attempted)
ErrorMessage string // Optional error message details
}
// Error implements the error interface
func (e *SettleError) Error() string {
if e.ErrorMessage != "" {
return fmt.Sprintf("%s: %s", e.ErrorReason, e.ErrorMessage)
}
return e.ErrorReason
}
// NewSettleError creates a new settlement error
//
// Args:
//
// reason: Error reason/code
// payer: Payer address (empty string if unknown)
// network: Network identifier
// transaction: Transaction hash (empty string if not submitted)
// err: Optional underlying error
//
// Returns:
//
// *SettleError
func NewSettleError(reason string, payer string, network Network, transaction string, message string) *SettleError {
return &SettleError{
ErrorReason: reason,
Payer: payer,
Network: network,
Transaction: transaction,
ErrorMessage: message,
}
}