# How to throw errors from content sources?

Source: https://docs.arcxp.com/en/knowledge-articles/pagebuilder-engine/how-to-throw-errors-from-content-sources-.html

## 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

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**

```javascript
const NotFoundError = (message = 'Not Found') => {
  const err = new Error(message);
  err.statusCode = 404;
  return err;
}
```

**Custom redirect error**

```javascript
const RedirectError = (message = 'Redirect', code = 302, location) => {
  const err = new Error(message);
  err.statusCode = code;
  err.location = location;
  return err;
}
```
