Skip to content

Commit

Permalink
Fix hbs with layout not throwing errors correctly (#363)
Browse files Browse the repository at this point in the history
  • Loading branch information
MellowMelon authored Jan 26, 2023
1 parent 441400f commit 9c6ed6d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,8 @@ function fastifyView (fastify, opts, next) {
header: () => { },
send: (result) => {
if (result instanceof Error) {
throw result
that.send(result)
return
}

data = Object.assign((data || {}), { body: result })
Expand Down
28 changes: 28 additions & 0 deletions test/test-handlebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,34 @@ test('reply.view with handlebars engine catches render error', t => {
})
})

test('reply.view with handlebars engine and layout catches render error', t => {
t.plan(3)
const fastify = Fastify()
const handlebars = require('handlebars')

handlebars.registerHelper('badHelper', () => { throw new Error('kaboom') })

fastify.register(require('../index'), {
engine: {
handlebars
},
layout: './templates/layout.hbs'
})

fastify.get('/', (req, reply) => {
reply.view('./templates/error.hbs')
})

fastify.inject({
method: 'GET',
url: '/'
}, (err, res) => {
t.error(err)
t.equal(JSON.parse(res.body).message, 'kaboom')
t.equal(res.statusCode, 500)
})
})

test('reply.view with handlebars engine and defaultContext', t => {
t.plan(6)
const fastify = Fastify()
Expand Down

0 comments on commit 9c6ed6d

Please sign in to comment.