Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
},
"extends": "standard",
"parserOptions": {
"ecmaVersion": 8
"ecmaVersion": 2018
}
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

------

[![Greenkeeper badge](https://badges.greenkeeper.io/fs-opensource/hapi-dev-errors.svg)](https://greenkeeper.io/)

<p align="center"><sup>Development of this hapi plugin is supported by <a href="https://futurestud.io">Future Studio University 🚀</a></sup>
<br><b>
Join the <a href="https://futurestud.io/university">Future Studio University and Skyrocket in Node.js</a></b>
Expand Down
19 changes: 1 addition & 18 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,31 +116,19 @@ async function register (server, options) {
}

const config = Object.assign({}, defaults, options)
config.links = Array.isArray(config.links) ? config.links : [config.links]

/**
* Cut early if `showErrors` is false. No need to
* hook the extension point in production.
*/
if (!config.showErrors) {
return
}

// require `vision` plugin in case the user provides an error template
if (config.template) {
server.dependency(['vision'])
}

// Make sure the `links` are an array
if (!Array.isArray(config.links)) {
config.links = [config.links]
}

// extend the request lifecycle at `onPreResponse`
// to change the default error handling behavior (if enabled)
server.ext('onPreResponse', async (request, h) => {
const error = request.response

// only show `bad implementation` developer errors (status code 500)
if (error.isBoom && error.output.statusCode === 500) {
const accept = request.raw.req.headers.accept
const agent = request.raw.req.headers['user-agent']
Expand All @@ -165,9 +153,6 @@ async function register (server, options) {
console.log(ForTerminal(json))
}

// take priority:
// - check "agent" header for REST request (cURL, Postman & Co.)
// - check "accept" header for JSON request
if (wantsJson({ accept, agent })) {
const details = Object.assign({}, errorResponse, {
stacktrace: errorResponse.stacktrace.split('\n').map(line => line.trim())
Expand All @@ -187,7 +172,6 @@ async function register (server, options) {
.code(statusCode)
}

// render Youch HTML template
const html = await youch.toHTML()

return h
Expand All @@ -196,7 +180,6 @@ async function register (server, options) {
.code(statusCode)
}

// no developer error, go ahead with the response
return h.continue
})
}
Expand Down
24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@
"youch-terminal": "~1.0.0"
},
"devDependencies": {
"boom": "~7.2.0",
"boom": "~7.3.0",
"code": "~5.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.6.0",
"husky": "~1.1.1",
"joi": "~13.7.0",
"lab": "~15.5.0",
"sinon": "~6.3.5",
"eslint": "~5.9.0",
"eslint-config-standard": "~12.0.0",
"eslint-plugin-import": "~2.14.0",
"eslint-plugin-node": "~8.0.0",
"eslint-plugin-promise": "~4.0.1",
"eslint-plugin-standard": "~4.0.0",
"hapi": "~17.8.1",
"husky": "~1.2.0",
"joi": "~14.3.0",
"lab": "~18.0.0",
"sinon": "~7.1.1",
"vision": "~5.4.0"
},
"engines": {
Expand Down
6 changes: 3 additions & 3 deletions test/plugin-falls-back-to-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const Hapi = require('hapi')

let server

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

experiment('hapi-dev-error falls back to json', () => {
before(async () => {
Expand All @@ -32,7 +32,7 @@ experiment('hapi-dev-error falls back to json', () => {
server.route(routeOptions)
})

test('test if the plugin responds json with json accept header', async () => {
it('responds json with json accept header', async () => {
const response = await server.inject({
url: '/error',
method: 'GET',
Expand All @@ -46,7 +46,7 @@ experiment('hapi-dev-error falls back to json', () => {
Code.expect(payload).to.startWith('{')
})

test('test if the plugin responds json with curl user-agent', async () => {
it('responds json with curl user-agent', async () => {
const response = await server.inject({
url: '/error',
method: 'GET',
Expand Down
12 changes: 6 additions & 6 deletions test/plugin-is-enabled-when-not-in-production.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const Path = require('path')

let server

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

experiment('hapi-dev-error register plugin', () => {
Expand All @@ -26,7 +26,7 @@ experiment('hapi-dev-error register plugin', () => {
})
})

test('test if the plugin is enabled in development for web requests', async () => {
it('is enabled in development for web requests', async () => {
const routeOptions = {
path: '/showErrorsForWeb',
method: 'GET',
Expand All @@ -49,7 +49,7 @@ experiment('hapi-dev-error register plugin', () => {
expect(payload).to.startWith('<!DOCTYPE html>')
})

test('test if the plugin is enabled in development for JSON/REST requests', async () => {
it('is enabled in development for JSON/REST requests', async () => {
const routeOptions = {
path: '/showErrorsForREST',
method: 'GET',
Expand Down Expand Up @@ -77,7 +77,7 @@ experiment('hapi-dev-error register plugin', () => {
expect(payload.method).to.equal(routeOptions.method)
})

test('test when the error is from a rejected Promise', async () => {
it('test when the error is from a rejected Promise', async () => {
const routeOptions = {
path: '/showPromiseError',
method: 'GET',
Expand Down Expand Up @@ -105,7 +105,7 @@ experiment('hapi-dev-error register plugin', () => {
expect(payload.method).to.equal(routeOptions.method)
})

test('test if the request payload is added to the error response', async () => {
it('test if the request payload is added to the error response', async () => {
const routeOptions = {
path: '/with-request-payload',
method: 'POST',
Expand Down Expand Up @@ -167,7 +167,7 @@ experiment('hapi-dev-error renders a custom template', () => {
})
})

test('render a template', async () => {
it('render a template', async () => {
const routeOptions = {
path: '/custom-view',
method: 'GET',
Expand Down
27 changes: 22 additions & 5 deletions test/plugin-skips-non-500-responses.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
const Lab = require('lab')
const Code = require('code')
const Hapi = require('hapi')
const Boom = require('boom')

let server

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

experiment('hapi-dev-error works only with 500 errors', () => {
Expand All @@ -22,13 +23,11 @@ experiment('hapi-dev-error works only with 500 errors', () => {
})
})

test('test if the plugin skips handling for non-error response', async () => {
it('skips handling for non-error response', async () => {
const routeOptions = {
path: '/ok',
method: 'GET',
handler: () => {
return 'ok'
}
handler: () => 'ok'
}

server.route(routeOptions)
Expand All @@ -44,4 +43,22 @@ experiment('hapi-dev-error works only with 500 errors', () => {
expect(response.statusCode).to.equal(200)
expect(payload).to.equal('ok')
})

it('skips handling for 404 errors', async () => {
const routeOptions = {
path: '/404',
method: 'GET',
handler: () => Boom.notFound()
}

server.route(routeOptions)

const request = {
url: routeOptions.path,
method: routeOptions.method
}

const response = await server.inject(request)
expect(response.statusCode).to.equal(404)
})
})
10 changes: 5 additions & 5 deletions test/plugin-uses-links.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const Code = require('code')
const Hapi = require('hapi')
const Sinon = require('sinon')

const { experiment, test, beforeEach, afterEach } = (exports.lab = Lab.script())
const { experiment, it, beforeEach, afterEach } = (exports.lab = Lab.script())

experiment('hapi-dev-error handles custom user links', () => {
async function createServer (options) {
Expand Down Expand Up @@ -39,7 +39,7 @@ experiment('hapi-dev-error handles custom user links', () => {
console.error.restore()
})

test('that the plugin works fine with empty links', async () => {
it('works fine with empty links', async () => {
const server = await createServer({ links: [] })

const response = await server.inject({
Expand All @@ -53,7 +53,7 @@ experiment('hapi-dev-error handles custom user links', () => {
Code.expect(response.payload).to.startWith('<')
})

test('that the plugin throws if the links are strings', async () => {
it('throws if the links are strings', async () => {
const server = await createServer({ links: [ 'error' ] })

const response = await server.inject({
Expand All @@ -68,7 +68,7 @@ experiment('hapi-dev-error handles custom user links', () => {
Code.expect(response.payload).to.include('Internal Server Error')
})

test('that the plugin throws if the links is not an array of functions', async () => {
it('throws if the links is not an array of functions', async () => {
const server = await createServer({ links: 'error' })

const response = await server.inject({
Expand All @@ -83,7 +83,7 @@ experiment('hapi-dev-error handles custom user links', () => {
Code.expect(response.payload).to.include('Internal Server Error')
})

test('that the plugin works fine with a link function', async () => {
it('works fine with a link function', async () => {
const server = await createServer({ links: () => `link` })

const response = await server.inject({
Expand Down