Skip to content

Commit

Permalink
Make SyntaxError include the line number in the message (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina authored Sep 25, 2018
1 parent 0fe3a14 commit 6c75958
Show file tree
Hide file tree
Showing 18 changed files with 47 additions and 3 deletions.
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ module.exports = function (fastify, opts, next) {
fastify.register(plugin, pluginOptions)
}
} catch (err) {
// Hack SyntaxError message so that we provide
// the line number to the user, otherwise they
// will be left in the cold.
if (err instanceof SyntaxError) {
err.message += ' at ' + err.stack.split('\n')[0]
}
next(err)
return
}
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Require all plugins in a directory",
"main": "index.js",
"scripts": {
"test": "standard | snazzy && tap test.js"
"test": "standard | snazzy && tap test/*.js"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -36,5 +36,10 @@
},
"dependencies": {
"steed": "^1.1.3"
},
"standard": {
"ignore": [
"test/error/lib/a.js"
]
}
}
2 changes: 1 addition & 1 deletion test.js → test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ t.plan(29)

const app = Fastify()

app.register(require('./fixtures/app'))
app.register(require('./basic/app'))

app.ready(function (err) {
t.error(err)
Expand Down
2 changes: 1 addition & 1 deletion fixtures/app.js → test/basic/app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const path = require('path')
const AutoLoad = require('..')
const AutoLoad = require('../..')

module.exports = function (fastify, opts, next) {
fastify.register(AutoLoad, {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
15 changes: 15 additions & 0 deletions test/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict'

const t = require('tap')
const Fastify = require('fastify')

t.plan(2)

const app = Fastify()

app.register(require('./error/app'))

app.ready(function (err) {
t.type(err, SyntaxError)
t.match(err.message, /Unexpected token \} at .*\/test\/error\/lib\/a.js:6/)
})
12 changes: 12 additions & 0 deletions test/error/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict'

const path = require('path')
const AutoLoad = require('../..')

module.exports = function (fastify, opts, next) {
fastify.register(AutoLoad, {
dir: path.join(__dirname, 'lib')
})

next()
}
6 changes: 6 additions & 0 deletions test/error/lib/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict'

module.exports = function (app, opts, next) {
// this has a syntax error on purpose
[
}

0 comments on commit 6c75958

Please sign in to comment.