use of com.enonic.xp.portal.impl.error.ErrorHandlerScript in project xp by enonic.
the class ExceptionRendererImpl method renderApplicationCustomError.
private PortalResponse renderApplicationCustomError(final ApplicationKey appKey, final String errorScriptPath, final PortalError portalError, final String handlerMethod) {
final ResourceKey script = getScript(appKey, errorScriptPath);
if (script == null) {
return null;
}
final ErrorHandlerScript errorHandlerScript = this.errorHandlerScriptFactory.errorScript(script);
final PortalRequest request = portalError.getRequest();
final ApplicationKey previousApp = request.getApplicationKey();
// set application of the error handler in the current context PortalRequest
try {
request.setApplicationKey(appKey);
return errorHandlerScript.execute(portalError, handlerMethod);
} finally {
request.setApplicationKey(previousApp);
}
}
use of com.enonic.xp.portal.impl.error.ErrorHandlerScript in project xp by enonic.
the class ExceptionRendererImplTest method render_custom_error_with_site_context.
@Test
void render_custom_error_with_site_context() {
this.request.getHeaders().put(HttpHeaders.ACCEPT, "text/html,text/*");
final Site site = newSite();
this.request.setSite(site);
final ResourceKey errorResource = ResourceKey.from(ApplicationKey.from("myapplication"), "site/error/error.js");
final ErrorHandlerScript errorHandlerScript = (portalError, handlerMethod) -> PortalResponse.create().body("Custom message page").status(HttpStatus.BAD_REQUEST).postProcess(false).build();
when(this.errorHandlerScriptFactory.errorScript(errorResource)).thenReturn(errorHandlerScript);
final Resource resource = mock(Resource.class);
when(resource.exists()).thenReturn(true);
when(this.resourceService.getResource(errorResource)).thenReturn(resource);
final RuntimeException cause = new RuntimeException("Custom message");
final PortalResponse res = this.renderer.render(this.request, new WebException(HttpStatus.BAD_REQUEST, cause));
assertEquals(HttpStatus.BAD_REQUEST, res.getStatus());
assertEquals("Custom message page", res.getBody().toString());
assertFalse(postProcessor.isExecuted());
}
use of com.enonic.xp.portal.impl.error.ErrorHandlerScript in project xp by enonic.
the class ExceptionRendererImplTest method customErrorWithPostProcessing.
@Test
void customErrorWithPostProcessing() {
this.request.getHeaders().put(HttpHeaders.ACCEPT, "text/html,text/*");
final Site site = newSite();
this.request.setSite(site);
final ResourceKey errorResource = ResourceKey.from(ApplicationKey.from("myapplication"), "site/error/error.js");
final ErrorHandlerScript errorHandlerScript = (portalError, handlerMethod) -> PortalResponse.create().body("<h1>Custom message page</h1><!--#COMPONENT module:myPart -->").status(HttpStatus.BAD_REQUEST).postProcess(true).build();
when(this.errorHandlerScriptFactory.errorScript(errorResource)).thenReturn(errorHandlerScript);
final Resource resource = mock(Resource.class);
when(resource.exists()).thenReturn(true);
when(this.resourceService.getResource(errorResource)).thenReturn(resource);
final RuntimeException cause = new RuntimeException("Custom message");
final PortalResponse res = this.renderer.render(this.request, new WebException(HttpStatus.BAD_REQUEST, cause));
assertEquals(HttpStatus.BAD_REQUEST, res.getStatus());
assertEquals("<h1>Custom message page</h1><h3>My Part</h3>", res.getBody().toString());
assertTrue(postProcessor.isExecuted());
}
use of com.enonic.xp.portal.impl.error.ErrorHandlerScript in project xp by enonic.
the class ExceptionRendererImplTest method render_default_error_page_when_error_in_custom_handler.
@Test
void render_default_error_page_when_error_in_custom_handler() {
this.request.getHeaders().put(HttpHeaders.ACCEPT, "text/html,text/*");
final Site site = newSite();
this.request.setSite(site);
final ResourceKey errorResource = ResourceKey.from(ApplicationKey.from("myapplication"), "site/error/error.js");
final ErrorHandlerScript errorHandlerScript = (portalError, handleMethod) -> {
throw new RuntimeException("Something went wrong in the handler script");
};
when(this.errorHandlerScriptFactory.errorScript(errorResource)).thenReturn(errorHandlerScript);
final Resource resource = mock(Resource.class);
when(resource.exists()).thenReturn(true);
when(this.resourceService.getResource(errorResource)).thenReturn(resource);
final RuntimeException cause = new RuntimeException("Custom message");
final PortalResponse res = this.renderer.render(this.request, new WebException(HttpStatus.BAD_REQUEST, cause));
final String body = res.getBody().toString();
assertTrue(body.contains("400 Bad Request"));
assertTrue(body.contains("Custom message"));
}
use of com.enonic.xp.portal.impl.error.ErrorHandlerScript in project xp by enonic.
the class ExceptionRendererImplTest method render_custom_error_for_404_in_site_path.
@Test
void render_custom_error_for_404_in_site_path() {
this.request.getHeaders().put(HttpHeaders.ACCEPT, "text/html,text/*");
this.request.setContentPath(ContentPath.from("/mysite/some/long/path"));
final Site site = newSite();
when(contentService.findNearestSiteByPath(eq(ContentPath.from("/mysite/some/long/path")))).thenReturn(site);
final ResourceKey errorResource = ResourceKey.from(ApplicationKey.from("myapplication"), "site/error/error.js");
final ErrorHandlerScript errorHandlerScript = (portalError, handleMethod) -> PortalResponse.create().body("Custom message page").status(HttpStatus.NOT_FOUND).postProcess(false).build();
when(this.errorHandlerScriptFactory.errorScript(errorResource)).thenReturn(errorHandlerScript);
final Resource resource = mock(Resource.class);
when(resource.exists()).thenReturn(true);
when(this.resourceService.getResource(errorResource)).thenReturn(resource);
final RuntimeException cause = new RuntimeException("Custom message");
final PortalResponse res = this.renderer.render(this.request, new WebException(HttpStatus.NOT_FOUND, cause));
assertEquals(HttpStatus.NOT_FOUND, res.getStatus());
assertEquals("Custom message page", res.getBody().toString());
assertFalse(postProcessor.isExecuted());
}
Aggregations