Search in sources :

Example 6 with ExceptionMapper

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;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ExceptionMapper(jakarta.ws.rs.ext.ExceptionMapper)

Example 7 with ExceptionMapper

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);
}
Also used : ExceptionMapper(jakarta.ws.rs.ext.ExceptionMapper) EJBAccessException(jakarta.ejb.EJBAccessException) EJBException(jakarta.ejb.EJBException) EJBAccessException(jakarta.ejb.EJBAccessException)

Example 8 with ExceptionMapper

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;
}
Also used : Response(jakarta.ws.rs.core.Response) ExceptionMapper(jakarta.ws.rs.ext.ExceptionMapper)

Example 9 with ExceptionMapper

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));
}
Also used : Response(jakarta.ws.rs.core.Response) Type(java.lang.reflect.Type) ExceptionMapper(jakarta.ws.rs.ext.ExceptionMapper) AbstractMapperMyCustom(org.jboss.resteasy.test.exception.resource.AbstractMapperMyCustom) Test(org.junit.Test)

Example 10 with ExceptionMapper

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));
}
Also used : Response(jakarta.ws.rs.core.Response) Type(java.lang.reflect.Type) ExceptionMapperWebAppExceptionMapper(org.jboss.resteasy.test.exception.resource.ExceptionMapperWebAppExceptionMapper) NotFoundExceptionMapper(org.jboss.resteasy.test.exception.resource.NotFoundExceptionMapper) ExceptionMapperMyCustomExceptionMapper(org.jboss.resteasy.test.exception.resource.ExceptionMapperMyCustomExceptionMapper) ExceptionMapperAbstractExceptionMapper(org.jboss.resteasy.test.exception.resource.ExceptionMapperAbstractExceptionMapper) ExceptionMapper(jakarta.ws.rs.ext.ExceptionMapper) ExceptionMapperMyCustomExceptionMapper(org.jboss.resteasy.test.exception.resource.ExceptionMapperMyCustomExceptionMapper) Test(org.junit.Test)

Aggregations

ExceptionMapper (jakarta.ws.rs.ext.ExceptionMapper)14 Response (jakarta.ws.rs.core.Response)6 Type (java.lang.reflect.Type)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Config (io.helidon.config.Config)3 Test (org.junit.Test)3 BeforeAll (org.junit.jupiter.api.BeforeAll)3 ForbiddenException (jakarta.ws.rs.ForbiddenException)2 ParameterizedType (java.lang.reflect.ParameterizedType)2 URI (java.net.URI)2 Security (io.helidon.security.Security)1 EJBAccessException (jakarta.ejb.EJBAccessException)1 EJBException (jakarta.ejb.EJBException)1 RuntimeType (jakarta.ws.rs.RuntimeType)1 GenericType (jakarta.ws.rs.core.GenericType)1 IOException (java.io.IOException)1 List (java.util.List)1 AbstractMapperMyCustom (org.jboss.resteasy.test.exception.resource.AbstractMapperMyCustom)1 ExceptionMapperAbstractExceptionMapper (org.jboss.resteasy.test.exception.resource.ExceptionMapperAbstractExceptionMapper)1