Error Handling
Handling errors gracefully is crucial for a smooth user experience during a KYC process.
Initialization Errors
If the SDK fails to initialize (e.g. invalid API key, backend down), it will throw an error. You should wrap your mount() or launch() calls in a try/catch block.
try {
await Spidify.mount();
} catch (error) {
// Show a fallback UI to the user
console.error("Failed to load KYC:", error.message);
}
Runtime Errors
During the execution of the workflow, if a critical failure occurs, the onFailure callback will be triggered.
const Spidify = new SpidifySDK({
// ...
onFailure: (errorMessage) => {
alert(`Verification failed: ${errorMessage}`);
// Redirect user or allow retry
}
});
Headless Errors
If you are using the KycSDK (Headless Engine), you should listen to the error event:
sdk.on('error', (message) => {
showErrorToast(message);
});
Additionally, promises returned by methods like submitOtp or uploadDocument will reject on failure, so handle them appropriately:
try {
await sdk.submitOtp('123456');
} catch (err) {
setOtpError("Invalid OTP entered. Please try again.");
}