diff --git a/examples/nextjs-bot-categories/app/api/arcjet/route.ts b/examples/nextjs-bot-categories/app/api/arcjet/route.ts index ea98b5a24..ae5fcb719 100644 --- a/examples/nextjs-bot-categories/app/api/arcjet/route.ts +++ b/examples/nextjs-bot-categories/app/api/arcjet/route.ts @@ -43,12 +43,21 @@ function checkSpoofed(rule: ArcjetRuleResult) { return rule.reason.isBot() && rule.reason.isSpoofed() } +function collectErrors(errors: string[], rule: ArcjetRuleResult) { + if (rule.reason.isError()) { + return [...errors, rule.reason.message]; + } else { + return errors; + } +} + export async function GET(req: Request) { const decision = await aj.protect(req); - if (decision.isErrored()) { + const errors = decision.results.reduce(collectErrors, []); + if (errors.length > 0) { return NextResponse.json( - { error: decision.reason.message }, + { errors }, { status: 500, statusText: "Internal Server Error" }, ) } diff --git a/examples/nextjs-server-actions/app/actions.ts b/examples/nextjs-server-actions/app/actions.ts index 0474dd36e..7777396ea 100644 --- a/examples/nextjs-server-actions/app/actions.ts +++ b/examples/nextjs-server-actions/app/actions.ts @@ -1,6 +1,6 @@ "use server" -import arcjet, { request, validateEmail } from "@arcjet/next"; +import arcjet, { ArcjetRuleResult, request, validateEmail } from "@arcjet/next"; const aj = arcjet({ key: process.env.ARCJET_KEY!, @@ -11,6 +11,14 @@ const aj = arcjet({ ] }); +function collectErrors(errors: string[], rule: ArcjetRuleResult) { + if (rule.reason.isError()) { + return [...errors, rule.reason.message]; + } else { + return errors; + } +} + export async function validate(prev: { message: string }, formData: FormData) { const email = formData.get("email"); @@ -28,8 +36,9 @@ export async function validate(prev: { message: string }, formData: FormData) { // If Arcjet encounters an error, you could fail "open" or you could respond // with a "closed"-style message like below - if (decision.isErrored()) { - console.log("Error occurred:", decision.reason.message); + const errors = decision.results.reduce(collectErrors, []); + if (errors.length > 0) { + console.log("Errors occurred:", errors); return { message: "Encountered an error" }