Custom error handler not working for extension less URL

vijayk87714775 12-09-2018

Hi, I'm using AEM version 6.2 and I've override the error handler and created the custom error handler. Inside the custom error handler, added the use of custom ErrorHandler model instead of using OOTB one.

Custom 404.html  code:

<html data-sly-use.responseStatus="com.sample.ResponseStatus">

<head>

<title>File not found</title>

</head>

<body>

    <p>A custom errorhandler for 404 responses</p>

</body>

</html>

When I hit the URL with resource that doesn't exist in AEM and URL contains .html extension then the control comes in custom 404.html and invokes the model which internally include the 404 error page and displays the result.

But, if I do hit the page without .html extension then it displays error message which is in the custom 404.html.
Not sure if control is actually coming to the custom 404.html 'cos if it comes then it should've invoke the model and displayed 404 page which it displays with .html extension.

Any suggestions?

Regards,
Vijay

Accepted Solutions (1)

Accepted Solutions (1)

Arun_Patidar
MVP
18-09-2018

Hi Vijay,

You are returning 404 resource which doesn't have html extension. add .html at the end and then check

Please do like below:

    <sly data-sly-resource="{responseStatus.pageNotFound}.html" />

Answers (8)

Answers (8)

Arun_Patidar
MVP
17-09-2018

Hi,

Vijay, instead of doing dispatcher.include(getRequest(), getResponse()), you can return the 404 page URL from the ResponseStatus class and you can include resource directly in Sightly using data-sly-resource or data-sly-include.

Arun_Patidar
MVP
14-09-2018

Hi,

I tried same and its look like when you include resource other than html, it is not including response in the page. I checked using filter as well but there is no request for 404 page if there is no extension.

The reason could be there is no script to render other than html e.g. my component.html

you may try to create servlet and read response and then include in the page, kind of indirect. or you can change your strategy and do redirect to actual 404 pages from 404.html.

vijayk87714775 18-09-2018

Hi Arun, As suggested, I've modifide the java code to get the resource path and then render it in 404.html using data-sly-resource. I'm getting the resource path from Error Model but I'm trying to render the in 404.html, it's only working for .html pages but not for extensionless pages. <!-- --!> Thanks, Vijay.

vijayk87714775 16-09-2018

Hi, Thanks for your response. We don't want to do the redirect to 404 page so can't go with that option. About the servlet part, I didn't get how it'll be triggered if we're going to hit the page without extension? If I write a servlet based on resource type 'sling/servlet/deafult' but then again, it's going to required to have extension to call it. How can I achieve this scenario? Could you please explain? Regards, Vijay

vijayk87714775 14-09-2018

Hi Arun , Thanks for responding . 404 page is getting triggered from the model. Below is the code snippet

public class ResponseStatus extends WCMUsePojo {

private static final String DIR_404_HTML = "/404";

private static final String CONTENT_SAMPLE = "/content/sample/";

private static final String AUTHOR = "author";

private static final String EN_US = "en-us";

private static final Pattern REGEX_LANGUAGE_SELECTOR = Pattern

.compile("(/content/myproject)?/([a-zA-Z]{2}-[a-zA-Z]{2})");

private static final Logger log = LoggerFactory.getLogger(ResponseStatus.class);

private static final int ERROR_CODE_404 = 404;

@Override

public void activate() throws Exception {

Set<String> runModes;

SlingSettingsService slingSettings = this.getSlingScriptHelper().getService(SlingSettingsService.class);

if (null != slingSettings) {

runModes = slingSettings.getRunModes();

getResponse().setStatus(ERROR_CODE_404);

if (!runModes.contains(AUTHOR)) {

String requestUrl = getRequest().getRequestURL().toString();

if (StringUtils.isNotEmpty(requestUrl)) {

String languageCode = setLanguageCode(requestUrl);

redirectToErrorPage(languageCode);

}

}

}

}

protected void redirectToErrorPage(String languageCode) throws IOException {

String redirectUrl = CONTENT_SAMPLE + languageCode + DIR_404_HTML;

ResourceResolver resolver = getRequest().getResourceResolver();

Resource targetResource = resolver.getResource(redirectUrl);

if (targetResource != null) {

try {

RequestDispatcher dispatcher = getRequest().getRequestDispatcher(targetResource);

if (dispatcher != null) {

dispatcher.include(getRequest(), getResponse());

}

} catch (ServletException e) {

log.error("Error while forwarding the requesting {}", e);

}

} else {

log.info("404 Page not found {}", redirectUrl);

}

}

/**

* Returns the languageCode of the request URL

*/

private String setLanguageCode(String requestUrl) {

String language = EN_US;

String path = getPath(requestUrl);

if (path != null) {

Matcher languageMatcher = REGEX_LANGUAGE_SELECTOR.matcher(path);

if (languageMatcher.find()) {

language = languageMatcher.group(2);

}

}

return language;

}

private static String getPath(String uri)

{

String path = null;

try {

URI url = new URI(uri);

path = url.getPath();

} catch (URISyntaxException e) {

log.error("Could not process uri {}", uri, e);

}

return path;

}

}

Thanks

Arun_Patidar
MVP
13-09-2018

Hi Vijay,

How did you included your 404 page inside 404.html?

I tried in 6.3 with below and working in both the scenario

<article data-sly-resource="path/to/resource"></article>