Skip to content

Conversation

@lewie9021
Copy link
Contributor

@lewie9021 lewie9021 commented Sep 6, 2025

This PR includes:

  • Added a custom KeychainError error class
  • Added a fix similar to ci: fix e2e #767 to get the CI checks passing

This is mostly a follow on from #762 and looks to make it easier for consumers to identify errors from this library and provide type safety when responding to various error codes:

import Keychain, { KeychainError, ERROR_CODE } from "react-native-keychain";

Keychain.getGenericPassword()
  .catch((err) => {
    if (err instanceof KeychainError) {
      console.log('Retry error?', err.retryable);

      switch (err.code) {
        case ERROR_CODE.AUTH_CANCELED:
          console.log('Authentication was canceled');
          break;
        case ERROR_CODE.BIOMETRIC_TIMEOUT:
          console.log('Biometric prompt timed out');
          break;
        case ERROR_CODE.BIOMETRIC_LOCKOUT:
          console.log('Too many failed attempts, biometric authentication locked');
          break;
        default:
          console.log(`Keychain error: ${err.code} - ${err.message}`);
      }
    }
  })

If there's interest, I'd be happy to extend the KeychainError class with static methods that can help for cases where only specific error handling is required:

import Keychain, { KeychainError } from "react-native-keychain";

Keychain.getGenericPassword()
  .catch((err) => {
    if (KeychainError.isAuthCanceledError(err)) {
      return console.log('Authentication was canceled');
    }

    console.log(`Error: ${err.message}`);
  })

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.

1 participant