Skip to content

Keep the response status and original error on passkey errors - #41

Merged
joetannenbaum merged 2 commits into
laravel:mainfrom
muqg:keep-error-status-and-cause
Nov 6, 2026
Merged

joetannenbaum merged 2 commits into
laravel:mainfrom
muqg:keep-error-status-and-cause

Conversation

@muqg

@muqg muqg commented Oct 11, 2026

Copy link
Copy Markdown
Contributor

Every failure the package has no kind for reaches the application as a plain PasskeyError with nothing but a message. A refusal from the server, a request that never reached the server and an authenticator failing on its own all look the same, so the application can only show error.message as it arrives.

This is a problem for a localized application, and I ran into it while working on one. The server's sentences arrive already translated (Clave de acceso no reconocida.), while the browser's arrive in English (Failed to fetch), and there is no way to tell which is which in order to translate the latter.

In order to make that possible, a response with an error status now throws a ServerError that carries the response's status, and every other unmapped failure keeps the original error as cause. Both are additive. ServerError extends PasskeyError and keeps the same message, and cause is a new optional property, so existing instanceof PasskeyError checks and error.message keep working as before. Since the package targets ES2020, cause is declared on PasskeyError itself instead of relying on ES2022's ErrorOptions.

This means that an application can show the server's sentence where the server wrote one, and its own everywhere else. Today the last four cases below all arrive as the same PasskeyError:

import { ServerError, UserCancelledError } from "@laravel/passkeys";

const describe = (error) => {
    if (error instanceof UserCancelledError) {
        return trans("passkeys.cancelled");
    }

    // A crash or a proxy's error page, neither of them written for the user.
    if (error instanceof ServerError && error.status >= 500) {
        return trans("passkeys.server_error");
    }

    // The server's own sentence, already in the user's language.
    if (error instanceof ServerError) {
        return error.message;
    }

    // The request never reached the server.
    if (error.cause instanceof TypeError) {
        return trans("passkeys.offline");
    }

    // The authenticator failed on its own.
    return trans("passkeys.failed");
};

We add tests for both in errors.test.ts, http.test.ts and passkeys.test.ts, and document them under Typed Errors in the README.

@joetannenbaum
joetannenbaum merged commit 2d34949 into laravel:main Nov 6, 2026
3 checks passed
@joetannenbaum

Copy link
Copy Markdown
Collaborator

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants