fix: improve error handling for non-JSON API responses

- Wrap response.json() in try-catch to handle non-JSON responses
- Fall back to response.text() and HTTP status information
- Preserve original HTTP error details instead of masking with JSON parse errors
- Ensure robust error reporting for HTML/text responses from server
This commit is contained in:
bymyself
2025-12-11 00:57:29 -08:00
parent 7e4912ef30
commit 8b7194e073

View File

@@ -223,10 +223,23 @@ const initiateCheckout = async (tierKey: TierKey) => {
)
if (!response.ok) {
const errorData = await response.json()
let errorMessage = 'Failed to initiate checkout'
try {
const errorData = await response.json()
errorMessage = errorData.message || errorMessage
} catch {
// If JSON parsing fails, try to get text response or use HTTP status
try {
const errorText = await response.text()
errorMessage = errorText || `HTTP ${response.status} ${response.statusText}`
} catch {
errorMessage = `HTTP ${response.status} ${response.statusText}`
}
}
throw new FirebaseAuthStoreError(
t('toastMessages.failedToInitiateSubscription', {
error: errorData.message || 'Failed to initiate checkout'
error: errorMessage
})
)
}