Skip to content

Server error codes #646

Open
Open

Description

@Mykhailo-Sichkaruk

Proposal

It would be great if server lib had codes for errors.
There are such error codes for frontend - #367 .

Consider example -

  • you want to run some logic if counter is lower than expected,
  • and you want to run another logic if origin mismathes
  • else rethrow

I want to it to look like this in my code:

			try {
				const verification = await verifyAuthenticationResponse({
				...
				});
			} catch (error: WebAuthnError) {
			        if (error.code === 'COUNTER_TOO_LOW') {
			            // logout user
			            // delete passkey
			        } else if (error.code === 'ORIGIN_MISMATCH'){
			            throw new Error('I'm a teapot');
                                } else {
                                    throw error;
                                }
			}

Just say what do you think about it generally and we can start discussing implementation details.
I want to contribute.


P.S.: thank you for such great library

Activity

  1. MasterKale commented on Dec 6, 2024

    @MasterKale
    Owner

    Hello @Mykhailo-Sichkaruk, thanks for the suggestion! This is a great idea, I see how it'd be nice to bring some order to the many more errors that @simplewebauthn/server might raise. I'll begin thinking about this and try to implement a custom error like @simplewebauthn/browser's WebAuthnError from #367 like you called out... 🤔

  2. Mykhailo-Sichkaruk commented on Dec 9, 2024

    @Mykhailo-Sichkaruk
    Author

    Thanks for considering this feature 😊
    I'm also thinking how we can improve error handling

    I have a related idea: grouping errors to handle not just one error but set of related errors.

    For example, a PublicKeyError category could group all public key-related issues, such as:

    • PublicKeyMissingAlgError
    • PublicKeyInvalidAlgError
    • other

    This could be implemented with inheritance:

    class PublicKeyError extends WebAuthnError {}
    class PublicKeyInvalidAlgError extends PublicKeyError {}

    Or, alternatively, using a list of error codes:

    const PublicKeyErrors = ['public_key_invalid_alg', 'public_key_missing_alg', ...];

    This can help not only in handling errors, but also in understanding for users of the lib how errors relate to each other and how they should be handled.

  3. junderw commented on Aug 25, 2026

    @junderw

    Ran into this today, had to write some regex to match the message string passed into an error deep in this library.

    Luckily it's open source, and I wrote some unit tests to check for regressions if I ever update and the message changes.

    I noticed pull requests are locked. Would you be interested in taking a patch if I have some time to work on it?

  4. added this to the v14.1.0 milestone on Aug 29, 2026
  5. MasterKale commented on Aug 29, 2026

    @MasterKale
    Owner

    Thanks for keeping this issue alive @junderw. I still think this is worth doing. I'm trying to get v14.0.0 out the door, but it's got enough features in it that I'm going to wait till after that goes out to begin working on this as a "v14.1.0" release.

    I noticed pull requests are locked. Would you be interested in taking a patch if I have some time to work on it?

    I appreciate the offer but will respectfully decline. I'm all ears for any proposals you might have for how to pull this off though. My game plan remains to add something like @simplewebauthn/browser's WebAuthnError into @simplewebauthn/server, with a code property that gets set to some enum string like ERROR_CEREMONY_ABORTED, etc... Then I'd update everything to consistently throw WebAuthnError.

  6. junderw commented on Sep 3, 2026

    @junderw

    Browser's WebAuthnError is perfect. Can't wait to get rid of all those regression tests against error messages (AI helped a lot though).

    No rush. Just don't change the error messages until then! 😆

  7. MasterKale commented on Sep 4, 2026

    @MasterKale
    Owner

    I always think of this issue when I throw new Error(...) in @simplwebauthn/server. So while working on #800 I thought it might be possible to establish a pattern that'll finally address this issue (not with #800, but definitely in the short term):

    https://github.com/MasterKale/SimpleWebAuthn/blob/10b4f53802290c867edcdc67072be069fa2ed8a3/packages/server/src/errors/index.ts

    export class SimpleWebAuthnError extends Error {
      code: SimpleWebAuthnErrorCode;
    
      constructor({ message, code, cause }: {
        message: string;
        code: SimpleWebAuthnErrorCode;
        cause?: Error;
      }) {
        super(message, { cause });
        this.name = 'SimpleWebAuthnError';
        this.code = code;
      }
    }
    
    export class PQCNotSupportedError extends SimpleWebAuthnError {
      constructor(alg: COSEALG) {
        const webCryptoAlg = mapCoseAlgToWebCryptoKeyAlgName(alg);
        const message = `This runtime's WebCrypto.subtle does not support use of ${webCryptoAlg}`;
        super({ message, code: 'RUNTIME_NO_PQC_SUPPORT' });
      }
    }
    
    export type SimpleWebAuthnErrorCode = 'RUNTIME_NO_PQC_SUPPORT';

    Then even if I get fancy with subclasses of SimpleWebAuthnError users of the library could still use a simple error instanceof SimpleWebAuthnError to know it's something out of this library 🤔

  8. MasterKale commented on Sep 4, 2026

    @MasterKale
    Owner

    And in @simplewebauthn/browser I could add a simple subclass of Error so that even the existing WebAuthnError in that library could be detected with error instanceof SimpleWebAuthnError without breaking existing code that handles WebAuthnError:

    export class SimpleWebAuthnError extends Error {}
    
    export class WebAuthnError extends SimpleWebAuthnError {
      // ...
    }
  9. modified the milestones: v14.1.0, v14.2.0 on Oct 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions