package io.dropwizard.views;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * An {@link ExceptionMapper} that returns a 500 error response with a generic
 * HTML error page when a {@link ViewRenderException} is thrown.
 * 
 * @since 1.1.0
 */
@Provider
public class ViewRenderExceptionMapper implements ExceptionMapper<WebApplicationException> {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(ViewRenderExceptionMapper.class);

    /**
     * The generic HTML error page template.
     */
    public static final String TEMPLATE_ERROR_MSG =
            "<html>" +
                "<head><title>Template Error</title></head>" +
                "<body><h1>Template Error</h1><p>Something went wrong rendering the page</p></body>" +
            "</html>";

    @Override
    public Response toResponse(WebApplicationException exception) {
        Throwable cause = exception.getCause();
        if (cause instanceof ViewRenderException) {
            LOGGER.error("Template Error", cause);
            return Response.serverError()
                    .type(MediaType.TEXT_HTML_TYPE)
                    .entity(TEMPLATE_ERROR_MSG)
                    .build();
        }
        return exception.getResponse();
    }

}

@@ -163,59 +163,134 @@
 ext.
-ExceptionMapper;%0Aimport javax.ws.rs.ext.Provid
+Provider;%0A%0Aimport org.apache.commons.lang3.exception.ExceptionUtils;%0Aimport org.glassfish.jersey.spi.ExtendedExceptionMapp
 er;%0A
-%0A
 impo
@@ -360,16 +360,24 @@
  %7B@link 
+Extended
 Exceptio
@@ -497,17 +497,19 @@
 s th
-rown
+e cause
 .%0A *
- 
 %0A * 
@@ -585,16 +585,24 @@
 lements 
+Extended
 Exceptio
@@ -636,20 +636,16 @@
 tion%3E %7B%0A
-    
 %0A    pri
@@ -1149,112 +1149,8 @@
 ) %7B%0A
-        Throwable cause = exception.getCause();%0A        if (cause instanceof ViewRenderException) %7B%0A    
     
@@ -1184,21 +1184,25 @@
 Error%22, 
-cause
+exception
 );%0A     
@@ -1200,28 +1200,24 @@
 n);%0A        
-    
 return Respo
@@ -1234,20 +1234,16 @@
 Error()%0A
-    
         
@@ -1298,20 +1298,16 @@
         
-    
 .entity(
@@ -1326,20 +1326,16 @@
 OR_MSG)%0A
-    
         
@@ -1356,21 +1356,91 @@
 ();%0A    
+%7D%0A%0A
     
-%7D
+@Override%0A    public boolean isMappable(WebApplicationException e) %7B
 %0A       
@@ -1447,25 +1447,25 @@
  return 
-e
+E
 xception
 .getResp
@@ -1460,29 +1460,73 @@
 tion
-.getResponse()
+Utils.indexOfThrowable(e, ViewRenderException.class) != -1
 ;%0A    %7D%0A
 %0A%7D%0A
@@ -1522,11 +1522,10 @@
 ;%0A    %7D%0A
-%0A
 %7D%0A

Move ViewExceptionMapper to an ExtendedExceptionMapper