Skip to content

JohannesKlauss/astro-error-pages

Repository files navigation


Astro Error Pages

Bundlephobia Types NPM Version MIT License

Developed during Spacetime

npm i astro-error-pages

Astro Error Pages is a simple yet effective middleware for your Astro SSR (Server-Side Rendered) applications. It provides you with a way to intercept server responses, analyze status codes, and redirect to custom error pages when necessary. This functionality enables a more user-friendly error handling and overall smoother user experience.


Quick Start

In your Astro project create a file src/middleware.ts if you don't have one already. Then add the following code:

import {sequence} from "astro/middleware"
import {astroErrorPages} from "error-pages"

export const onRequest = sequence(astroErrorPages())

Custom Error Pages

To take advantage of Astro Error Pages, you'll need to create corresponding error pages at the specified path. For example, if you keep the default configuration, you should create pages like /error-pages/404, /error-pages/500, etc.

When a server response with a status code >= 400 is encountered, the user will be redirected to the corresponding error page.

For unexpected errors (those that throw exceptions), the user will be redirected to the 500 error page. If logInternalServerErrors is set to true (default), the error will also be logged to the console.

Documentation

Config

Option Type Default value Description
path string /error-pages The base path to redirect to if the server encountered an error.
logInternalServerErrors boolean true Whether to log internal server errors that causes a 500 error or not.

Support

  • Ask your question in the Github Discussions
  • Ask your question on StackOverflow

Found an issue or have a feature request?

Open up an issue or pull request and participate.

Local Development

Checkout this repo, run npm i and then go into the proper example (or add a new one) to test your case.

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

Distributed under the MIT License. See LICENSE for more information.

Contact

Johannes Klauss - @JohannesKlauss - klauss.johannes@gmail.com

Project Link: https://github.com/JohannesKlauss/astro-error-pages

Contributors