How to implement custom error handling in AEM 6.4?
Hi, I am trying to implement project level custom error handling in aem 6.4, but getting some exception.
Based on project error page should change.I have created 404,500 pages in my content path.
I have followed the below link https://www.albinsblog.com/2018/07/how-to-display-custom-site-specific-error-pages-in-aem.html#.XOfDB_kzaUn
These are the steps to reproduce the exception.
Step - 1
Crate java class under core
ErrorHandlerReqStepuestModel.java
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.models.annotations.Default;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.Self;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
@Model(adaptables = SlingHttpServletRequest.class,
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class ErrorHandlerReqStepuestModel {
protected final Logger logger = LoggerFactory.getLogger(ErrorHandlerRequestModel.class);
private static final String DEFAULT_ERROR_PAGE = "/content/aem/en";
private static final String ERROR_CODE_404 = "404";
private static final int MAX_DEPTH = 2;
private static final String PATH_SEPERATOR = "/";
@Self
private SlingHttpServletRequest slingRequest;
@Inject
@Default(values = ERROR_CODE_404)
private String errorCode;
private String pagePath;
@PostConstruct
protected void init() {
pagePath = DEFAULT_ERROR_PAGE + errorCode;
final String requestURI = slingRequest.getRequestPathInfo().getResourcePath();
if (requestURI != null && !requestURI.equals("")) {
pagePath = getErrorPageFromRequestedUrl(errorCode, requestURI);
}
}
private String getErrorPageFromRequestedUrl(final String errorCode, final String requestURI) {
final Page resolvedPage = getPageFromPath(requestURI);
if (resolvedPage != null) {
return getErrorPathFromPage(errorCode, resolvedPage);
}
return null;
}
private Page getPageFromPath(String requestURI) {
final PageManager pageManager = slingRequest.getResourceResolver().adaptTo(PageManager.class);
while (requestURI.contains(PATH_SEPERATOR)) {
Page page = pageManager.getContainingPage(requestURI);
if (page != null) {
return page;
} else {
requestURI = requestURI.substring(0, requestURI.lastIndexOf(PATH_SEPERATOR));
}
}
return null;
}
private String getErrorPathFromPage(final String errorCode, final Page resolvedPage) {
if (resolvedPage.hasChild(errorCode)) {
return resolvedPage.getPath() + PATH_SEPERATOR + errorCode;
}
if (resolvedPage.getParent() != null && resolvedPage.getDepth() >= MAX_DEPTH) {
return getErrorPathFromPage(errorCode, resolvedPage.getParent());
}
return null;
}
public String getPagePath() {
return pagePath;
}
}
Step - 2
ResponseStatus.java
import com.adobe.cq.sightly.WCMUsePojo;
public class ResponseStatus extends WCMUsePojo {
@Override
public void activate() throws Exception {
getResponse().setStatus(404);
}
}
Step - 3
Exception.html
<sly data-sly-use.responseStatus="apps.sling.servlet.errorhandler.ResponseStatus">
<sly data-sly-use.errorPage="${'com.aem.test.ErrorHandlerRequestModel'@ errorCode='500'}"/>
<sly data-sly-resource="${errorPage.pagePath}.html"/>
</sly>
Step - 4
404.html
<sly data-sly-use.responseStatus="apps.sling.servlet.errorhandler.ResponseStatus">
<sly data-sly-use.errorPage="${'com.aem.test.ErrorHandlerRequestModel'@ errorCode='404'}"/>
<sly data-sly-resource="${errorPage.pagePath}.html"/>
</sly>
Screenshot for error handler
Overlay the eror handler from libs.

Step - 5
Create 404 and 500 pages under content/***/en/

Output-1

Output-2

Please help me to achieve this
Thanks,
Kiran
Message was edited by: Kiran veeranki