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

generateView at 'onRequest' extension point #1137

Closed
joliss opened this issue Nov 10, 2013 · 5 comments
Closed

generateView at 'onRequest' extension point #1137

joliss opened this issue Nov 10, 2013 · 5 comments
Assignees
Labels
bug Bug or defect
Milestone

Comments

@joliss
Copy link

joliss commented Nov 10, 2013

I'm trying to use request.generateView to generate a custom error page in an 'onRequest' extension function, like so:

server.ext('onRequest', function (request, next) {
  if (errorCondition)
    next(request.generateView('error', {message: 'Foo'}))
  else
    next()
})

request.generateView fails in request.js:716 with

Debug: hapi, uncaught, onRequest, error TypeError: Cannot read property 'env' of null
    at internals.Request.generateView (.../node_modules/hapi/lib/request.js:716:35)

Changing request.js:716 to

    var viewsManager = (this._route && this._route.env.views) || this.server._views;

in order to guard against missing this._route seems to fix it, but I don't know if that's the correct fix. I'm not sure if calling request.generateView at that point is supposed to be supported at all.

@joliss
Copy link
Author

joliss commented Nov 10, 2013

My current workaround is to use

new hapi.response.View(request.server._views, 'templateName', context)

in place of request.generateResponse.

@hueniverse
Copy link
Contributor

this._route should never be null. I'll take a look.

@ghost ghost assigned hueniverse Nov 11, 2013
@hueniverse
Copy link
Contributor

Ok. The problem is that the route is not matched until right after onRequest, hence the bug. I need to map the flows to figure out the right solution here, but it will need to take into account the environment of the plugin, not just the server (if a plugin is in play).

@joliss
Copy link
Author

joliss commented Nov 12, 2013

Lovely, thanks Eran!

P.S. I attended your session at NodeConf, and now I'm using hapi in this build tool I'm writing. It's amazing!

@hueniverse
Copy link
Contributor

Sweet!

jmonster pushed a commit to jmonster/hapi that referenced this issue Feb 10, 2014
@lock lock bot locked as resolved and limited conversation to collaborators Jan 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Bug or defect
Projects
None yet
Development

No branches or pull requests

2 participants