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.

Procedure

  1. Create an error in the content source. This can be a custom class or a regular error.
  2. Set a statusCode. If it is a 3xx status code, you can provide a location to redirect to.
  3. Use throw to 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;
}