Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More β†’
Socket
Sign inDemoInstall
Socket

hapi-dev-errors

Package Overview
Dependencies
Maintainers
2
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hapi-dev-errors - npm Package Compare versions

Comparing version 3.0.1 to 3.1.0

test/plugin-handles-failed-response-validations.js

6

CHANGELOG.md
# Changelog
## Version [3.1.0](https://github.com/fs-opensource/hapi-dev-errors/compare/v3.0.1...v3.1.0) (2018-09-28)
- `add` pass `request` to custom view: this allows you to access every request property
- `add` pass `error` to custom view: this allows you to access every response property
- `add` tests to verify plugin functionality for failed response validations (thank you [venikman](https://github.com/fs-opensource/hapi-dev-errors/pull/6))
- `update` dependencies
## Version [3.0.1](https://github.com/fs-opensource/hapi-dev-errors/compare/v3.0.0...v3.0.1) (2018-08-21)

@@ -4,0 +10,0 @@ - `update` readme: quick navigation and logo size fix for small screens

50

lib/index.js

@@ -9,7 +9,9 @@ 'use strict'

* Create a Youch instance for pretty error printing.
* This instance is used to format output for the console
* and for a web view.
* This instance is used to format output for the
* console and for a web view.
*
* @param {object} request - the request object
* @param {object} error - object with error details
* @param {Object} request - the request object
* @param {Object} error - object with error details
*
* @returns {Object}
*/

@@ -27,3 +29,3 @@ function createYouch (request, error) {

// create new Youch instance and print a pretty error to the console
// pretty error printing on terminal or web view
return new Youch(error, request)

@@ -34,8 +36,10 @@ }

* 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.
* This is true for requests where the "accept" header
* contains "json" or the agent is a CLI/GUI app.
*
* @param {object}
* @param {Object}
*
* @returns {Boolean}
*/
function isJsonRequest ({ agent, accept }) {
function wantsJson ({ agent, accept }) {
return matches(agent, /curl|wget|postman|insomnia/i) || matches(accept, /json/)

@@ -45,3 +49,9 @@ }

/**
* Helper function to test if string matches a value
* Helper function to test whether a given
* string matches a RegEx.
*
* @param {String} str
* @param {String} regex
*
* @returns {Boolean}
*/

@@ -53,9 +63,8 @@ function matches (str, regex) {

/**
* Render better error views during development
* Render better error views during development.
*
* @param {*object} server - hapi server instance where the plugin is registered
* @param {*object} options - plugin options
* @param {Object} server - hapi server instance where the plugin is registered
* @param {Object} options - plugin options
*/
async function register (server, options) {
// default option values
const defaults = {

@@ -66,8 +75,8 @@ showErrors: false,

// merge user-defined plugin options into defaults
const config = Object.assign({}, defaults, options)
// cut early if `showErrors` is false
// no need to read the template or hook extension point
// do this in production!
/**
* Cut early if `showErrors` is false. No need to
* hook the extension point in production.
*/
if (!config.showErrors) {

@@ -85,3 +94,2 @@ return

server.ext('onPreResponse', async (request, h) => {
// error response shortcut
const error = Hoek.clone(request.response)

@@ -117,3 +125,3 @@

// - check "accept" header for JSON request
if (isJsonRequest({ accept, agent })) {
if (wantsJson({ accept, agent })) {
const details = Object.assign({}, errorResponse, {

@@ -133,3 +141,3 @@ stacktrace: errorResponse.stacktrace.split('\n').map(line => line.trim())

return h
.view(config.template, errorResponse)
.view(config.template, { request, error, ...errorResponse })
.code(statusCode)

@@ -136,0 +144,0 @@ }

{
"name": "hapi-dev-errors",
"description": "Return better error details and skip the look at command line to catch the issue.",
"version": "3.0.1",
"version": "3.1.0",
"author": "Future Studio <info@futurestud.io>",

@@ -23,7 +23,8 @@ "bugs": {

"eslint-plugin-standard": "~3.1.0",
"hapi": "~17.5.2",
"husky": "~0.14.3",
"hapi": "~17.6.0",
"husky": "~1.0.1",
"joi": "~13.6.0",
"lab": "~15.5.0",
"sinon": "~6.1.3",
"vision": "~5.3.3"
"sinon": "~6.3.4",
"vision": "~5.4.0"
},

@@ -30,0 +31,0 @@ "engines": {

@@ -128,2 +128,4 @@ <div align="center">

- `request`: the request that caused the error
- `error`: the error response with all its properties
- `title`: error title like `Internal Server Error`

@@ -130,0 +132,0 @@ - `statusCode`: HTTP response status code (always 500)

@@ -10,3 +10,2 @@ 'use strict'

const { experiment, test, before } = (exports.lab = Lab.script())
const expect = Code.expect

@@ -47,4 +46,4 @@ experiment('hapi-dev-error falls back to json', () => {

expect(response.statusCode).to.equal(500)
expect(payload).to.startWith('{')
Code.expect(response.statusCode).to.equal(500)
Code.expect(payload).to.startWith('{')
})

@@ -62,5 +61,5 @@

expect(response.statusCode).to.equal(500)
expect(payload).to.startWith('{')
Code.expect(response.statusCode).to.equal(500)
Code.expect(payload).to.startWith('{')
})
})

@@ -30,3 +30,3 @@ 'use strict'

const options = {
const request = {
url: routeOptions.path,

@@ -36,3 +36,3 @@ method: routeOptions.method

const response = await server.inject(options)
const response = await server.inject(request)
const payload = JSON.parse(response.payload || '{}')

@@ -39,0 +39,0 @@

@@ -35,3 +35,3 @@ 'use strict'

const options = {
const request = {
url: routeOptions.path,

@@ -41,3 +41,3 @@ method: routeOptions.method

const response = await server.inject(options)
const response = await server.inject(request)
const payload = JSON.parse(response.payload || '{}')

@@ -44,0 +44,0 @@

@@ -40,3 +40,3 @@ 'use strict'

const options = {
const request = {
url: routeOptions.path,

@@ -46,3 +46,3 @@ method: routeOptions.method

const response = await server.inject(options)
const response = await server.inject(request)
const payload = response.payload

@@ -65,3 +65,3 @@

const options = {
const request = {
url: routeOptions.path,

@@ -74,3 +74,3 @@ method: routeOptions.method,

const response = await server.inject(options)
const response = await server.inject(request)
const payload = JSON.parse(response.payload || '{}')

@@ -95,3 +95,3 @@

const options = {
const request = {
url: routeOptions.path,

@@ -104,3 +104,3 @@ method: routeOptions.method,

const response = await server.inject(options)
const response = await server.inject(request)
const payload = JSON.parse(response.payload || '{}')

@@ -126,3 +126,3 @@

const requestPayload = { name: 'Future Studio' }
const options = {
const request = {
url: routeOptions.path,

@@ -136,3 +136,3 @@ method: routeOptions.method,

const response = await server.inject(options)
const response = await server.inject(request)
const payload = JSON.parse(response.payload || '{}')

@@ -189,3 +189,3 @@

const options = {
const request = {
url: routeOptions.path,

@@ -195,3 +195,3 @@ method: routeOptions.method

const response = await server.inject(options)
const response = await server.inject(request)
const payload = response.payload

@@ -198,0 +198,0 @@

@@ -36,3 +36,3 @@ 'use strict'

const options = {
const request = {
url: routeOptions.path,

@@ -42,3 +42,3 @@ method: routeOptions.method

const response = await server.inject(options)
const response = await server.inject(request)
const payload = response.payload

@@ -45,0 +45,0 @@

@@ -11,3 +11,2 @@ 'use strict'

const { experiment, test, before } = (exports.lab = Lab.script())
const expect = Code.expect

@@ -49,3 +48,3 @@ experiment('hapi-dev-error register plugin', () => {

const response = await server.inject(request)
expect(response.statusCode).to.equal(500)
Code.expect(response.statusCode).to.equal(500)

@@ -52,0 +51,0 @@ Sinon.assert.called(console.log)

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚑️ by Socket Inc