From 859e14fb660ce405e226f9e5c9c475ea934099d4 Mon Sep 17 00:00:00 2001 From: Alexey Pakseykin Date: Fri, 13 Sep 2019 18:14:51 +0800 Subject: [PATCH 1/2] Propagate server response to exception --- gradle.properties | 2 +- .../launchdarkly/eventsource/EventSource.java | 2 +- .../UnsuccessfulResponseException.java | 26 ++++++++++++++++--- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/gradle.properties b/gradle.properties index 42661f9..aa7f3fa 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,3 @@ -version=1.10.0 +version=1.11.0-SNAPSHOT ossrhUsername= ossrhPassword= diff --git a/src/main/java/com/launchdarkly/eventsource/EventSource.java b/src/main/java/com/launchdarkly/eventsource/EventSource.java index 0619cf8..25651d8 100644 --- a/src/main/java/com/launchdarkly/eventsource/EventSource.java +++ b/src/main/java/com/launchdarkly/eventsource/EventSource.java @@ -256,7 +256,7 @@ private void connect() { } } else { logger.debug("Unsuccessful Response: " + response); - errorHandlerAction = dispatchError(new UnsuccessfulResponseException(response.code())); + errorHandlerAction = dispatchError(new UnsuccessfulResponseException(response)); } } catch (EOFException eofe) { logger.warn("Connection unexpectedly closed."); diff --git a/src/main/java/com/launchdarkly/eventsource/UnsuccessfulResponseException.java b/src/main/java/com/launchdarkly/eventsource/UnsuccessfulResponseException.java index 40b5a24..799ef72 100644 --- a/src/main/java/com/launchdarkly/eventsource/UnsuccessfulResponseException.java +++ b/src/main/java/com/launchdarkly/eventsource/UnsuccessfulResponseException.java @@ -1,20 +1,30 @@ package com.launchdarkly.eventsource; +import okhttp3.Response; + /** * Exception class that means the remote server returned an HTTP error. */ @SuppressWarnings("serial") public class UnsuccessfulResponseException extends Exception { - private final int code; + private final Response response; /** * Constructs an exception instance. * @param code the HTTP status */ public UnsuccessfulResponseException(int code) { - super("Unsuccessful response code received from stream: " + code); - this.code = code; + this(new Response.Builder().code(code).build()); + } + + /** + * Constructs an exception instance. + * @param response {@link Response} provided by server + */ + public UnsuccessfulResponseException(Response response) { + super("Unsuccessful response code received from stream: " + response.code()); + this.response = response; } /** @@ -22,6 +32,14 @@ public UnsuccessfulResponseException(int code) { * @return the HTTP status */ public int getCode() { - return code; + return response.code(); + } + + /** + * Returns the HTTP {@link Response}. + * @return the HTTP {@link Response} + */ + public Response getResponse() { + return response; } } From 0991ec2ab557eb67733190b83f6a25c4b5dee917 Mon Sep 17 00:00:00 2001 From: Alexey Pakseykin Date: Fri, 13 Sep 2019 20:13:23 +0800 Subject: [PATCH 2/2] Build surrogate response for a given code --- .../UnsuccessfulResponseException.java | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/launchdarkly/eventsource/UnsuccessfulResponseException.java b/src/main/java/com/launchdarkly/eventsource/UnsuccessfulResponseException.java index 799ef72..9307e8e 100644 --- a/src/main/java/com/launchdarkly/eventsource/UnsuccessfulResponseException.java +++ b/src/main/java/com/launchdarkly/eventsource/UnsuccessfulResponseException.java @@ -1,5 +1,7 @@ package com.launchdarkly.eventsource; +import okhttp3.Protocol; +import okhttp3.Request; import okhttp3.Response; /** @@ -14,8 +16,9 @@ public class UnsuccessfulResponseException extends Exception { * Constructs an exception instance. * @param code the HTTP status */ + @Deprecated public UnsuccessfulResponseException(int code) { - this(new Response.Builder().code(code).build()); + this(buildSurrogateResponse(code)); } /** @@ -42,4 +45,20 @@ public int getCode() { public Response getResponse() { return response; } + + /** + * Used for backward compatibility only: provide surrogate response for a given code + */ + @Deprecated + private static Response buildSurrogateResponse(int code) { + return new Response.Builder() + .request(new Request.Builder() + .url("http://example.com") + .build() + ) + .protocol(Protocol.HTTP_1_1) + .message("") + .code(code) + .build(); + } }