Trigger a 404 from within view
See original GitHub issueIs 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:
- Created 8 years ago
- Comments:13 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I think this is what you are looking for:
keystone.wrapHTMLError
is a method that generates a string of the default error page. In addition, you can pass any number tostatus()
and any string assend
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: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.