Skip to content

Commit

Permalink
Type info for ExceptionHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan-Willem Gmelig Meyling authored and jwgmeligmeyling committed Apr 9, 2017
1 parent ddda7e5 commit 5755cf9
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/main/java/spark/ExceptionHandler.java
Expand Up @@ -4,7 +4,7 @@
* Created by Per Wendel on 2014-05-10. * Created by Per Wendel on 2014-05-10.
*/ */
@FunctionalInterface @FunctionalInterface
public interface ExceptionHandler { public interface ExceptionHandler<T extends Exception> {


/** /**
* Invoked when an exception that is mapped to this handler occurs during routing * Invoked when an exception that is mapped to this handler occurs during routing
Expand All @@ -13,5 +13,5 @@ public interface ExceptionHandler {
* @param request The request object providing information about the HTTP request * @param request The request object providing information about the HTTP request
* @param response The response object providing functionality for modifying the response * @param response The response object providing functionality for modifying the response
*/ */
void handle(Exception exception, Request request, Response response); void handle(T exception, Request request, Response response);
} }
13 changes: 7 additions & 6 deletions src/main/java/spark/ExceptionHandlerImpl.java
Expand Up @@ -16,18 +16,19 @@
*/ */
package spark; package spark;


public abstract class ExceptionHandlerImpl implements ExceptionHandler { public abstract class ExceptionHandlerImpl<T extends Exception> implements ExceptionHandler<T> {

/** /**
* Holds the type of exception that this filter will handle * Holds the type of exception that this filter will handle
*/ */
protected Class<? extends Exception> exceptionClass; protected Class<? extends T> exceptionClass;


/** /**
* Initializes the filter with the provided exception type * Initializes the filter with the provided exception type
* *
* @param exceptionClass Type of exception * @param exceptionClass Type of exception
*/ */
public ExceptionHandlerImpl(Class<? extends Exception> exceptionClass) { public ExceptionHandlerImpl(Class<T> exceptionClass) {
this.exceptionClass = exceptionClass; this.exceptionClass = exceptionClass;
} }


Expand All @@ -36,7 +37,7 @@ public ExceptionHandlerImpl(Class<? extends Exception> exceptionClass) {
* *
* @return Type of exception * @return Type of exception
*/ */
public Class<? extends Exception> exceptionClass() { public Class<? extends T> exceptionClass() {
return this.exceptionClass; return this.exceptionClass;
} }


Expand All @@ -45,7 +46,7 @@ public Class<? extends Exception> exceptionClass() {
* *
* @param exceptionClass Type of exception * @param exceptionClass Type of exception
*/ */
public void exceptionClass(Class<? extends Exception> exceptionClass) { public void exceptionClass(Class<? extends T> exceptionClass) {
this.exceptionClass = exceptionClass; this.exceptionClass = exceptionClass;
} }


Expand All @@ -56,5 +57,5 @@ public void exceptionClass(Class<? extends Exception> exceptionClass) {
* @param request The request object providing information about the HTTP request * @param request The request object providing information about the HTTP request
* @param response The response object providing functionality for modifying the response * @param response The response object providing functionality for modifying the response
*/ */
public abstract void handle(Exception exception, Request request, Response response); public abstract void handle(T exception, Request request, Response response);
} }
6 changes: 3 additions & 3 deletions src/main/java/spark/Service.java
Expand Up @@ -540,11 +540,11 @@ private void initializeRouteMatcher() {
* @param exceptionClass the exception class * @param exceptionClass the exception class
* @param handler The handler * @param handler The handler
*/ */
public synchronized void exception(Class<? extends Exception> exceptionClass, ExceptionHandler handler) { public synchronized <T extends Exception> void exception(Class<T> exceptionClass, ExceptionHandler<? super T> handler) {
// wrap // wrap
ExceptionHandlerImpl wrapper = new ExceptionHandlerImpl(exceptionClass) { ExceptionHandlerImpl wrapper = new ExceptionHandlerImpl<T>(exceptionClass) {
@Override @Override
public void handle(Exception exception, Request request, Response response) { public void handle(T exception, Request request, Response response) {
handler.handle(exception, request, response); handler.handle(exception, request, response);
} }
}; };
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/spark/Spark.java
Expand Up @@ -879,7 +879,7 @@ public static void patch(String path,
* @param exceptionClass the exception class * @param exceptionClass the exception class
* @param handler The handler * @param handler The handler
*/ */
public static void exception(Class<? extends Exception> exceptionClass, ExceptionHandler handler) { public static <T extends Exception> void exception(Class<T> exceptionClass, ExceptionHandler<? super T> handler) {
getInstance().exception(exceptionClass, handler); getInstance().exception(exceptionClass, handler);
} }


Expand Down

0 comments on commit 5755cf9

Please sign in to comment.