Since Adobe UXP lacks a global error handler, Bolt UXP provides polyfills to catch and track unhandled errors, which is useful for integrating services like Sentry.
1. Initialize the Global Error Handler
Use polyFillGlobalErrorHandler() to intercept errors. This should be called once during initialization.
import { polyFillGlobalErrorHandler } from 'src/api/errors';
export const initUXP = () => {
polyFillGlobalErrorHandler((error: Error) => {
// Example: sendToSentry(error);
});
};
2. Use Safe Wrappers
To handle errors locally while still reporting them to the global handler, use safe() for synchronous code and safeAsync() for asynchronous code. These return the result or an Error instance.
Synchronous:
const res = safe(() => window.notExist());
if (res instanceof Error) {
// handle error in-context
}
Asynchronous:
const res = await safeAsync(async () => window.notExist());
if (res instanceof Error) {
// handle error in-context
}
// Sync Example
const res = safe(() => window.notExist()); // error is sent to global error handler
if (res instanceof Error) {
return console.log("there was an error"); // also handle error in-context
}
console.log("all good");
// Async Example
const res = await safeAsync(async () => window.notExist()); // error is sent to global error handler
if (res instanceof Error) {
return console.log("there was an error"); // also handle error in-context
}
console.log("all good");