@bagelink/sdk 0.0.1274 → 0.0.1276
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/index.ts +2 -0
- package/bin/utils.ts +66 -0
- package/package.json +1 -1
package/bin/index.ts
CHANGED
package/bin/utils.ts
CHANGED
|
@@ -71,3 +71,69 @@ export async function handleAuthCode(_withAuth: boolean, _bagelinkDir: string) {
|
|
|
71
71
|
await formatAndWriteCode(authPath, authCode)
|
|
72
72
|
}
|
|
73
73
|
}
|
|
74
|
+
|
|
75
|
+
export function formatAPIErrorMessage(error: any) {
|
|
76
|
+
// Handle case where error or response is undefined
|
|
77
|
+
if (!error || !error.response) {
|
|
78
|
+
return 'Network error occurred. Please check your connection.'
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const { status, data } = error.response
|
|
82
|
+
|
|
83
|
+
// Handle validation errors (422)
|
|
84
|
+
if (data?.detail && Array.isArray(data.detail)) {
|
|
85
|
+
return data.detail
|
|
86
|
+
.map((err: any) => {
|
|
87
|
+
// Handle nested field paths properly
|
|
88
|
+
const fieldPath = err.loc.slice(1).join('.')
|
|
89
|
+
const field = fieldPath
|
|
90
|
+
.split('.')
|
|
91
|
+
.map((part: string) => part.charAt(0).toUpperCase() + part.slice(1))
|
|
92
|
+
.join(' ')
|
|
93
|
+
|
|
94
|
+
let message = err.msg
|
|
95
|
+
// Common validation messages
|
|
96
|
+
.replace(/^(?:field|value|string|none) required$/i, 'is required')
|
|
97
|
+
.replace(/^value is not a valid/i, 'must be a valid')
|
|
98
|
+
.replace(/^ensure this value/i, 'this value must')
|
|
99
|
+
.replace(/^str type expected$/i, 'must be text')
|
|
100
|
+
.replace(/^value could not be parsed to/i, 'must be a')
|
|
101
|
+
.replace(/^ensure this value has at least/i, 'must have at least')
|
|
102
|
+
.replace(/^ensure this value has at most/i, 'must have at most')
|
|
103
|
+
.replace(/^invalid datetime format/i, 'must be a valid date/time')
|
|
104
|
+
.replace(/^value is not a valid email address/i, 'must be a valid email address')
|
|
105
|
+
.replace(/^value is not a valid integer/i, 'must be a whole number')
|
|
106
|
+
.replace(/^value is not a valid float/i, 'must be a valid number')
|
|
107
|
+
|
|
108
|
+
return `${field} ${message.toLowerCase()}`
|
|
109
|
+
})
|
|
110
|
+
.join('\n')
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Handle specific HTTP status codes
|
|
114
|
+
switch (status) {
|
|
115
|
+
case 401:
|
|
116
|
+
return 'Authentication required. Please log in.'
|
|
117
|
+
case 403:
|
|
118
|
+
return 'You do not have permission to perform this action.'
|
|
119
|
+
case 404:
|
|
120
|
+
return 'The requested resource was not found.'
|
|
121
|
+
case 429:
|
|
122
|
+
return 'Too many requests. Please try again later.'
|
|
123
|
+
case 500:
|
|
124
|
+
return 'An internal server error occurred. Please try again later.'
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Handle other errors with detail
|
|
128
|
+
if (data?.detail && typeof data.detail === 'string') {
|
|
129
|
+
return data.detail
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// Handle errors with a message field
|
|
133
|
+
if (data?.message) {
|
|
134
|
+
return data.message
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Generic error fallback
|
|
138
|
+
return `An error occurred (Status ${status}): ${error.message || 'Unknown error'}`
|
|
139
|
+
}
|