Skip to content

Commit

Permalink
Default Exception Mapper re-worked (#4908)
Browse files Browse the repository at this point in the history
Signed-off-by: Maxim Nesen <[email protected]>
  • Loading branch information
senivam authored Dec 2, 2021
1 parent fee1c32 commit b7a3946
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ public class ExceptionMapperFactory implements ExceptionMappers {

private static final Logger LOGGER = Logger.getLogger(ExceptionMapperFactory.class.getName());

private static final ExceptionMapper<Throwable> DEFAULT_EXCEPTION_MAPPER = new DefaultExceptionMapper();

/**
* Configurator which initializes and register {@link ExceptionMappers} instance into {@link InjectionManager} and
* {@link BootstrapBag}.
Expand Down Expand Up @@ -129,7 +127,7 @@ private <T extends Throwable> ExceptionMapper<T> find(final Class<T> type, final
}
}
}
return mapper == null ? (ExceptionMapper<T>) DEFAULT_EXCEPTION_MAPPER : mapper;
return mapper;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.jersey.internal;
package org.glassfish.jersey.server;

import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.ExceptionMapper;

public class DefaultExceptionMapper implements ExceptionMapper<Throwable> {
class DefaultExceptionMapper implements ExceptionMapper<Throwable> {
@Override
public Response toResponse(Throwable exception) {
return (exception instanceof WebApplicationException)
Expand Down
176 changes: 106 additions & 70 deletions core-server/src/main/java/org/glassfish/jersey/server/ServerRuntime.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ error.closing.commit.output.stream=Error while closing the output stream in orde
error.closing.finder=Error while closing {0} resource finder.
error.exception.mapping.original.exception=An exception mapping did not successfully produce and processed a response. Logging the original error.
error.exception.mapping.processed.response.error=A response error mapping did not successfully produce and processed a response.
error.exception.mapping.thrown.to.container=An exception mapping did not successfully produce and processed a response. Logging the exception propagated to the container.
error.exception.mapping.thrown.to.container=An exception mapping did not successfully produce and processed a response. Logging the exception propagated to the default exception mapper.
error.marshalling.jaxb=Error marshalling JAXB object of type "{0}".
error.msg=ERROR: {0}
error.monitoring.statistics.generation=Error generating monitoring statistics.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -558,14 +558,12 @@ public void testMapResponseErrorForMbw() throws Exception {
/**
* Test that un-mapped response errors are tried to be processed only once (MBW).
*/
@Test
@Test(expected = ExecutionException.class)
public void testMapCyclicResponseErrorForMbw() throws Exception {
final ApplicationHandler handler = new ApplicationHandler(MapResponseErrorApplication.class);

final ContainerRequest context = RequestContextBuilder.from("/foobar", "GET").build();

final ContainerResponse containerResponse = handler.apply(context).get();
assertEquals(500, containerResponse.getStatus());
assertEquals("Cannot do that!", containerResponse.getEntity());
handler.apply(context).get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@

import jakarta.inject.Singleton;

import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
Expand Down Expand Up @@ -72,7 +71,6 @@ public void testCompletionCallback() throws ExecutionException, InterruptedExcep
}

@Test
@Ignore("since JAXRS 3.1 failure is not processed by callbacks")
public void testCompletionFail() throws ExecutionException, InterruptedException {
final Flags flags = new Flags();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,11 @@ public Response apply(final ContainerRequestContext request) {
});
resourceConfig.registerResources(rb.build());
final ApplicationHandler application = new ApplicationHandler(resourceConfig);
int status = application.apply(RequestContextBuilder.from("/test", "GET").build()).get().getStatus();
Assert.assertEquals(500, status);
try {
application.apply(RequestContextBuilder.from("/test", "GET").build()).get().getStatus();
Assert.fail("should throw an exception");
} catch (final Exception e) {
// ok
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -278,8 +278,8 @@ public void test() {
LocalizationMessages.ERROR_EXCEPTION_MAPPING_ORIGINAL_EXCEPTION(),
LocalizationMessages.ERROR_EXCEPTION_MAPPING_THROWN_TO_CONTAINER()}) {

if (logRecord.getMessage().contains(message) && logRecord.getLevel().intValue() > Level.FINE.intValue()) {
fail("Log message should be logged at lower (FINE) level: " + message);
if (logRecord.getMessage().contains(message) && logRecord.getLevel().intValue() > Level.WARNING.intValue()) {
fail("Log message should be logged at lower (WARNING) level: " + message);
}
}
}
Expand Down

0 comments on commit b7a3946

Please sign in to comment.