hapi-dev-errors
Advanced tools
Comparing version 2.3.0 to 3.0.0
# Changelog | ||
## Version 2.3.0 (2018-07-12) | ||
## Version [3.0.0](https://github.com/fs-opensource/hapi-dev-errors/compare/v2.3.0...v3.0.0) (2018-07-17) | ||
- `add` Youch error view as the default error view | ||
- `add` Insomnia REST client to list of REST clients | ||
- `add` register plugin only once by activating the hapi plugin’s `once: true` attribute | ||
- `update` response type for JSON responses to `application/json` | ||
- `update` JSON responses have well-formatted payload | ||
- `update` examples: use a wildcard route and show error details for each path | ||
- `remove` the `useYouch` plugin option (Youch is the default now) | ||
- `remove` previous default error view | ||
### Breaking Changes | ||
The most important breaking change is the removed `useYouch` option. Upgrading to `hapi-dev-errors` 3.0 should be no problem, even if you use the `useYouch` option. Because Youch is the default error view in 3.0, you’ll receive the exact same error view. | ||
If you used the default error view before, you’ll now see the Youch error details instead. You’ll like it :) | ||
## Version [2.3.0](https://github.com/fs-opensource/hapi-dev-errors/compare/v2.2.0...v2.3.0) (2018-07-12) | ||
- `add` new plugin option: `toTerminal`, default: `true` | ||
- `add` pretty error details in the terminal besides the web view. Based on [`youch-terminal`].(https://github.com/poppinss/youch-terminal) | ||
- `add` pretty error details in the terminal besides the web view. Based on [`youch-terminal`](https://github.com/poppinss/youch-terminal) | ||
- `update` minor code reformats and restructuring | ||
## Version 2.2.0 (2018-06-02) | ||
## Version [2.2.0](https://github.com/fs-opensource/hapi-dev-errors/compare/v2.1.0...v2.2.0) (2018-06-02) | ||
- `add` JSON response for CLI requests (cURL, Postman, wget) (thank you @pi0) | ||
## Version 2.1.0 (2018-02-12) | ||
## Version [2.1.0](https://github.com/fs-opensource/hapi-dev-errors/compare/v2.0.1...v2.1.0) (2018-02-12) | ||
- `add` status code to Youch error and show it in the view | ||
@@ -16,18 +32,18 @@ - `update` example code in readme to async/await | ||
## Version 2.0.1 (2017-11-06) | ||
## Version [2.0.1](https://github.com/fs-opensource/hapi-dev-errors/compare/v2.0.0...v2.0.1) (2017-11-06) | ||
- `update` dependencies to latest stable releases | ||
- `update` readme | ||
## Version 2.0.0 (2017-11-06) | ||
## Version [2.0.0](https://github.com/fs-opensource/hapi-dev-errors/compare/v2.0.0-rc.1...v2.0.0) (2017-11-06) | ||
- `update` readme: requirements and examples | ||
## Version 2.0.0-rc.1 (2017-10-27) | ||
## Version [2.0.0-rc.1](https://github.com/fs-opensource/hapi-dev-errors/compare/v1.3.2...v2.0.0-rc.1) (2017-10-27) | ||
- `update` code to support hapi v17 | ||
- `update` dependencies to newest versions | ||
## Version 1.3.2 (2017-10-18) | ||
## Version [1.3.2](https://github.com/fs-opensource/hapi-dev-errors/compare/v1.3.1...v1.3.2) (2017-10-18) | ||
- `update` code formatting to ESLint styling for hapi, based on `eslint-config-hapi` | ||
- `add` Handlebars as devDependency for template example | ||
## Version 1.3.1 (2017-10-17) | ||
## Version [1.3.1](https://github.com/fs-opensource/hapi-dev-errors/compare/v1.3.0...v1.3.1) (2017-10-17) | ||
- `remove` editor config for code formatting (`.editorconfig` file) | ||
@@ -37,6 +53,6 @@ - `add` eslint configuration via `.eslintrc.json` | ||
## Version 1.3.0 (2017-10-12) | ||
## Version [1.3.0](https://github.com/fs-opensource/hapi-dev-errors/compare/v1.2.0...v1.3.0) (2017-10-12) | ||
- `add` handling of promise rejections, like `reply(Promise.reject(new Error('')))` (Thank you [Tafari](https://github.com/tafarij)) | ||
## Version 1.2.0 (2017-08-18) | ||
## Version [1.2.0](https://github.com/fs-opensource/hapi-dev-errors/compare/v1.1.0...v1.2.0) (2017-08-18) | ||
- `add` new option `useYouch` (boolean) to delegate the error handling to Youch and use its view | ||
@@ -48,3 +64,3 @@ - `add` preview of Youch’s error view in the readme introduction | ||
## Version 1.1.0 (2017-06-14) | ||
## Version [1.1.0](https://github.com/fs-opensource/hapi-dev-errors/compare/v1.0.0...v1.1.0) (2017-06-14) | ||
- `optimize` plugin registration: abort early if `showErrors` is false, don’t get the template from filesystem and don’t hook the the request lifecycle extension point | ||
@@ -51,0 +67,0 @@ |
@@ -22,3 +22,3 @@ 'use strict' | ||
method: 'GET', | ||
path: '/', | ||
path: '/{path*}', | ||
handler: (request, h) => { | ||
@@ -25,0 +25,0 @@ h.notAvailable() |
@@ -22,4 +22,3 @@ 'use strict' | ||
showErrors: process.env.NODE_ENV !== 'production', | ||
template: 'error', | ||
useYouch: true // this will be ignored, option 'template' > 'useYouch' | ||
template: 'error' | ||
} | ||
@@ -40,3 +39,3 @@ } | ||
method: 'GET', | ||
path: '/', | ||
path: '/{path*}', | ||
handler: (request, reply) => { | ||
@@ -43,0 +42,0 @@ reply.notAvailable() |
'use strict' | ||
const Fs = require('fs') | ||
const Path = require('path') | ||
const Hoek = require('hoek') | ||
const Youch = require('youch') | ||
const ForTerminal = require('youch-terminal') | ||
const { promisify: Promisify } = require('util') | ||
const TemplatePath = Path.join(__dirname, './error.html') | ||
const ReadFile = Promisify(Fs.readFile) | ||
/** | ||
* Create a Youch instance for pretty error printing | ||
* on the console and in a web view | ||
* Create a Youch instance for pretty error printing. | ||
* This instance is used to format output for the console | ||
* and for a web view. | ||
* | ||
@@ -20,10 +16,8 @@ * @param {object} request - the request object | ||
function createYouch (request, error) { | ||
// clone the request to not change it due to this reference | ||
const req = Hoek.clone(request) | ||
// assign the url’s path to "url" property of request directly | ||
// hapi uses a URL object and Youch wants the path directly | ||
req.url = req.path | ||
request.url = request.path | ||
// assign httpVersion -> same as with request.url | ||
req.httpVersion = req.raw.req.httpVersion | ||
request.httpVersion = request.raw.req.httpVersion | ||
@@ -38,4 +32,15 @@ // let Youch show the error’s status code | ||
/** | ||
* helper function to test if string matches a value | ||
* Check whether the incoming request requires a JSON response. | ||
* This is true for requests where the "accept" header contains "json" | ||
* or the agent is a CLI/GUI app. | ||
* | ||
* @param {object} | ||
*/ | ||
function isJsonRequest ({ agent, accept }) { | ||
return matches(agent, /curl|wget|postman|insomnia/i) || matches(accept, /json/) | ||
} | ||
/** | ||
* Helper function to test if string matches a value | ||
*/ | ||
function matches (str, regex) { | ||
@@ -55,3 +60,2 @@ return str && str.match(regex) | ||
showErrors: false, | ||
useYouch: false, | ||
toTerminal: true | ||
@@ -61,3 +65,3 @@ } | ||
// merge user-defined plugin options into defaults | ||
const config = Object.assign(defaults, options) | ||
const config = Object.assign({}, defaults, options) | ||
@@ -76,5 +80,2 @@ // cut early if `showErrors` is false | ||
// read and keep the default error template | ||
const errorTemplate = await ReadFile(TemplatePath, 'utf8') | ||
// extend the request lifecycle at `onPreResponse` | ||
@@ -84,3 +85,3 @@ // to change the default error handling behavior (if enabled) | ||
// error response shortcut | ||
const error = request.response | ||
const error = Hoek.clone(request.response) | ||
@@ -101,3 +102,3 @@ // only show `bad implementation` developer errors (status code 500) | ||
payload: request.raw.req.method !== 'GET' ? request.payload : '', | ||
stacktrace: error.stack.replace(/[a-z_-\d]+.js:\d+:\d+/gi, '<mark>$&</mark>') | ||
stacktrace: error.stack | ||
} | ||
@@ -113,39 +114,27 @@ | ||
// take priority: check header if request is in CLI | ||
if (matches(agent, /curl|wget|postman/i)) { | ||
const formattedJSON = JSON.stringify(Object.assign({}, errorResponse, { | ||
stacktrace: errorResponse.stacktrace.replace(/<\/?mark>/g, '').split('\n').map(s => s.trim()) | ||
}), null, 2) | ||
// take priority: | ||
// - check "agent" header for REST request (cURL, Postman & Co.) | ||
// - check "accept" header for JSON request | ||
if (isJsonRequest({ accept, agent })) { | ||
const details = Object.assign({}, errorResponse, { | ||
stacktrace: errorResponse.stacktrace.split('\n').map(line => line.trim()) | ||
}) | ||
return h.response(formattedJSON).code(statusCode).type('application/json') | ||
return h | ||
.response(JSON.stringify(details, null, 2)) | ||
.type('application/json') | ||
.code(statusCode) | ||
} | ||
// take priority: check header if there’s a JSON REST request | ||
if (matches(accept, /json/)) { | ||
return h.response(errorResponse).code(statusCode) | ||
} | ||
// did the user explicitly specify an error template | ||
// this is high priority: custom template overrides any other template | ||
// favor a user’s custom template over the default template | ||
if (config.template) { | ||
return h.view(config.template, errorResponse).code(statusCode) | ||
} | ||
// user wants to use Youch over the default template | ||
if (config.useYouch) { | ||
// render Youch HTML template | ||
const html = await youch.toHTML() | ||
return h | ||
.response(html) | ||
.type('text/html') | ||
.view(config.template, errorResponse) | ||
.code(statusCode) | ||
} | ||
// prepare the error template and replace `%placeholder%` with error specific details | ||
const html = errorTemplate.replace(/%(\w+)%/g, (full, token) => { | ||
return errorResponse[token] || '' | ||
}) | ||
// render Youch HTML template | ||
const html = await youch.toHTML() | ||
// go with the default template, because no user template is defined and Youch isn’t selected | ||
return h | ||
@@ -157,3 +146,3 @@ .response(html) | ||
// go ahead with the response, no developer error detected | ||
// no developer error, go ahead with the response | ||
return h.continue | ||
@@ -165,3 +154,4 @@ }) | ||
register, | ||
pkg: require('../package.json') | ||
pkg: require('../package.json'), | ||
once: true | ||
} |
{ | ||
"name": "hapi-dev-errors", | ||
"description": "Return better error details and skip the look at command line to catch the issue.", | ||
"version": "2.3.0", | ||
"version": "3.0.0", | ||
"author": "Future Studio <info@futurestud.io>", | ||
@@ -6,0 +6,0 @@ "bugs": { |
@@ -22,18 +22,10 @@ <p align="center"> | ||
## Introduction | ||
A hapi plugin to return an error view for web requests, providing more details of the issue. Also, provides the | ||
error stacktrace within the browser and you can skip the extra look at your command line to catch the issue. | ||
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 default error view](media/hapi-dev-errors-default-view.png) | ||
![hapi-dev-errors default error Youch error view](media/hapi-dev-errors-default-youch-view.png) | ||
You can choose [Youch](https://github.com/poppinss/youch) to handle your error reporting by using the `useYouch: true` | ||
[option](https://github.com/fs-opensource/hapi-dev-errors#plugin-registration-options). `hapi-dev-errors` integrates | ||
seamlessly with Youch and delegates the error handling, if activated. The view will look like this: | ||
`hapi-dev-errors` seamlessly integrates [Youch](https://github.com/poppinss/youch) to show the error details. | ||
![hapi-dev-errors Youch error view](media/hapi-dev-errors-useYouch-view.png) | ||
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](https://github.com/fs-opensource/hapi-dev-errors#plugin-registration-options). | ||
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. | ||
To disable the terminal error, use the [`toTerminal: false` option](https://github.com/fs-opensource/hapi-dev-errors#plugin-registration-options). | ||
![hapi-dev-errors pretty terminal error](media/hapi-dev-errors-on-terminal.png) | ||
@@ -98,3 +90,2 @@ | ||
- **showErrors**: `(boolean)`, default: `false` — by default, the plugin is disabled and keeps hapi's default error handling behavior | ||
- **useYouch**: `(boolean)`, default: `false` — use [Youch](https://github.com/poppinss/youch) to handle and display the error instead of using `hapi-dev-error`’s default handling | ||
- **template**: `(string)`, no default — provide the template name that you want to render with `h.view(template, errorData)` | ||
@@ -101,0 +92,0 @@ - **toTerminal**: `(boolean)`, default: `true` — print pretty errors to the terminal as well (enabled by default) |
@@ -47,3 +47,3 @@ 'use strict' | ||
expect(response.statusCode).to.equal(500) | ||
expect(payload).to.startWith('{"') | ||
expect(payload).to.startWith('{') | ||
}) | ||
@@ -50,0 +50,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
11
210408
20
569
154