diff --git a/src/hooks/useShakespeare.ts b/src/hooks/useShakespeare.ts index 233c0a2..3f5d643 100644 --- a/src/hooks/useShakespeare.ts +++ b/src/hooks/useShakespeare.ts @@ -109,9 +109,11 @@ async function handleAPIError(response: Response) { } else if (response.status === 402) { throw new Error('Insufficient credits. Please add credits to your account to use premium models, or use the free "tybalt" model.'); } else if (response.status === 400) { + // Read raw text first so we always have the body for logging + const rawBody = await response.text().catch(() => '(unreadable body)'); + console.error('[Shakespeare API 400] raw body:', rawBody); try { - const error = await response.json(); - console.error('[Shakespeare API 400]', JSON.stringify(error)); + const error = JSON.parse(rawBody); if (error.error?.type === 'invalid_request_error') { if (error.error.code === 'minimum_amount_not_met') { throw new Error(`Minimum credit amount is $${error.error.minimum_amount}. Please increase your payment amount.`); @@ -121,11 +123,12 @@ async function handleAPIError(response: Response) { throw new Error('Invalid redirect URL provided for Stripe payment.'); } } - const msg = error.error?.message || error.message || error.details || JSON.stringify(error); + const msg = error.error?.message || error.message || error.details || rawBody; throw new Error(`API 400: ${msg}`); } catch (inner) { if (inner instanceof Error && inner.message.startsWith('API 400:')) throw inner; - throw new Error('Invalid request. Please check your parameters and try again.'); + // Body was not JSON or unrecognised structure — surface the raw body + throw new Error(`API 400: ${rawBody}`); } } else if (response.status === 404) { throw new Error('Resource not found. Please check the payment ID or try again.');