Search in sources :

Example 1 with ExceptionMapper

use of jakarta.ws.rs.ext.ExceptionMapper in project resteasy by resteasy.

the class ExceptionHandler method executeExceptionMapper.

/**
 * Execute an ExceptionMapper if one exists for the given exception.  Recurse to base class if not found.
 *
 * @param exception exception
 * @param logger logger
 * @return true if an ExceptionMapper was found and executed
 */
@SuppressWarnings(value = "unchecked")
protected Response executeExceptionMapper(Throwable exception, RESTEasyTracingLogger logger) {
    if (logger == null)
        logger = RESTEasyTracingLogger.empty();
    ExceptionMapper mapper = null;
    Class causeClass = exception.getClass();
    while (mapper == null) {
        if (causeClass == null)
            break;
        mapper = providerFactory.getExceptionMapperForClass(causeClass);
        if (mapper == null)
            causeClass = causeClass.getSuperclass();
    }
    if (mapper != null) {
        mapperExecuted = true;
        final long timestamp = logger.timestamp("EXCEPTION_MAPPING");
        Response jaxrsResponse = mapper.toResponse(exception);
        logger.logDuration("EXCEPTION_MAPPING", timestamp, mapper, exception, exception.getLocalizedMessage(), jaxrsResponse);
        if (jaxrsResponse == null) {
            jaxrsResponse = Response.status(204).build();
        }
        return jaxrsResponse;
    }
    return null;
}
Also used : Response(jakarta.ws.rs.core.Response) ExceptionMapper(jakarta.ws.rs.ext.ExceptionMapper)

Example 2 with ExceptionMapper

use of jakarta.ws.rs.ext.ExceptionMapper in project resteasy by resteasy.

the class ExceptionHandler method executeExceptionMapperForClass.

@SuppressWarnings(value = "unchecked")
protected Response executeExceptionMapperForClass(Throwable exception, Class clazz, RESTEasyTracingLogger logger) {
    if (logger == null)
        logger = RESTEasyTracingLogger.empty();
    ExceptionMapper mapper = providerFactory.getExceptionMapperForClass(clazz);
    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 3 with ExceptionMapper

use of jakarta.ws.rs.ext.ExceptionMapper in project resteasy by resteasy.

the class ServerHelper method addExceptionMapper.

private void addExceptionMapper(ExceptionMapper provider, Class providerClass, boolean isBuiltin) {
    // Check for weld proxy.
    if (providerClass.isSynthetic()) {
        providerClass = providerClass.getSuperclass();
    }
    Type exceptionType = Types.getActualTypeArgumentsOfAnInterface(providerClass, ExceptionMapper.class)[0];
    Utils.injectProperties(rpf, providerClass, provider);
    Class<?> exceptionClass = Types.getRawType(exceptionType);
    if (!Throwable.class.isAssignableFrom(exceptionClass)) {
        throw new RuntimeException(Messages.MESSAGES.incorrectTypeParameterExceptionMapper());
    }
    int priority = Utils.getPriority(null, null, ExceptionMapper.class, providerClass);
    SortedKey<ExceptionMapper> candidateExceptionMapper = new SortedKey<>(null, provider, providerClass, priority, isBuiltin);
    SortedKey<ExceptionMapper> registeredExceptionMapper;
    if (exceptionMappers != null) {
        if ((registeredExceptionMapper = exceptionMappers.get(exceptionClass)) != null && (candidateExceptionMapper.compareTo(registeredExceptionMapper) > 0)) {
            return;
        }
    }
    Map<Class<?>, SortedKey<ExceptionMapper>> mappers = getExceptionMappersForWrite();
    mappers.put(exceptionClass, candidateExceptionMapper);
    attachedExceptionMappers = false;
    exceptionMappers = mappers;
}
Also used : RuntimeType(jakarta.ws.rs.RuntimeType) Type(java.lang.reflect.Type) ExceptionMapper(jakarta.ws.rs.ext.ExceptionMapper)

Example 4 with ExceptionMapper

use of jakarta.ws.rs.ext.ExceptionMapper in project helidon by oracle.

the class ExtractQueryParamsTest method initClass.

@BeforeAll
public static void initClass() throws Throwable {
    Config config = Config.create();
    Security security = Security.create(config.get("security"));
    SecurityFeature feature = SecurityFeature.builder(security).config(config.get("security.jersey")).build();
    server = Routing.builder().register(JerseySupport.builder().register(BindingTest.MyResource.class).register(TestResource1.class).register(new TestResource2()).register(feature).register(new ExceptionMapper<Exception>() {

        @Override
        public Response toResponse(Exception exception) {
            exception.printStackTrace();
            return Response.serverError().build();
        }
    }).build()).build().createServer();
    CountDownLatch cdl = new CountDownLatch(1);
    AtomicReference<Throwable> th = new AtomicReference<>();
    server.start().whenComplete((webServer, throwable) -> {
        th.set(throwable);
        cdl.countDown();
    });
    cdl.await();
    if (th.get() != null) {
        throw th.get();
    }
    client = ClientBuilder.newClient();
    baseTarget = client.target(UriBuilder.fromUri("http://localhost/").port(server.port()).build());
}
Also used : ExceptionMapper(jakarta.ws.rs.ext.ExceptionMapper) Config(io.helidon.config.Config) AtomicReference(java.util.concurrent.atomic.AtomicReference) Security(io.helidon.security.Security) CountDownLatch(java.util.concurrent.CountDownLatch) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 5 with ExceptionMapper

use of jakarta.ws.rs.ext.ExceptionMapper in project minijax by minijax.

the class MinijaxProviders method getExceptionMapper.

@Override
@SuppressWarnings("unchecked")
public <T extends Throwable> ExceptionMapper<T> getExceptionMapper(final Class<T> type) {
    for (final Class<? extends ExceptionMapper<?>> exceptionMapperClass : context.getApplication().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)

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