diff --git a/src/main/java/com/launchdarkly/eventsource/EventSource.java b/src/main/java/com/launchdarkly/eventsource/EventSource.java index ff302a4..8037bc9 100644 --- a/src/main/java/com/launchdarkly/eventsource/EventSource.java +++ b/src/main/java/com/launchdarkly/eventsource/EventSource.java @@ -261,7 +261,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..9307e8e 100644 --- a/src/main/java/com/launchdarkly/eventsource/UnsuccessfulResponseException.java +++ b/src/main/java/com/launchdarkly/eventsource/UnsuccessfulResponseException.java @@ -1,20 +1,33 @@ package com.launchdarkly.eventsource; +import okhttp3.Protocol; +import okhttp3.Request; +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 */ + @Deprecated public UnsuccessfulResponseException(int code) { - super("Unsuccessful response code received from stream: " + code); - this.code = code; + this(buildSurrogateResponse(code)); + } + + /** + * 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 +35,30 @@ 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; + } + + /** + * 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(); } }