Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error instanceof BiometryError, error.code typing #20

Closed
aeharding opened this issue Nov 12, 2023 · 3 comments
Closed

error instanceof BiometryError, error.code typing #20

aeharding opened this issue Nov 12, 2023 · 3 comments

Comments

@aeharding
Copy link

For developer experience with typescript, I want to do error instanceof BiometryError like the following:

    try {
      await BiometricAuth.authenticate({
        allowDeviceCredential: true,
      });
    } catch (error) {
      if (error instanceof BiometryError && error.code === 'biometryNotEnrolled') {
         // do thing with error.code
      }

      throw error;
    }

Because error instanceof BiometryError evaluates to false, I have to do the following:

    try {
      await BiometricAuth.authenticate({
        allowDeviceCredential: true,
      });
    } catch (error) {
      if (error && typeof error === 'object' &&'code' in error && error.code === 'biometryNotEnrolled') {
        
      }
      throw error;
    }

or I have to unsafely cast.

Also, BiometryError.code is typed as string, it would be nice if it was BiometryErrorType.

Thank you!

@aeharding aeharding changed the title Extend error instanceof BiometryError, code typing Nov 12, 2023
@aeharding aeharding changed the title error instanceof BiometryError, code typing error instanceof BiometryError, error.code typing Nov 12, 2023
@aparajita
Copy link
Owner

The error thrown by any Capacitor plugin is actually an instance of CapacitorException. I'm not sure if that's something that happened after I originally wrote this plugin, because I don't remember that being the case.

Even if I intercept the exception and throw a BiometryError instance, you would still need a type guard because Typescript does not type catch variables. So you can achieve what you want now like this:

interface TypedBiometryError extends BiometryError {
  code: BiometryErrorType
}

async function showErrorAlert(error: TypedBiometryError): Promise<void> {
  await showAlert(`${error.message} [${error.code}].`);
}

function isBiometryError(error: unknown): error is TypedBiometryError {
  return error instanceof CapacitorException;
}

function isBiometryError(error: unknown): error is TypedBiometryError {
  return error instanceof CapacitorException;
}

async function authenticate(): Promise<void> {
    try {
      await BiometricAuth.authenticate({
        allowDeviceCredential: true,
      });
    } catch (error) {
      // The type guard will ensure you compare error.code against a valid BiometryErrorType value
      if (isBiometryError(error) && error.code === BiometryErrorType.biometryNotEnrolled) {
        showAlert(error)
      }
      throw error;
    }
}

@aparajita
Copy link
Owner

In any case I should add that type guard function to the plugin and type code as BiometryErrorType.

@aparajita
Copy link
Owner

Thanks for the request, v6.0.0 has just been released that throws an instance of BiometryError from authenticate(), and BiometryError.code is typed as BiometryErrorType.

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

No branches or pull requests

2 participants