use of jakarta.ws.rs.ext.ExceptionMapper in project minijax by minijax.
the class MinijaxClientProviders method getExceptionMapper.
@Override
@SuppressWarnings("unchecked")
public <T extends Throwable> ExceptionMapper<T> getExceptionMapper(final Class<T> type) {
for (final Class<? extends ExceptionMapper<?>> exceptionMapperClass : context.getExceptionMappers()) {
final ParameterizedType parameterizedType = (ParameterizedType) exceptionMapperClass.getGenericInterfaces()[0];
final Class<? extends Exception> exClass = (Class<? extends Exception>) parameterizedType.getActualTypeArguments()[0];
if (exClass.isAssignableFrom(type)) {
return (ExceptionMapper<T>) context.getResource(exceptionMapperClass);
}
}
return null;
}
use of jakarta.ws.rs.ext.ExceptionMapper in project tomee by apache.
the class EJBExceptionMapper method toResponse.
@Override
public Response toResponse(final EJBException ejbException) {
final Exception cause = ejbException.getCausedByException();
if (cause != null) {
final Class causeClass = cause.getClass();
final ExceptionMapper exceptionMapper = providers.getExceptionMapper(causeClass);
if (exceptionMapper == null) {
return defaultResponse(cause);
}
return exceptionMapper.toResponse(cause);
} else if (EJBAccessException.class.isInstance(ejbException)) {
return Response.status(Response.Status.FORBIDDEN).build();
}
return defaultResponse(ejbException);
}
use of jakarta.ws.rs.ext.ExceptionMapper in project resteasy by resteasy.
the class ExceptionHandler method executeExactExceptionMapper.
/**
* If there exists an Exception mapper for exception, execute it, otherwise, do NOT recurse up class hierarchy
* of exception.
*
* @param exception exception
* @param logger logger
* @return response response object
*/
@SuppressWarnings(value = "unchecked")
protected Response executeExactExceptionMapper(Throwable exception, RESTEasyTracingLogger logger) {
if (logger == null)
logger = RESTEasyTracingLogger.empty();
ExceptionMapper mapper = providerFactory.getExceptionMapperForClass(exception.getClass());
if (mapper == null)
return null;
mapperExecuted = true;
long timestamp = logger.timestamp("EXCEPTION_MAPPING");
Response resp = mapper.toResponse(exception);
logger.logDuration("EXCEPTION_MAPPING", timestamp, mapper, exception, exception.getLocalizedMessage(), resp);
return resp;
}
use of jakarta.ws.rs.ext.ExceptionMapper in project resteasy by resteasy.
the class AbstractExceptionMapperTest method testCustomUsed.
/**
* @tpTestDetails Correct exception mapper should be chosen when ExceptionMapper implement statement is in abstract class.
* @tpSince RESTEasy 3.0.16
*/
@Test
public void testCustomUsed() {
Type exceptionType = Types.getActualTypeArgumentsOfAnInterface(AbstractMapperMyCustom.class, ExceptionMapper.class)[0];
Assert.assertEquals(AbstractMapperException.class, exceptionType);
Response response = client.target(PortProviderUtil.generateURL("/resource/custom", AbstractExceptionMapperTest.class.getSimpleName())).request().get();
Assert.assertEquals(HttpResponseCodes.SC_OK, response.getStatus());
Assert.assertEquals("custom", response.readEntity(String.class));
}
use of jakarta.ws.rs.ext.ExceptionMapper in project resteasy by resteasy.
the class ExceptionMapperTest method testCustomExceptionsUsed.
/**
* @tpTestDetails Client sends GET request to the server, which causes application custom exception being thrown, this
* exception is caught by application provided ExceptionMapper. The application provides two providers for mapping
* exceptions. General RuntimeException mapping and MyCustomException mapping which extends the RuntimeException.
* @tpPassCrit The more specific MyCustomException mapping will be used.
* @tpSince RESTEasy 3.0.16
*/
@Test
public void testCustomExceptionsUsed() {
Type exceptionType = Types.getActualTypeArgumentsOfAnInterface(ExceptionMapperMyCustomExceptionMapper.class, ExceptionMapper.class)[0];
Assert.assertEquals(ExceptionMapperMyCustomException.class, exceptionType);
Response response = client.target(generateURL("/resource/custom")).request().get();
Assert.assertNotEquals("General RuntimeException mapper was used instead of more specific one", HttpResponseCodes.SC_NOT_ACCEPTABLE, response.getStatus());
Assert.assertEquals(HttpResponseCodes.SC_OK, response.getStatus());
Assert.assertEquals("custom", response.readEntity(String.class));
}
Aggregations