Skip to content

Commit 1bf94a4

Browse files
author
Alvin Wang
committed
Make ExceptionHandler a class
1 parent 5735515 commit 1bf94a4

File tree

4 files changed

+10
-38
lines changed

4 files changed

+10
-38
lines changed

src/main/java/co/cask/http/DefaultExceptionHandler.java

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/main/java/co/cask/http/ExceptionHandler.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@
1717
package co.cask.http;
1818

1919
import org.jboss.netty.handler.codec.http.HttpRequest;
20+
import org.jboss.netty.handler.codec.http.HttpResponseStatus;
2021

2122
/**
2223
* Handles exceptions and provides a response via the {@link HttpResponder}.
2324
*/
24-
public interface ExceptionHandler {
25-
public void handle(Throwable t, HttpRequest request, HttpResponder responder);
25+
public class ExceptionHandler {
26+
public void handle(Throwable t, HttpRequest request, HttpResponder responder) {
27+
String message = String.format("Exception encountered while processing request : %s", t.getMessage());
28+
responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, message);
29+
}
2630
}

src/main/java/co/cask/http/NettyHttpService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,12 @@ public NettyHttpService(InetSocketAddress bindAddress, int bossThreadPoolSize, i
113113
this.rejectedExecutionHandler = rejectedExecutionHandler;
114114
this.channelGroup = new DefaultChannelGroup();
115115
this.resourceHandler = new HttpResourceHandler(httpHandlers, handlerHooks, urlRewriter,
116-
new DefaultExceptionHandler());
116+
new ExceptionHandler());
117117
this.handlerContext = new BasicHandlerContext(this.resourceHandler);
118118
this.httpChunkLimit = httpChunkLimit;
119119
this.pipelineModifier = null;
120120
this.sslHandlerFactory = null;
121-
this.exceptionHandler = new DefaultExceptionHandler();
121+
this.exceptionHandler = new ExceptionHandler();
122122
}
123123

124124
/**
@@ -342,7 +342,7 @@ protected Builder() {
342342
channelConfigs = Maps.newHashMap();
343343
channelConfigs.put("backlog", DEFAULT_CONNECTION_BACKLOG);
344344
sslHandlerFactory = null;
345-
exceptionHandler = new DefaultExceptionHandler();
345+
exceptionHandler = new ExceptionHandler();
346346
}
347347

348348
/**

src/test/java/co/cask/http/HttpServerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public static void setup() throws Exception {
7979
NettyHttpService.Builder builder = NettyHttpService.builder();
8080
builder.addHttpHandlers(handlers);
8181
builder.setHttpChunkLimit(75 * 1024);
82-
builder.setExceptionHandler(new DefaultExceptionHandler() {
82+
builder.setExceptionHandler(new ExceptionHandler() {
8383
@Override
8484
public void handle(Throwable t, HttpRequest request, HttpResponder responder) {
8585
if (t instanceof TestHandler.CustomException) {

0 commit comments

Comments
 (0)