question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Trigger a 404 from within view

See original GitHub issue

Is there a way to pass an error out of a view to be handled by the default handlers in Keystone? What I was hoping to do was pass along a 404 if a query returned no results. Passing an error to next() doesn’t stop the view from rendering.

var keystone = require('keystone');

exports = module.exports = function(req, res) {

var view = new keystone.View(req, res),
locals = res.locals;

// locals.section is used to set the currently selected
// item in the header navigation.
locals.section = 'serviceareas';

view.on('init', function(next){

    var q =  keystone.list('ServiceArea').model.find().sort('sortOrder');
    q.exec(function(err, result){
        if(err) next(err);
        if(result.length === 0){
            var notfound = new Error();
            notfound.status = 404;
            next(notfound); // any error I pass to next() doesn't stop the render call
        } else {
            locals.serviceareas = result;
            next();
        }

    });
})

// Render the view
view.render('index');

};

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
albertogasparincommented, Apr 1, 2015

I think this is what you are looking for:

if (result.length === 0) {
  return res.status(404).send(keystone.wrapHTMLError('Sorry, no page could be found at this address (404)'));
}

keystone.wrapHTMLError is a method that generates a string of the default error page. In addition, you can pass any number to status() and any string as send argument. I prefer a “Not found” string and then let nginx do the error page rendering.

On a final note, the res object is quite powerful. You can also handle dynamic redirects like:

if (result.length === 0) {
  return res.status(301).redirect('/');
}
0reactions
gautamsicommented, Apr 14, 2019

Keystone 4 is going in maintenance mode. Expect no major change. see #4913 for details.

@albertogasparin’s comment is the best solution/workaround for v4.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Trigger 404 in Spring-MVC controller? - java - Stack Overflow
How do I get a Spring 3.0 controller to trigger a 404? I have a controller with @RequestMapping(value = "/**", method = RequestMethod....
Read more >
Build a trigger to collect custom metrics for HTTP 404 errors
Click the System Settings icon , and then click Triggers. · Click Create. · Type a name for the trigger. For this walkthrough,...
Read more >
Track and View 404 Errors with Google Analytics 4
Learn how to track 404 errors with Google Analytics. In this blog post I have covered three options, one of them includes Google...
Read more >
Error 404: 4 Ways to Fix It - Hostinger
Error 404 is a response code, meaning the server could not locate the requested content. Check this article to learn 4 steps to...
Read more >
Error 404 not found - What does it mean & how to fix it! - IONOS
The typical trigger for an error 404 message is when website content has been removed or moved to another URL. There are also...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found