Skip to content

Commit 0197256

Browse files
committed
Replace ErrorHandler with a Filter, which makes more sense
1 parent 0e0194b commit 0197256

File tree

2 files changed

+26
-24
lines changed

2 files changed

+26
-24
lines changed

java/server/src/org/openqa/selenium/grid/web/ErrorHandler.java renamed to java/server/src/org/openqa/selenium/grid/web/ErrorFilter.java

+23-16
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,38 @@
1717

1818
package org.openqa.selenium.grid.web;
1919

20-
import static com.google.common.net.MediaType.JSON_UTF_8;
21-
import static org.openqa.selenium.remote.http.Contents.asJson;
22-
2320
import org.openqa.selenium.internal.Require;
21+
import org.openqa.selenium.json.Json;
22+
import org.openqa.selenium.remote.http.Filter;
2423
import org.openqa.selenium.remote.http.HttpHandler;
25-
import org.openqa.selenium.remote.http.HttpRequest;
2624
import org.openqa.selenium.remote.http.HttpResponse;
2725

28-
import java.io.UncheckedIOException;
26+
import static org.openqa.selenium.remote.http.Contents.asJson;
2927

30-
public class ErrorHandler implements HttpHandler {
28+
public class ErrorFilter implements Filter {
3129

32-
private final Throwable throwable;
33-
private final ErrorCodec errors = ErrorCodec.createDefault();
30+
private final ErrorCodec errors;
31+
32+
public ErrorFilter() {
33+
this(ErrorCodec.createDefault());
34+
}
3435

35-
public ErrorHandler(Throwable throwable) {
36-
this.throwable = Require.nonNull("Exception", throwable);
36+
public ErrorFilter(ErrorCodec errors) {
37+
this.errors = Require.nonNull("Error codec", errors);
3738
}
3839

3940
@Override
40-
public HttpResponse execute(HttpRequest req) throws UncheckedIOException {
41-
return new HttpResponse()
42-
.setHeader("Cache-Control", "none")
43-
.setHeader("Content-Type", JSON_UTF_8.toString())
44-
.setStatus(errors.getHttpStatusCode(throwable))
45-
.setContent(asJson(errors.encode(throwable)));
41+
public HttpHandler apply(HttpHandler next) {
42+
return req -> {
43+
try {
44+
return next.execute(req);
45+
} catch (Throwable throwable) {
46+
return new HttpResponse()
47+
.setHeader("Cache-Control", "none")
48+
.setHeader("Content-Type", Json.JSON_UTF_8)
49+
.setStatus(errors.getHttpStatusCode(throwable))
50+
.setContent(asJson(errors.encode(throwable)));
51+
}
52+
};
4653
}
4754
}

java/server/src/org/openqa/selenium/netty/server/SeleniumHandler.java

+3-8
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
import io.netty.channel.ChannelHandlerContext;
2121
import io.netty.channel.SimpleChannelInboundHandler;
22-
import org.openqa.selenium.grid.web.ErrorHandler;
22+
import org.openqa.selenium.grid.web.ErrorFilter;
2323
import org.openqa.selenium.internal.Require;
2424
import org.openqa.selenium.remote.http.HttpHandler;
2525
import org.openqa.selenium.remote.http.HttpRequest;
@@ -35,18 +35,13 @@ class SeleniumHandler extends SimpleChannelInboundHandler<HttpRequest> {
3535

3636
public SeleniumHandler(HttpHandler seleniumHandler) {
3737
super(HttpRequest.class);
38-
this.seleniumHandler = Require.nonNull("HTTP handler", seleniumHandler);
38+
this.seleniumHandler = Require.nonNull("HTTP handler", seleniumHandler).with(new ErrorFilter());
3939
}
4040

4141
@Override
4242
protected void channelRead0(ChannelHandlerContext ctx, HttpRequest msg) {
4343
EXECUTOR.submit(() -> {
44-
HttpResponse res;
45-
try {
46-
res = seleniumHandler.execute(msg);
47-
} catch (Throwable e) {
48-
res = new ErrorHandler(e).execute(msg);
49-
}
44+
HttpResponse res = seleniumHandler.execute(msg);
5045
ctx.writeAndFlush(res);
5146
});
5247
}

0 commit comments

Comments
 (0)