Skip to main content
‹ Errors
Function

isKeychainError()

Whether a value is a KeychainError, and, with a code, one with that code. A type guard: after it, TypeScript knows the error's code and native details.

Import
import { isKeychainError } from '@apexnative/keychain';

Usage​

import { isKeychainError, KeychainErrorCode } from '@apexnative/keychain';

try {
await vault.getItem('refreshToken');
} catch (error) {
if (isKeychainError(error, KeychainErrorCode.KeyInvalidated)) return signInAgain();
if (isKeychainError(error)) log(error.code, error.nativeCode);
throw error;
}

Signature​

function isKeychainError(error: unknown, code?: KeychainErrorCode): error is KeychainError;
ReturnsbooleanPlatformsiOSAndroid

Parameters​

ParameterTypeDescription
errorRequiredunknownWhat a catch caught.
codeKeychainErrorCodeA code to match: a constant, KeychainErrorCode.Lockout, or its string, 'LOCKOUT'. Without it, any code matches.

Returns​

true when error is a KeychainError and, if code is given, its code is that one. In TypeScript the error is a KeychainError from then on.

It is error instanceof KeychainError, with the code compared: one line instead of two in each catch. @apexnative/keychain/migrate, /compat and /jest export the same function, for the same class.

See also​