Hello, community.
I'm trying to change the standard error handler along the path /libs/sling/servlet/errorhandler.
I also created a service and an implementation for it to use the configuration.
In default.jsp I added import com.custom.aem.common.errorhandler.CustomErrorHandlerService
<%@page session="false" pageEncoding="utf-8"
import="com.day.cq.wcm.api.WCMMode,
java.io.PrintWriter,
org.apache.sling.api.SlingConstants,
org.apache.sling.settings.SlingSettingsService,
org.apache.sling.api.request.RequestProgressTracker,
org.apache.sling.api.request.ResponseUtil,
org.apache.commons.lang3.StringEscapeUtils,
com.custom.aem.common.errorhandler.CustomErrorHandlerService" %>
<%@taglib prefix="sling" uri="http://sling.apache.org/taglibs/sling/1.0" %>
<sling:defineObjects />
And trying to get properties from config by service in JSP.
<%
String message = (String) request.getAttribute("javax.servlet.error.message");
Integer scObject = (Integer) request.getAttribute("javax.servlet.error.status_code");
boolean isAuthorMode = WCMMode.fromRequest(request) != WCMMode.DISABLED && !sling.getService(SlingSettingsService.class).getRunModes().contains("publish");
int statusCode = (scObject != null)
? scObject.intValue()
: HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
if (message == null) {
message = statusToString(statusCode);
}
response.setStatus(statusCode);
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
CustomErrorHandlerService customErrorHandlerService = sling.getService(CustomErrorHandlerService.class);
String[] sites = customErrorHandlerService.getSites();
String externalErrorPage = customErrorHandlerService.getExternalErrorPage();
String requestSite = request.getRequestURL().toString();
boolean isSiteCatchError = isSiteContains(sites, requestSite);
%>
but faced issue
Caused by: java.lang.ClassCastException: com.custom.aem.common.errorhandler.CustomErrorHandlerServiceImpl cannot be cast to com.custom.aem.common.errorhandler.CustomErrorHandlerService at org.apache.jsp.apps.sling.servlet.errorhandler.default_jsp._jspService(default_jsp.java:408) at org.apache.sling.scripting.jsp.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) at javax.servlet.http.HttpServlet.service(HttpServlet.java:725) at org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:502) ... 84 common frames omitted
Service
public interface CustomErrorHandlerService {
String[] getSites();
String getExternalErrorPage();
}
Implementation
(service = CustomErrorHandlerService.class, immediate = true)
@Designate(ocd = CustomErrorHandlerServiceImpl.Config.class)
public class CustomErrorHandlerServiceImpl implements CustomErrorHandlerService {
private static final Logger LOGGER = LoggerFactory.getLogger(CustomErrorHandlerServiceImpl.class);
@ObjectClassDefinition(
name = "Custom Error Page Handler Configuration",
description = "This configuration reads the values to make an error page for sites"
)
Config {
@AttributeDefinition(
name = "Sites",
description = "Sites for Handling Error Pages",
type = AttributeType.STRING)
String[] getSites();
@AttributeDefinition(
name = "External Error Page Url",
description = "Link to external error page")
String getExternalErrorPage();
}
private String[] sites;
private String externalErrorPage;
protected void activate(Config config) {
this.sites = config.getSites();
this.externalErrorPage = config.getExternalErrorPage();
}
protected void deactivate() {
LOGGER.info("CustomErrorHandlerServiceImpl has been deactivated!");
}
public String[] getSites() {
return sites;
}
public String getExternalErrorPage() {
return externalErrorPage;
}
}
What am I doing wrong? Thanks in advance.
Solved! Go to Solution.
Views
Replies
Total Likes
Fixed. The reason was that 2 identical services were registered. After turning off (deleting) everything worked fine. Thanks to all.
Are you missing @component annotation of top of the implementation class?
Sorry, I missed in the message. I have annotation in class
@component(service = CustomErrorHandlerService.class, immediate = true)
Fixed. The reason was that 2 identical services were registered. After turning off (deleting) everything worked fine. Thanks to all.