Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

How to implement custom error handling in AEM 6.4?

Avatar

Level 3

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...

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.

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.

1758434_pastedImage_9.png

Step - 5

Create 404 and 500 pages under content/***/en/

1758437_pastedImage_26.png

Output-1

1758435_pastedImage_12.png

Output-2

1758436_pastedImage_15.png

Please help me to achieve this

Thanks,

Kiran

Message was edited by: Kiran veeranki

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor
5 Replies

Avatar

Correct answer by
Employee Advisor

Have you already checked Error Page Handler ?

Avatar

Level 3

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

Avatar

Level 3

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

Avatar

Level 2

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-erro...

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