From 631807b51103104a9f76ab3ae03854b62b0a2552 Mon Sep 17 00:00:00 2001 From: William Oldham Date: Sun, 17 Dec 2023 20:59:28 +0000 Subject: [PATCH] Add turnstile error codes --- src/index.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index 64b6092..1c3b93b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -90,7 +90,10 @@ async function validateTurnstile(context: Context) { }); const outcome = await result.json(); - return outcome.success; + return { + success: outcome.success as boolean, + errorCodes: outcome['error-codes'] as string[], + }; } app.get('/scrape', async (context) => { @@ -99,11 +102,15 @@ app.get('/scrape', async (context) => { const turnstileEnabled = Boolean(context.env?.TURNSTILE_ENABLED); if (turnstileEnabled) { - const success = await validateTurnstile(context); + const turnstileResponse = await validateTurnstile(context); - if (!success) { + if (!turnstileResponse.success) { context.status(401); - return context.text('Turnstile invalid'); + return context.text( + `Turnstile invalid, error codes: ${turnstileResponse.errorCodes.join( + ', ', + )}`, + ); } }