react-native-filament
Version:
A real-time physically based 3D rendering engine for React Native
45 lines (42 loc) • 1.46 kB
JavaScript
import { Worklets } from 'react-native-worklets-core';
/**
* Report an error to react native's `ErrorUtils` if available, or log to console otherwise.
*/
export function reportFatalError(error) {
// @ts-expect-error this is defined by react-native.
if (global.ErrorUtils != null) {
// @ts-expect-error this is defined by react-native.
global.ErrorUtils.reportFatalError(error);
} else {
console.error(`An unknown Filament error occurred!`, error);
}
}
const throwErrorOnJS = Worklets.createRunOnJS((message, stack) => {
const error = new Error();
error.message = message;
error.stack = stack;
error.name = 'Filament Error';
// @ts-expect-error this is react-native specific
error.jsEngine = 'Filament';
// From react-native:
// @ts-ignore the reportFatalError method is an internal method of ErrorUtils not exposed in the type definitions
reportFatalError(error);
});
export function reportWorkletError(error) {
'worklet';
const safeError = error;
const message = safeError != null && 'message' in safeError ? safeError.message : 'Filament threw an error.';
throwErrorOnJS(message, safeError === null || safeError === void 0 ? void 0 : safeError.stack);
}
export function wrapWithErrorHandler(callback) {
return (...args) => {
'worklet';
try {
return callback(...args);
} catch (error) {
reportFatalError(error);
throw error;
}
};
}
//# sourceMappingURL=ErrorUtils.js.map