Development of this hapi plugin is supported by Future Studio University 🚀
Join the Future Studio University and Skyrocket in Node.js
A hapi plugin to return an error view for web requests, providing more details of the issue. hapi-dev-errors
will give you the exact file where the error happend and a nice error stacktrace within the browser. Skip the extra look at your command line to catch the issue's location.
hapi-dev-errors
seamlessly integrates Youch to show the error details.
Besides the web view, hapi-dev-errors
prints pretty error details to the terminal. This is nice when running your hapi server as an API. Printing error details to the console is enabled by default. To disable the terminal error, use the toTerminal: false
option.
This plugin uses async/await which requires Node.js v8 or newer.
Add hapi-dev-errors
as a dependency to your project:
# NPM 5: this way is yours
npm i hapi-dev-errors
# NPM 4:
npm i -S hapi-dev-errors
Use the 1.3.2
release of hapi-dev-errors
with hapi v16. Later versions are only compatible with hapi v17.
# NPM 5: this way is yours
npm i hapi-dev-errors@1.3.2
# NPM 4: use NPM shortcuts to (i)nstall and (-S)ave the module as a dependency
npm i -S hapi-dev-errors@1.3.2
Check out the examples directory and get
an impression on how to configure hapi-dev-errors
with the individual plugins options and how to customize the
error view.
hapi-dev-errors
is disabled by default to avoid leaking sensitive error details during production.
Enable the plugin by define a "truthy" value for the showErrors
option.
The most straight forward way to register the hapi-dev-errors
plugin:
await server.register({
plugin: require('hapi-dev-errors'),
options: {
showErrors: process.env.NODE_ENV !== 'production'
}
})
// went smooth like chocolate :)
The following plugin options allow you to customize the default behavior of hapi-dev-errors
:
-
showErrors:
(boolean)
, default:false
— by default, the plugin is disabled and keeps hapi's default error handling behavior -
template:
(string)
, no default — provide the template name that you want to render withh.view(template, errorData)
-
toTerminal:
(boolean)
, default:true
— print pretty errors to the terminal as well (enabled by default)
await server.register({
plugin: require('hapi-dev-errors'),
options: {
showErrors: process.env.NODE_ENV !== 'production',
template: 'my-error-view',
toTerminal: true
}
})
// went smooth like chocolate :)
hapi-dev-errors
supports the template
option while registering the plugin. Provide a template name to
use your personal error template and not the default one shipped with hapi-dev-errors
. In case you pass a string
value for the template name, the view will be rendered with h.view(template, errorData).code(500)
.
Available properties to use in your custom error view:
-
title
: error title likeInternal Server Error
-
statusCode
: HTTP response status code (always 500) -
message
: error message, likeUncaught error: h.view(...).test is not a function
-
method
: HTTP request method, likeGET
-
url
: URL request path, like/signup
-
headers
: HTTP request headers object, in key-value format -
payload
: HTTP request payload, only available for HTTP methods other thanGET
, in key-value format -
stacktrace
: error stacktrace
Do you miss a feature? Please don’t hesitate to create an issue with a short description of your desired addition to this plugin.
- hapi tutorial series with 90+ tutorials
- Youch - Pretty error reporting for Node.js
- Youch terminal - Pretty error reporting on your terminal
- Create a fork
- Create your feature branch:
git checkout -b my-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request 🚀
MIT © Future Studio
futurestud.io · GitHub @fs-opensource · Twitter @futurestud_io