hapi-dev-errors
Advanced tools
Comparing version 2.2.0 to 2.3.0
# Changelog | ||
## Version 2.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) | ||
- `update` minor code reformats and restructuring | ||
## Version 2.2.0 (2018-06-02) | ||
@@ -4,0 +9,0 @@ - `add` JSON response for CLI requests (cURL, Postman, wget) (thank you @pi0) |
@@ -7,5 +7,39 @@ 'use strict' | ||
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 | ||
* | ||
* @param {object} request - the request object | ||
* @param {object} error - object with error details | ||
*/ | ||
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 | ||
// assign httpVersion -> same as with request.url | ||
req.httpVersion = req.raw.req.httpVersion | ||
// let Youch show the error’s status code | ||
error.status = error.output.statusCode | ||
// create new Youch instance and print a pretty error to the console | ||
return new Youch(error, request) | ||
} | ||
/** | ||
* helper function to test if string matches a value | ||
*/ | ||
function matches (str, regex) { | ||
return str && str.match(regex) | ||
} | ||
/** | ||
* Render better error views during development | ||
@@ -16,10 +50,11 @@ * | ||
*/ | ||
function register (server, options) { | ||
async function register (server, options) { | ||
// default option values | ||
const defaults = { | ||
showErrors: false, | ||
useYouch: false | ||
useYouch: false, | ||
toTerminal: true | ||
} | ||
// assign config object from merged options into defaults | ||
// merge user-defined plugin options into defaults | ||
const config = Object.assign(defaults, options) | ||
@@ -29,2 +64,3 @@ | ||
// no need to read the template or hook extension point | ||
// do this in production! | ||
if (!config.showErrors) { | ||
@@ -40,7 +76,4 @@ return | ||
// read and keep the default error template | ||
const errorTemplate = Fs.readFileSync(TemplatePath, 'utf8') | ||
const errorTemplate = await ReadFile(TemplatePath, 'utf8') | ||
// helper function to test if string matches a value | ||
const matches = (str, regex) => str && str.match(regex) | ||
// extend the request lifecycle at `onPreResponse` | ||
@@ -69,2 +102,10 @@ // to change the default error handling behavior (if enabled) | ||
const youch = createYouch(request, error) | ||
// print a pretty error to terminal as well | ||
if (config.toTerminal) { | ||
const json = await youch.toJSON() | ||
console.log(ForTerminal(json)) | ||
} | ||
// take priority: check header if request is in CLI | ||
@@ -92,18 +133,3 @@ if (matches(agent, /curl|wget|postman/i)) { | ||
if (config.useYouch) { | ||
// clone the request to not change it | ||
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 | ||
// assign httpVersion -> same as with request.url | ||
req.httpVersion = req.raw.req.httpVersion | ||
// let Youch show the error’s status code | ||
error.status = statusCode | ||
// create new Youch instance and reply rendered HTML | ||
const youch = new Youch(error, req) | ||
// render response HTML template | ||
// render Youch HTML template | ||
const html = await youch.toHTML() | ||
@@ -110,0 +136,0 @@ |
{ | ||
"name": "hapi-dev-errors", | ||
"description": "Return better error details and skip the look at command line to catch the issue.", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"author": "Future Studio <info@futurestud.io>", | ||
@@ -11,16 +11,19 @@ "bugs": { | ||
"hoek": "~5.0.3", | ||
"youch": "~2.0.7" | ||
"youch": "~2.0.8", | ||
"youch-terminal": "~1.0.0" | ||
}, | ||
"devDependencies": { | ||
"boom": "~7.1.1", | ||
"boom": "~7.2.0", | ||
"code": "~5.2.0", | ||
"eslint-config-standard": "~10.2.1", | ||
"eslint-plugin-import": "~2.8.0", | ||
"eslint-plugin-node": "~5.2.1", | ||
"eslint-plugin-promise": "~3.6.0", | ||
"eslint-plugin-standard": "~3.0.1", | ||
"hapi": "~17.2.0", | ||
"eslint": "~4.19.1", | ||
"eslint-config-standard": "~11.0.0", | ||
"eslint-plugin-import": "~2.13.0", | ||
"eslint-plugin-node": "~6.0.1", | ||
"eslint-plugin-promise": "~3.8.0", | ||
"eslint-plugin-standard": "~3.1.0", | ||
"hapi": "~17.5.2", | ||
"husky": "~0.14.3", | ||
"lab": "~15.2.2", | ||
"vision": "~5.3.1" | ||
"lab": "~15.5.0", | ||
"sinon": "~6.1.3", | ||
"vision": "~5.3.3" | ||
}, | ||
@@ -32,8 +35,9 @@ "engines": { | ||
"keywords": [ | ||
"developer", | ||
"hapi", | ||
"hapijs", | ||
"plugin", | ||
"error", | ||
"errors", | ||
"hapi", | ||
"hapijs", | ||
"plugin" | ||
"debugging", | ||
"developer" | ||
], | ||
@@ -40,0 +44,0 @@ "license": "MIT", |
@@ -33,3 +33,10 @@ <p align="center"> | ||
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) | ||
## Requirements | ||
@@ -43,7 +50,7 @@ This plugin uses async/await which requires **Node.js v8 or newer**. | ||
```bash | ||
# NPM 5: this way is yours | ||
npm i hapi-dev-errors | ||
# NPM 4: | ||
npm i -S hapi-dev-errors | ||
# you’re using NPM shortcuts to (i)nstall and (-S)ave the module as a dependency | ||
# NPM v5 users, this way is yours | ||
npm i hapi-dev-errors | ||
``` | ||
@@ -56,7 +63,7 @@ | ||
```bash | ||
# 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 | ||
# you’re using NPM shortcuts to (i)nstall and (-S)ave the module as a dependency | ||
# NPM v5 users, this way is yours | ||
npm i hapi-dev-errors@1.3.2 | ||
``` | ||
@@ -95,2 +102,3 @@ | ||
- **template**: `(string)`, no default — provide the template name that you want to render with `h.view(template, errorData)` | ||
- **toTerminal**: `(boolean)`, default: `true` — print pretty errors to the terminal as well (enabled by default) | ||
@@ -102,3 +110,4 @@ ```js | ||
showErrors: process.env.NODE_ENV !== 'production', | ||
template: 'my-error-view' | ||
template: 'my-error-view', | ||
toTerminal: true | ||
} | ||
@@ -110,2 +119,3 @@ }) | ||
## Provided Values for Your Custom Error View | ||
@@ -136,5 +146,7 @@ `hapi-dev-errors` supports the `template` option while registering the plugin. Provide a template name to | ||
- [hapi tutorial series](https://futurestud.io/tutorials/hapi-get-your-server-up-and-running) with 80+ tutorials | ||
- [hapi tutorial series](https://futurestud.io/tutorials/hapi-get-your-server-up-and-running) with 90+ tutorials | ||
- [Youch](https://github.com/poppinss/youch) - Pretty error reporting for Node.js | ||
- [Youch terminal](https://github.com/poppinss/youch-terminal) - Pretty error reporting on your terminal | ||
## Contributing | ||
@@ -141,0 +153,0 @@ |
@@ -20,3 +20,4 @@ 'use strict' | ||
options: { | ||
showErrors: true | ||
showErrors: true, | ||
toTerminal: false | ||
} | ||
@@ -23,0 +24,0 @@ }) |
@@ -14,3 +14,3 @@ 'use strict' | ||
// fake production env | ||
process.env.NODE_ENV = 'prod' | ||
process.env.NODE_ENV = 'production' | ||
@@ -20,3 +20,3 @@ await server.register({ | ||
options: { | ||
showErrors: process.env.NODE_ENV !== 'prod' | ||
showErrors: process.env.NODE_ENV !== 'production' | ||
} | ||
@@ -23,0 +23,0 @@ }) |
@@ -17,8 +17,10 @@ 'use strict' | ||
server = new Hapi.Server() | ||
// fake dev env | ||
process.env.NODE_ENV = 'development' | ||
// fake dev env, no process.env.NODE_ENV defined | ||
await server.register({ | ||
plugin: require('../lib/index'), | ||
options: { | ||
showErrors: process.env.NODE_ENV !== 'production' | ||
showErrors: process.env.NODE_ENV !== 'production', | ||
toTerminal: false | ||
} | ||
@@ -148,10 +150,11 @@ }) | ||
{ | ||
plugin: require('vision') | ||
}, | ||
{ | ||
plugin: require('../lib/index'), | ||
options: { | ||
showErrors: process.env.NODE_ENV !== 'production', | ||
template: 'error' | ||
template: 'error', | ||
toTerminal: false | ||
} | ||
}, | ||
{ | ||
plugin: require('vision') | ||
} | ||
@@ -158,0 +161,0 @@ ]) |
@@ -21,3 +21,4 @@ 'use strict' | ||
showErrors: process.env.NODE_ENV !== 'production', | ||
useYouch: true | ||
useYouch: true, | ||
toTerminal: false | ||
} | ||
@@ -24,0 +25,0 @@ }) |
Sorry, the diff of this file is not supported yet
311423
24
647
163
3
13
14
+ Addedyouch-terminal@~1.0.0
+ Addedkleur@4.1.5(transitive)
+ Addedyouch-terminal@1.0.1(transitive)
Updatedyouch@~2.0.8