From 6e70d94ef47a58923a4c514feb9de029ecd6ad0b Mon Sep 17 00:00:00 2001 From: Andrii Kostenko Date: Mon, 16 Feb 2026 08:10:24 +0000 Subject: [PATCH] fix: handle axios errors in Turnstile service to prevent misleading 500 responses Unhandled axios errors from Cloudflare API were propagating as 500 Internal Server Error with confusing message "Request failed with status code 400". Now catches these errors and returns a proper 400 BadRequest with a clear message. Co-Authored-By: Claude Opus 4.6 --- .../src/shared/services/turnstile.service.ts | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/backend/src/shared/services/turnstile.service.ts b/backend/src/shared/services/turnstile.service.ts index 26d82e0af..6bd704f29 100644 --- a/backend/src/shared/services/turnstile.service.ts +++ b/backend/src/shared/services/turnstile.service.ts @@ -29,15 +29,21 @@ export class TurnstileService { formData.append('secret', secretKey); formData.append('response', token); - const response = await axios.post(this.verifyUrl, formData.toString(), { - headers: { - 'Content-Type': 'application/x-www-form-urlencoded', - }, - }); - - if (!response.data.success) { - const errorCodes = response.data['error-codes']?.join(', ') || 'Unknown error'; - throw new BadRequestException(`Turnstile verification failed: ${errorCodes}`); + try { + const response = await axios.post(this.verifyUrl, formData.toString(), { + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + }, + }); + + if (!response.data.success) { + const errorCodes = response.data['error-codes']?.join(', ') || 'Unknown error'; + throw new BadRequestException(`Turnstile verification failed: ${errorCodes}`); + } + } catch (error) { + if (error instanceof BadRequestException) throw error; + console.error('Turnstile verification error:', error?.response?.data || error?.message || error); + throw new BadRequestException('Turnstile verification failed. Please try again.'); } return true;