Search in sources :

Example 1 with ErrorHandlerScript

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);
    }
}
Also used : ApplicationKey(com.enonic.xp.app.ApplicationKey) ErrorHandlerScript(com.enonic.xp.portal.impl.error.ErrorHandlerScript) ResourceKey(com.enonic.xp.resource.ResourceKey) PortalRequest(com.enonic.xp.portal.PortalRequest)

Example 2 with ErrorHandlerScript

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());
}
Also used : Site(com.enonic.xp.site.Site) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) ContentService(com.enonic.xp.content.ContentService) WebException(com.enonic.xp.web.WebException) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) ErrorHandlerScript(com.enonic.xp.portal.impl.error.ErrorHandlerScript) ResourceKey(com.enonic.xp.resource.ResourceKey) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Branch(com.enonic.xp.branch.Branch) ContentId(com.enonic.xp.content.ContentId) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpHeaders(com.google.common.net.HttpHeaders) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) PortalRequest(com.enonic.xp.portal.PortalRequest) PortalResponse(com.enonic.xp.portal.PortalResponse) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) PropertyTree(com.enonic.xp.data.PropertyTree) MediaType(com.google.common.net.MediaType) ResourceService(com.enonic.xp.resource.ResourceService) ContentPath(com.enonic.xp.content.ContentPath) SiteConfig(com.enonic.xp.site.SiteConfig) ErrorHandlerScriptFactory(com.enonic.xp.portal.impl.error.ErrorHandlerScriptFactory) Mockito.when(org.mockito.Mockito.when) RenderMode(com.enonic.xp.portal.RenderMode) ApplicationKey(com.enonic.xp.app.ApplicationKey) Test(org.junit.jupiter.api.Test) Site(com.enonic.xp.site.Site) RunMode(com.enonic.xp.server.RunMode) Resource(com.enonic.xp.resource.Resource) SiteConfigs(com.enonic.xp.site.SiteConfigs) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) HttpStatus(com.enonic.xp.web.HttpStatus) AdditionalMatchers.not(org.mockito.AdditionalMatchers.not) Mockito.mock(org.mockito.Mockito.mock) PortalResponse(com.enonic.xp.portal.PortalResponse) WebException(com.enonic.xp.web.WebException) Resource(com.enonic.xp.resource.Resource) ErrorHandlerScript(com.enonic.xp.portal.impl.error.ErrorHandlerScript) ResourceKey(com.enonic.xp.resource.ResourceKey) Test(org.junit.jupiter.api.Test)

Example 3 with ErrorHandlerScript

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());
}
Also used : Site(com.enonic.xp.site.Site) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) ContentService(com.enonic.xp.content.ContentService) WebException(com.enonic.xp.web.WebException) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) ErrorHandlerScript(com.enonic.xp.portal.impl.error.ErrorHandlerScript) ResourceKey(com.enonic.xp.resource.ResourceKey) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Branch(com.enonic.xp.branch.Branch) ContentId(com.enonic.xp.content.ContentId) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpHeaders(com.google.common.net.HttpHeaders) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) PortalRequest(com.enonic.xp.portal.PortalRequest) PortalResponse(com.enonic.xp.portal.PortalResponse) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) PropertyTree(com.enonic.xp.data.PropertyTree) MediaType(com.google.common.net.MediaType) ResourceService(com.enonic.xp.resource.ResourceService) ContentPath(com.enonic.xp.content.ContentPath) SiteConfig(com.enonic.xp.site.SiteConfig) ErrorHandlerScriptFactory(com.enonic.xp.portal.impl.error.ErrorHandlerScriptFactory) Mockito.when(org.mockito.Mockito.when) RenderMode(com.enonic.xp.portal.RenderMode) ApplicationKey(com.enonic.xp.app.ApplicationKey) Test(org.junit.jupiter.api.Test) Site(com.enonic.xp.site.Site) RunMode(com.enonic.xp.server.RunMode) Resource(com.enonic.xp.resource.Resource) SiteConfigs(com.enonic.xp.site.SiteConfigs) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) HttpStatus(com.enonic.xp.web.HttpStatus) AdditionalMatchers.not(org.mockito.AdditionalMatchers.not) Mockito.mock(org.mockito.Mockito.mock) PortalResponse(com.enonic.xp.portal.PortalResponse) WebException(com.enonic.xp.web.WebException) Resource(com.enonic.xp.resource.Resource) ErrorHandlerScript(com.enonic.xp.portal.impl.error.ErrorHandlerScript) ResourceKey(com.enonic.xp.resource.ResourceKey) Test(org.junit.jupiter.api.Test)

Example 4 with ErrorHandlerScript

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"));
}
Also used : Site(com.enonic.xp.site.Site) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) ContentService(com.enonic.xp.content.ContentService) WebException(com.enonic.xp.web.WebException) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) ErrorHandlerScript(com.enonic.xp.portal.impl.error.ErrorHandlerScript) ResourceKey(com.enonic.xp.resource.ResourceKey) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Branch(com.enonic.xp.branch.Branch) ContentId(com.enonic.xp.content.ContentId) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpHeaders(com.google.common.net.HttpHeaders) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) PortalRequest(com.enonic.xp.portal.PortalRequest) PortalResponse(com.enonic.xp.portal.PortalResponse) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) PropertyTree(com.enonic.xp.data.PropertyTree) MediaType(com.google.common.net.MediaType) ResourceService(com.enonic.xp.resource.ResourceService) ContentPath(com.enonic.xp.content.ContentPath) SiteConfig(com.enonic.xp.site.SiteConfig) ErrorHandlerScriptFactory(com.enonic.xp.portal.impl.error.ErrorHandlerScriptFactory) Mockito.when(org.mockito.Mockito.when) RenderMode(com.enonic.xp.portal.RenderMode) ApplicationKey(com.enonic.xp.app.ApplicationKey) Test(org.junit.jupiter.api.Test) Site(com.enonic.xp.site.Site) RunMode(com.enonic.xp.server.RunMode) Resource(com.enonic.xp.resource.Resource) SiteConfigs(com.enonic.xp.site.SiteConfigs) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) HttpStatus(com.enonic.xp.web.HttpStatus) AdditionalMatchers.not(org.mockito.AdditionalMatchers.not) Mockito.mock(org.mockito.Mockito.mock) PortalResponse(com.enonic.xp.portal.PortalResponse) WebException(com.enonic.xp.web.WebException) Resource(com.enonic.xp.resource.Resource) ErrorHandlerScript(com.enonic.xp.portal.impl.error.ErrorHandlerScript) ResourceKey(com.enonic.xp.resource.ResourceKey) Test(org.junit.jupiter.api.Test)

Example 5 with ErrorHandlerScript

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());
}
Also used : Site(com.enonic.xp.site.Site) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) ContentService(com.enonic.xp.content.ContentService) WebException(com.enonic.xp.web.WebException) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) ErrorHandlerScript(com.enonic.xp.portal.impl.error.ErrorHandlerScript) ResourceKey(com.enonic.xp.resource.ResourceKey) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Branch(com.enonic.xp.branch.Branch) ContentId(com.enonic.xp.content.ContentId) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpHeaders(com.google.common.net.HttpHeaders) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) PortalRequest(com.enonic.xp.portal.PortalRequest) PortalResponse(com.enonic.xp.portal.PortalResponse) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) PropertyTree(com.enonic.xp.data.PropertyTree) MediaType(com.google.common.net.MediaType) ResourceService(com.enonic.xp.resource.ResourceService) ContentPath(com.enonic.xp.content.ContentPath) SiteConfig(com.enonic.xp.site.SiteConfig) ErrorHandlerScriptFactory(com.enonic.xp.portal.impl.error.ErrorHandlerScriptFactory) Mockito.when(org.mockito.Mockito.when) RenderMode(com.enonic.xp.portal.RenderMode) ApplicationKey(com.enonic.xp.app.ApplicationKey) Test(org.junit.jupiter.api.Test) Site(com.enonic.xp.site.Site) RunMode(com.enonic.xp.server.RunMode) Resource(com.enonic.xp.resource.Resource) SiteConfigs(com.enonic.xp.site.SiteConfigs) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) HttpStatus(com.enonic.xp.web.HttpStatus) AdditionalMatchers.not(org.mockito.AdditionalMatchers.not) Mockito.mock(org.mockito.Mockito.mock) PortalResponse(com.enonic.xp.portal.PortalResponse) WebException(com.enonic.xp.web.WebException) Resource(com.enonic.xp.resource.Resource) ErrorHandlerScript(com.enonic.xp.portal.impl.error.ErrorHandlerScript) ResourceKey(com.enonic.xp.resource.ResourceKey) Test(org.junit.jupiter.api.Test)

Aggregations

ApplicationKey (com.enonic.xp.app.ApplicationKey)5 PortalRequest (com.enonic.xp.portal.PortalRequest)5 ErrorHandlerScript (com.enonic.xp.portal.impl.error.ErrorHandlerScript)5 ResourceKey (com.enonic.xp.resource.ResourceKey)5 Branch (com.enonic.xp.branch.Branch)4 ContentId (com.enonic.xp.content.ContentId)4 ContentPath (com.enonic.xp.content.ContentPath)4 ContentService (com.enonic.xp.content.ContentService)4 PropertyTree (com.enonic.xp.data.PropertyTree)4 PortalResponse (com.enonic.xp.portal.PortalResponse)4 RenderMode (com.enonic.xp.portal.RenderMode)4 ErrorHandlerScriptFactory (com.enonic.xp.portal.impl.error.ErrorHandlerScriptFactory)4 Resource (com.enonic.xp.resource.Resource)4 ResourceService (com.enonic.xp.resource.ResourceService)4 RunMode (com.enonic.xp.server.RunMode)4 Site (com.enonic.xp.site.Site)4 SiteConfig (com.enonic.xp.site.SiteConfig)4 SiteConfigs (com.enonic.xp.site.SiteConfigs)4 HttpStatus (com.enonic.xp.web.HttpStatus)4 WebException (com.enonic.xp.web.WebException)4