|
17 | 17 |
|
18 | 18 | package org.openqa.selenium.grid.web;
|
19 | 19 |
|
20 |
| -import static com.google.common.net.MediaType.JSON_UTF_8; |
21 |
| -import static org.openqa.selenium.remote.http.Contents.asJson; |
22 |
| - |
23 | 20 | import org.openqa.selenium.internal.Require;
|
| 21 | +import org.openqa.selenium.json.Json; |
| 22 | +import org.openqa.selenium.remote.http.Filter; |
24 | 23 | import org.openqa.selenium.remote.http.HttpHandler;
|
25 |
| -import org.openqa.selenium.remote.http.HttpRequest; |
26 | 24 | import org.openqa.selenium.remote.http.HttpResponse;
|
27 | 25 |
|
28 |
| -import java.io.UncheckedIOException; |
| 26 | +import static org.openqa.selenium.remote.http.Contents.asJson; |
29 | 27 |
|
30 |
| -public class ErrorHandler implements HttpHandler { |
| 28 | +public class ErrorFilter implements Filter { |
31 | 29 |
|
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 | + } |
34 | 35 |
|
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); |
37 | 38 | }
|
38 | 39 |
|
39 | 40 | @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 | + }; |
46 | 53 | }
|
47 | 54 | }
|
0 commit comments