Catch the Error "View Error" and execute 404

Currently, I am routing each page to my Controller pages that cannot be found in previous routes by including this line in routes.js:

this.match('/:page', { controller: 'pages', action: 'show' });

I had the idea that my PagesController descriptor would serve 404 if not found:

PagesController.show = function() {
    var page = this.param('page');
    if(page != undefined){
        page = page.replace(".html","");        
        try {
            this.render("./"+page);
        } catch(error){ //Failed to look up view -- not working at the moment =(
            this.redirect({action : "404"});
        };
    }

    return;
};

But my idea fails. An error cannot be detected, so the fatal signal will still be serviced. Should I add fn to the render call? What are the arguments? How it works? (/ simple questions).

+4
source share
1 answer

It might look something like this:

PagesController.show = function() {
  var self  = this;
  var page  = this.param('page');

  if (page !== undefined) {
    page = page.replace('.html', '');
    this.render("./" + page, function(err, html) {
      if (! err)
        return self.res.send(html);
      self.redirect({ action : '404' });
    });
  } else {
    // this will probably never be called, because `page`
    // won't be undefined, but still...
    this.redirect({ action : '404' });
  }
};
+6
source

All Articles