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;
}
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;
}
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;
}
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());
}
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;
}
Aggregations