How to throw errors from content sources?
Summary
This method can be used in PageBuilder Engine content sources to generate not found 404 and redirect 302 errors/status codes from a PageBuilder Engine content source.
Important
When using custom errors, consider using fetch instead of resolve. Using fetch allows inspection of the returned data, allowing for the use of custom errors. A resolve will perform the data request, but default errors like redirects or 404 Not Found are processed automatically, preventing further logic from running. Custom errors can be used in a resolve function only when their use case does not depend on the returned data.
Procedure
Create an error in the content source. This can be a custom class or a regular error.
Set a
statusCode. If it is a 3xx status code, you can provide a location to redirect to.Use
throwto generate the error.
Examples:
Custom not found error
const NotFoundError = (message = 'Not Found') => {
const err = new Error(message);
err.statusCode = 404;
return err;
}
Custom redirect error
const RedirectError = (message = 'Redirect', code = 302, location) => {
const err = new Error(message);
err.statusCode = code;
err.location = location;
return err;
}