Error handler function
Middleware function
import { createFej, createErrorMiddleware } from 'fej';
const api = createFej();
api.use('error-handler', createErrorMiddleware((error, ctx) => {
console.error(`Request to ${ctx.request.url} failed:`, error.message);
}));
api.use('error-handler', createErrorMiddleware(async (error, ctx) => {
// Log to error tracking service
await errorTracker.report({
message: error.message,
url: ctx.request.url,
stack: error.stack,
});
// Show user-friendly message
if (ctx.response?.status === 404) {
console.log('Resource not found');
}
}));
Create an error handling middleware
Creates middleware that catches and handles errors from downstream middleware and the fetch request. Converts non-FejError errors to FejError instances.