-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
115 lines (103 loc) · 3.02 KB
/
Copy patherrors.go
File metadata and controls
115 lines (103 loc) · 3.02 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
package bitpin
import (
"encoding/json"
"fmt"
"github.com/amiwrpremium/go-bitpin/types"
)
// GoBitpinError is the base error type for all errors in the SDK
type GoBitpinError struct {
Message string
Err error
}
func (e *GoBitpinError) Error() string {
if e.Err != nil {
return fmt.Sprintf("%s: %v", e.Message, e.Err)
}
return e.Message
}
// Unwrap implements the errors.Unwrap interface
func (e *GoBitpinError) Unwrap() error {
return e.Err
}
// RequestError represents errors that occur during HTTP request creation or sending
type RequestError struct {
GoBitpinError
Operation string // e.g., "creating request", "sending request"
}
// APIError represents errors returned by the Bitpin API
type APIError struct {
GoBitpinError
StatusCode int
Details map[string][]string // Store field-specific errors
}
// parseErrorResponse attempts to parse various error response formats from the API
func parseErrorResponse(statusCode int, respBody []byte) *APIError {
var details map[string][]string
// Try parsing as map[string][]string first (for field-specific errors)
if err := json.Unmarshal(respBody, &details); err == nil {
return &APIError{
GoBitpinError: GoBitpinError{
Message: fmt.Sprintf("API error (status %d): %s", statusCode, formatErrorDetails(details)),
},
StatusCode: statusCode,
Details: details,
}
}
// Try parsing as map[string]string (for simple key-value errors)
var simpleDetails map[string]string
if err := json.Unmarshal(respBody, &simpleDetails); err == nil {
details = make(map[string][]string)
for k, v := range simpleDetails {
details[k] = []string{v}
}
return &APIError{
GoBitpinError: GoBitpinError{
Message: fmt.Sprintf("API error (status %d): %s", statusCode, formatErrorDetails(details)),
},
StatusCode: statusCode,
Details: details,
}
}
// Try parsing as ErrorResponse struct
var errResp types.ErrorResponse
if err := json.Unmarshal(respBody, &errResp); err == nil {
details = make(map[string][]string)
if errResp.Detail != "" {
details["detail"] = []string{errResp.Detail}
}
if errResp.Code != "" {
details["code"] = []string{errResp.Code}
}
for k, v := range errResp.Messages {
details[k] = []string{v}
}
return &APIError{
GoBitpinError: GoBitpinError{
Message: fmt.Sprintf("API error (status %d): %s", statusCode, formatErrorDetails(details)),
},
StatusCode: statusCode,
Details: details,
}
}
// If all parsing attempts fail, return error with raw response
return &APIError{
GoBitpinError: GoBitpinError{
Message: fmt.Sprintf("API error (status %d): %s", statusCode, string(respBody)),
},
StatusCode: statusCode,
Details: map[string][]string{"raw": {string(respBody)}},
}
}
// formatErrorDetails creates a human-readable error message from the error details
func formatErrorDetails(details map[string][]string) string {
msg := ""
for field, errors := range details {
if len(errors) > 0 {
if msg != "" {
msg += "; "
}
msg += fmt.Sprintf("%s: %v", field, errors[0])
}
}
return msg
}