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#.XOfD...
These are the steps to reproduce the exception.
Crate java class under core
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;
}
}
import com.adobe.cq.sightly.WCMUsePojo;
public class ResponseStatus extends WCMUsePojo {
@Override
public void activate() throws Exception {
getResponse().setStatus(404);
}
}
<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>
<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.
Output-1
Output-2
Please help me to achieve this
Thanks,
Kiran
Message was edited by: Kiran veeranki
Solved! Go to Solution.
Views
Replies
Total Likes
Have you already checked Error Page Handler ?
Views
Replies
Total Likes
Have you already checked Error Page Handler ?
Views
Replies
Total Likes
Hi Joerg,
Sorry for late response.I have gone through the above url, but I need to implement my custom error handler.
Can you help me to achieve this?
Thanks,
Kiran
Views
Replies
Total Likes
Check this
Views
Replies
Total Likes
Hi Webcoholic,
I have already gone through this link and it is for global error handling which is working fine for me.
I need specific project level error handling.
Thanks,
Kiran
Views
Replies
Total Likes
For Project level you can create custom error pages as per your project and configure at root level .Also you may need to configure the settings at dispatcher level.
https://www.albinsblog.com/2015/03/how-to-customizeconfigure-404-error.html#.XO9cnYgzbIU
Above links are of previous versions but might help you to get some idea for implementation in 6.4 version