Skip to content

Commit

Permalink
NotAllowedExceptionMapper wrongly ported to JAX-RS 1.1
Browse files Browse the repository at this point in the history
Previous version of RestEasy has not Status#NOT_ALLOWED constant. But NotAllowedExceptionMapper should not use the BAD_REQUEST status.

Also, I had left some error text comparisons.
  • Loading branch information
tsegismont committed Aug 12, 2015
1 parent 44b0ca3 commit 75548fa
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.Response.Status;

import org.hawkular.metrics.api.jaxrs.ApiError;
Expand All @@ -31,15 +32,26 @@
*/
public class ExceptionMapperUtils {
private static final Logger LOG = LoggerFactory.getLogger(ExceptionMapperUtils.class);
private ExceptionMapperUtils(){

public static Response buildResponse(Throwable exception, int statusCode) {
ResponseBuilder responseBuilder = Response.status(statusCode);
return buildErrorResponse(exception, responseBuilder);
}

public static Response buildResponse(Throwable exception, Status status){
public static Response buildResponse(Throwable exception, Status status) {
ResponseBuilder responseBuilder = Response.status(status);
return buildErrorResponse(exception, responseBuilder);
}

private static Response buildErrorResponse(Throwable exception, ResponseBuilder responseBuilder) {
LOG.trace("RestEasy exception,", exception);
return Response.status(status)
return responseBuilder
.entity(new ApiError(Throwables.getRootCause(exception).getMessage()))
.type(MediaType.APPLICATION_JSON_TYPE)
.build();
}

private ExceptionMapperUtils() {
// Utility class
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class NotAllowedExceptionMapper implements ExceptionMapper<MethodNotAllow

@Override
public Response toResponse(MethodNotAllowedException exception) {
return ExceptionMapperUtils.buildResponse(exception, Response.Status.BAD_REQUEST);
return ExceptionMapperUtils.buildResponse(exception, 405);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ public void testNotAllowedException() {
.header(TENANT_HEADER_NAME, "test")
.post(null);
assertEquals(405, response.getStatus());
ApiErrorJson apiErrorJson = response.readEntity(ApiErrorJson.class);
assertEquals("No resource method found for POST, return 405 with Allow header", apiErrorJson.getErrorMsg());
}

@Test
Expand All @@ -71,8 +69,6 @@ public void testNumberFormatException() {
.header(TENANT_HEADER_NAME, "test")
.get();
assertEquals(400, response.getStatus());
ApiErrorJson apiErrorJson = response.readEntity(ApiErrorJson.class);
assertEquals("For input string: \"999999999999999999999999\"", apiErrorJson.getErrorMsg());
}

@Test
Expand Down

0 comments on commit 75548fa

Please sign in to comment.