Throw 404 from a part

Enonic version: 6.4.1
OS: OSX

Is it possible to throw a 404 from a part?

    var content = (req.params.id) ? contentLib.get({ key: req.params.id }) : null;

    if (!content) {
        return {
            status: 404
        }
    }

This will only render the page with no content where the part should have been. I would rather like to show our global 404-page.

1 Like

Hi,

Add the redirect param in the return statement =) With it you could redirect to the global page (fetch from settings in site.xml or from part or where ever you have it).

Hm, this doesn’t seem to work. I get the same result. That is, the page with an empty part without the part.

    if (!content) {
        return {
            redirect: "/some-page"
        }
    }

Sorry, I tricked you a little. Parts can’t redirect, only pages. Unless the part is handling the request as a post … I think.

Why should the part throw the 404? You could print “Article not available” or something too?

Ah, thanks for clarifying! Sounds good. Thanks for your help.

Did you get your error handling to work in the end?

Yes. As you suggested we just print “Article not found” instead of showing our generic 404.
You can close this issue.

1 Like