Skip to content

Commit 6e70d94

Browse files
guguclaude
andcommitted
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 <noreply@anthropic.com>
1 parent 3e5434f commit 6e70d94

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

backend/src/shared/services/turnstile.service.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,21 @@ export class TurnstileService {
2929
formData.append('secret', secretKey);
3030
formData.append('response', token);
3131

32-
const response = await axios.post<TurnstileVerifyResponse>(this.verifyUrl, formData.toString(), {
33-
headers: {
34-
'Content-Type': 'application/x-www-form-urlencoded',
35-
},
36-
});
37-
38-
if (!response.data.success) {
39-
const errorCodes = response.data['error-codes']?.join(', ') || 'Unknown error';
40-
throw new BadRequestException(`Turnstile verification failed: ${errorCodes}`);
32+
try {
33+
const response = await axios.post<TurnstileVerifyResponse>(this.verifyUrl, formData.toString(), {
34+
headers: {
35+
'Content-Type': 'application/x-www-form-urlencoded',
36+
},
37+
});
38+
39+
if (!response.data.success) {
40+
const errorCodes = response.data['error-codes']?.join(', ') || 'Unknown error';
41+
throw new BadRequestException(`Turnstile verification failed: ${errorCodes}`);
42+
}
43+
} catch (error) {
44+
if (error instanceof BadRequestException) throw error;
45+
console.error('Turnstile verification error:', error?.response?.data || error?.message || error);
46+
throw new BadRequestException('Turnstile verification failed. Please try again.');
4147
}
4248

4349
return true;

0 commit comments

Comments
 (0)