Implement logging and error handling for React Native.
Add logging
To add logging, perform the following steps:
- Register for events callbacks.
const registerEventsCallback = async () => {
const eventEmitter =
Platform.OS === 'ios'
? new NativeEventEmitter(NativeModules.FordefiSdk)
: new NativeEventEmitter(NativeModules.FordefiSdkModule);
- Add an events listener to the event type
LogEvent
:
eventEmitter.addListener('LogEvent', (event) => {
console.log(`${event.logLevel}: ${event.logMessage}`);
});
- After the Fordefi SDK is initialized, call the setLogger method with one of the desired LogLevels (
type LogLevel = 'verbose' | 'info' | 'warning' | 'error'
):
await setLogger("info");
Add error handling
To add error handling, perform the following steps:
- Register events callback.
const registerEventsCallback = async () => {
const eventEmitter =
Platform.OS === 'ios'
? new NativeEventEmitter(NativeModules.FordefiSdk)
: new NativeEventEmitter(NativeModules.FordefiSdkModule);
- Add events listener to event type
ErrorEvent
:
const eventEmitter = Platform.OS === 'ios' ? new NativeEventEmitter(NativeModules.FordefiSdk): new NativeEventEmitter(NativeModules.FordefiSdkModule);
eventEmitter.addListener('ErrorEvent', event => {
console.log(`Error ${event.errorCode}: ${event.errorMessage}`);
});
- After the Fordefi SDK is initialized, call the setErrorHandler method:
await setErrorHandler();