Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix hbs with layout not throwing errors correctly #363

Merged
merged 1 commit into from
Jan 26, 2023
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
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