From 63de63491f01e8c46e45ad740203039f029f3041 Mon Sep 17 00:00:00 2001 From: Neha Narkhede Date: Tue, 3 Feb 2015 14:15:19 -0800 Subject: [PATCH 1/2] Added RestServerErrorException to model exceptions that extend HTTP 500 --- .../exceptions/RestServerErrorException.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 core/src/main/java/io/confluent/rest/exceptions/RestServerErrorException.java diff --git a/core/src/main/java/io/confluent/rest/exceptions/RestServerErrorException.java b/core/src/main/java/io/confluent/rest/exceptions/RestServerErrorException.java new file mode 100644 index 0000000000..9393440434 --- /dev/null +++ b/core/src/main/java/io/confluent/rest/exceptions/RestServerErrorException.java @@ -0,0 +1,26 @@ +package io.confluent.rest.exceptions; + +import javax.ws.rs.ServerErrorException; +import javax.ws.rs.core.Response; + +public class RestServerErrorException extends ServerErrorException { + + private int errorCode; + + public RestServerErrorException(String message, int errorCode) { + this(message, errorCode, null); + } + + public RestServerErrorException(String message, int errorCode, Throwable cause) { + super(message, errorCode, cause); + this.errorCode = errorCode; + } + + public int getStatus() { + return Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(); + } + + public int getErrorCode() { + return errorCode; + } +} From 444899534f071512e45f32503284afed9f3f198e Mon Sep 17 00:00:00 2001 From: Neha Narkhede Date: Tue, 3 Feb 2015 15:29:05 -0800 Subject: [PATCH 2/2] Modified the ExceptionHandlingTest to test RestServerErrorException. Also RestServerErrorException now extends from RestException --- .../exceptions/RestServerErrorException.java | 16 ++-------------- .../io/confluent/rest/ExceptionHandlingTest.java | 5 +++-- 2 files changed, 5 insertions(+), 16 deletions(-) diff --git a/core/src/main/java/io/confluent/rest/exceptions/RestServerErrorException.java b/core/src/main/java/io/confluent/rest/exceptions/RestServerErrorException.java index 9393440434..efdf105728 100644 --- a/core/src/main/java/io/confluent/rest/exceptions/RestServerErrorException.java +++ b/core/src/main/java/io/confluent/rest/exceptions/RestServerErrorException.java @@ -1,26 +1,14 @@ package io.confluent.rest.exceptions; -import javax.ws.rs.ServerErrorException; import javax.ws.rs.core.Response; -public class RestServerErrorException extends ServerErrorException { - - private int errorCode; +public class RestServerErrorException extends RestException { public RestServerErrorException(String message, int errorCode) { this(message, errorCode, null); } public RestServerErrorException(String message, int errorCode, Throwable cause) { - super(message, errorCode, cause); - this.errorCode = errorCode; - } - - public int getStatus() { - return Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(); - } - - public int getErrorCode() { - return errorCode; + super(message, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), errorCode, cause); } } diff --git a/core/src/test/java/io/confluent/rest/ExceptionHandlingTest.java b/core/src/test/java/io/confluent/rest/ExceptionHandlingTest.java index 17bb4cf22f..2352be9dba 100644 --- a/core/src/test/java/io/confluent/rest/ExceptionHandlingTest.java +++ b/core/src/test/java/io/confluent/rest/ExceptionHandlingTest.java @@ -31,6 +31,7 @@ import io.confluent.rest.entities.ErrorMessage; import io.confluent.rest.exceptions.RestNotFoundException; +import io.confluent.rest.exceptions.RestServerErrorException; import static org.junit.Assert.assertEquals; @@ -86,7 +87,7 @@ public void testNonRestException() { public void testUnexpectedException() { // Under non-debug mode, this uses a completely generic message since unexpected errors // is the one case we want to be certain we don't leak extra info - testAppException("/unexpected", 500, 500, + testAppException("/unexpected", 500, 50001, Response.Status.INTERNAL_SERVER_ERROR.getReasonPhrase()); } @@ -122,7 +123,7 @@ public String notFound() { @GET @Path("/unexpected") public String unexpected() { - throw new RuntimeException("Internal server error."); + throw new RestServerErrorException("Internal Server Error", 50001); } }