Error page administrable

Enonic version: 6.4.1
OS: Windows 10

I am doing a redirect from the error.js file like this:

exports.handle404 = function (err) {
var body = thymeleaf.render(view404, {urlredirect: portal.pageUrl({ path: portal.getSite()._path + ‘/error/pagina404’}) });
return {
contentType: ‘text/html’,
body: body
}
};

I did this redirect because I wanna managed the content of this error page. How could I managed the content of this error page without a redirect?

Hi Diego,

By dropping the redirectUrl statement and just use a view directly. Placing a 404.html in your error-folder and using that with Thymeleaf directly in your errorHandler404 function.

If the user needs to be able to control the text on that page, perhaps that page should be placed inside Admin as content and be fetched when rendering response to the error.

A small comment to this - Ideally you would make your error page as lean as possible, and avoid depending on other functionality. I.e. what if the repository is currently failing and you try to fetch content from it in order to display the error page caused by this failure in the first place?

Error pages should normally not show up, and preferably be 100% based on raw assets and the code in the error handler. If you need text there, simply add it in your application code.

1 Like

@tsi - is it not possible to try to fetch data from the repository, and if it fails, fall back to some hard coded texts?

I guess, but would require that you use try clauses at least!

I think is better manage the page like @tsi tells us

I am redirecting to a specific page if exports.handle404 happens


exports.handle404 = function (err) {

var redirectPageUrl = portal.pageUrl({id: "bf7c96b4-10bf-48c8-ab34-8effe0601bfa"}); //default

return {
redirect: redirectPageUrl
}
}

Is it possible to set the status to 404 when redirecting to this page ?

You cannot redirect and set a 404 status at the same time. This is because an HTTP redirect is a response with status 3xx, plus the response header “Location” with the target URL to redirect to.

What you could do is redirect to the page you want and pass an extra parameter indicating that the page controller should set 404 in the response.

exports.handle404 = function (err) {
    var redirectPageUrl = portal.pageUrl({
        id: "bf7c96b4-10bf-48c8-ab34-8effe0601bfa",
        params: {
            'notFound': true
        }
    });
    return {
        redirect: redirectPageUrl
    }
}

And in the page controller where you redirect, check if req.params.notFound == ‘true’, and if so set status 404.

2 Likes

Thx for your reply. I see, I was taking the wrong approach to this.

I used HttpClient request to get the page I want from admin and then rendered it. It all works now.