How to implement custom error handling in AEM 6.4? | Community
Skip to main content
Level 3
May 23, 2019
Solved

How to implement custom error handling in AEM 6.4?

  • May 23, 2019
  • 5 replies
  • 6463 views

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

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by joerghoh

Have you already checked Error Page Handler ?

5 replies

joerghoh
Adobe Employee
joerghohAdobe EmployeeAccepted solution
Adobe Employee
May 24, 2019

Have you already checked Error Page Handler ?

Level 3
May 28, 2019

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

Level 2
May 28, 2019
Level 3
May 29, 2019

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

Level 2
May 30, 2019

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.

Reference: http://www.aemcq5tutorials.com/tutorials/adobe-aem-cq5-tutorials/custom-error-pages-acs-commons-errorhandler/

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