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

Catch Handlebars render errors #147

Merged
merged 3 commits into from
Feb 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
30 changes: 19 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,28 +340,36 @@ function fastifyView (fastify, opts, next) {
}

if (prod) {
const html = template(data)
if (!this.getHeader('content-type')) {
this.header('Content-Type', 'text/html; charset=' + charset)
try {
const html = template(data)
if (!this.getHeader('content-type')) {
this.header('Content-Type', 'text/html; charset=' + charset)
}
this.send(html)
} catch (e) {
this.send(e)
}
this.send(html)
} else {
getPartials(type, options.partials || {}, (err, partialsObject) => {
if (err) {
this.send(err)
return
}

Object.keys(partialsObject).forEach((name) => {
engine.registerPartial(name, engine.compile(partialsObject[name]))
})
try {
Object.keys(partialsObject).forEach((name) => {
engine.registerPartial(name, engine.compile(partialsObject[name]))
})

const html = template(data)
const html = template(data)

if (!this.getHeader('content-type')) {
this.header('Content-Type', 'text/html; charset=' + charset)
if (!this.getHeader('content-type')) {
this.header('Content-Type', 'text/html; charset=' + charset)
}
this.send(html)
} catch (e) {
this.send(e)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is going to return a JSON, there is not a handlebar feature like a 500 html service page?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK there isn't. The error can be caught in an error handler and you can display a 500 page there.

}
this.send(html)
})
}
})
Expand Down
1 change: 1 addition & 0 deletions templates/error.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{error}}
mohd-akram marked this conversation as resolved.
Show resolved Hide resolved
mohd-akram marked this conversation as resolved.
Show resolved Hide resolved
19 changes: 19 additions & 0 deletions test/test-handlebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,25 @@ test('fastify.view with handlebars engine', t => {
})
})

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

handlebars.registerHelper('error', () => { throw new Error('kaboom') })
mohd-akram marked this conversation as resolved.
Show resolved Hide resolved

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

fastify.ready(err => {
t.error(err)
t.rejects(fastify.view('./templates/error.hbs'), /kaboom/)
mohd-akram marked this conversation as resolved.
Show resolved Hide resolved
})
})

test('fastify.view for handlebars without data-parameter but defaultContext', t => {
t.plan(2)
const fastify = Fastify()
Expand Down