Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/firstandthird/hapi-friendly-errors
Hapi plugin to show a friendly error page
https://github.com/firstandthird/hapi-friendly-errors
hapi-plugin hapi-v17
Last synced: 3 days ago
JSON representation
Hapi plugin to show a friendly error page
- Host: GitHub
- URL: https://github.com/firstandthird/hapi-friendly-errors
- Owner: firstandthird
- License: mit
- Created: 2015-09-27T20:10:28.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2021-03-08T20:02:26.000Z (almost 4 years ago)
- Last Synced: 2025-01-02T05:28:52.210Z (20 days ago)
- Topics: hapi-plugin, hapi-v17
- Language: JavaScript
- Size: 62.5 KB
- Stars: 0
- Watchers: 6
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# hapi-friendly-errors
[![Coverage Status](https://coveralls.io/repos/github/firstandthird/hapi-friendly-errors/badge.svg)](https://coveralls.io/github/firstandthird/hapi-friendly-errors)
A small library for displaying very friendly errors in hapi.
## Installation
```
npm install hapi-friendly-errors
```## Basic Usage
Just register like a normal hapi plugin:
```js
await server.register({
plugin: require('hapi-friendly-errors')
});
```If you have a broken route like this:
```js
server.route({
path: '/foobar',
method: 'GET',
handler(request, reply) {
throw Boom.notFound('this is not foobar');
}
});
```when you fetch the _/foobar_ route, you will get back a 404 code with the following:
```html
There was an error
Not Found: this is not foobar
```## Custom Views
hapi-friendly-errors will also work with your view engine if you want a custom error page. For example
if you have a [handlebars](https://handlebarsjs.com/) template named _error-page_:```html
Unfortunately You Have Encountered an Error
Error: {{error}}
Reason: {{message}}
Additional Stuff: {{protocol}}{{domain}}
``````js
await server.register({
plugin: require('hapi-friendly-errors'),
options: {
view: 'error-page'
}
});
server.views({
engines: { html: require('handlebars') },
context: {
protocol: 'http://',
domain: 'foobar.com'
}
});
```Now fetching the _/foobar_ route will give:
```html
Unfortunately You Have Encountered an Error
Error: Not Found
Reason: this is not foobar
Additional Stuff: http://foobar.com
```## JSON
hapi-friendly-errors will skip HTML and just return a JSON object when requests contain the _Accept: application/json_ header:
```js
{ statusCode: 403, error: 'Forbidden', message: 'Not Authorized' }
```## Plugin Options
You can pass these when you register the plugin
- __errorBlacklist__
A regular expression passed as a string. Routes matching this regular expression will be ignored by hapi-friendly-errors
- __logErrors__
When true will log output any time there is a 500 Server error.
- __url__
You can also specify a local URL to render and return HTML errors, when this happens the statusCode, error and message will be passed as query parameters:
```js
await server.register({
plugin: require('hapi-friendly-errors'),
options: {
url: '/error'
}
});server.route({
path: '/foobar',
method: 'GET',
handler(request, reply) {
throw Boom.notFound('this is not foobar');
}
});server.route({
path: '/error',
method: 'GET',
handler(request, h) {
const query = request.query;
return `
An Error Handled by Route
Error ${query.statusCode} ${query.error.}
Message ${query.message}
`;
}
});
```Now when you call the _/foobar_ route you will get back:
```HTML
An Error Handled by Route
Error 404 Not Found
Message this is not foobar!
```- __context__
An object that will be merged with the context and passed to the rendering engine. If specified, _error_, _message_ and _statusCode_ will be overridden.
```js
await server.register({
plugin: require('hapi-friendly-errors'),
options: {
context: {
data1: 'some more data',
error: 'A Server Error'
}
}
});
```Now errors will look something like:
```html
There was an error
A Server Error: this is not foobar
```- __view__
Name of the view to render, if not specified then view will default to:
```js
`There was an error
${context.error}: ${context.message}
`
```