Skip to content

Commit

Permalink
Unify HttpException into a single shared type.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jawnnypoo authored and JakeWharton committed Feb 13, 2017
1 parent 78e0cd8 commit 4546d5d
Show file tree
Hide file tree
Showing 21 changed files with 89 additions and 167 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
* There are two configurations supported for the {@code ListenableFuture} type parameter:
* <ul>
* <li>Direct body (e.g., {@code ListenableFuture<User>}) returns the deserialized body for 2XX
* responses, sets {@link HttpException} errors for non-2XX responses, and sets {@link IOException}
* for network errors.</li>
* responses, sets {@link retrofit2.HttpException HttpException} errors for non-2XX responses, and
* sets {@link IOException} for network errors.</li>
* <li>Response wrapped body (e.g., {@code ListenableFuture<Response<User>>}) returns a
* {@link Response} object for all HTTP responses and sets {@link IOException} for network
* errors</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,10 @@

import retrofit2.Response;

/** Exception for an unexpected, non-2xx HTTP response. */
public final class HttpException extends Exception {
private final int code;
private final String message;
private final transient Response<?> response;

/** @deprecated Use {@link retrofit2.HttpException}. */
@Deprecated
public final class HttpException extends retrofit2.HttpException {
public HttpException(Response<?> response) {
super("HTTP " + response.code() + " " + response.message());
this.code = response.code();
this.message = response.message();
this.response = response;
}

/** HTTP status code. */
public int code() {
return code;
}

/** HTTP status message. */
public String message() {
return message;
}

/**
* The full HTTP response. This may be null if the exception was serialized.
*/
public Response<?> response() {
return response;
super(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ interface Service {
future.get();
fail();
} catch (ExecutionException e) {
assertThat(e.getCause()).isInstanceOf(HttpException.class)
assertThat(e.getCause())
.isInstanceOf(HttpException.class) // Required for backwards compatibility.
.isInstanceOf(retrofit2.HttpException.class)
.hasMessage("HTTP 404 Client Error");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,10 @@

import retrofit2.Response;

/** Exception for an unexpected, non-2xx HTTP response. */
public final class HttpException extends Exception {
private final int code;
private final String message;
private final transient Response<?> response;

/** @deprecated Use {@link retrofit2.HttpException}. */
@Deprecated
public final class HttpException extends retrofit2.HttpException {
public HttpException(Response<?> response) {
super("HTTP " + response.code() + " " + response.message());
this.code = response.code();
this.message = response.message();
this.response = response;
}

/** HTTP status code. */
public int code() {
return code;
}

/** HTTP status message. */
public String message() {
return message;
}

/**
* The full HTTP response. This may be null if the exception was serialized.
*/
public Response<?> response() {
return response;
super(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
* There are two configurations supported for the {@code CompletableFuture} type parameter:
* <ul>
* <li>Direct body (e.g., {@code CompletableFuture<User>}) returns the deserialized body for 2XX
* responses, sets {@link HttpException} errors for non-2XX responses, and sets {@link IOException}
* for network errors.</li>
* responses, sets {@link retrofit2.HttpException HttpException} errors for non-2XX responses, and
* sets {@link IOException} for network errors.</li>
* <li>Response wrapped body (e.g., {@code CompletableFuture<Response<User>>}) returns a
* {@link Response} object for all HTTP responses and sets {@link IOException} for network
* errors</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ interface Service {
future.get();
fail();
} catch (ExecutionException e) {
assertThat(e.getCause()).isInstanceOf(HttpException.class)
assertThat(e.getCause())
.isInstanceOf(HttpException.class) // Required for backwards compatibility.
.isInstanceOf(retrofit2.HttpException.class)
.hasMessage("HTTP 404 Client Error");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,10 @@

import retrofit2.Response;

/** Exception for an unexpected, non-2xx HTTP response. */
public final class HttpException extends Exception {
private static String getMessage(Response<?> response) {
if (response == null) throw new NullPointerException("response == null");
return "HTTP " + response.code() + " " + response.message();
}

private final int code;
private final String message;
private final transient Response<?> response;

/** @deprecated Use {@link retrofit2.HttpException}. */
@Deprecated
public final class HttpException extends retrofit2.HttpException {
public HttpException(Response<?> response) {
super(getMessage(response));
this.code = response.code();
this.message = response.message();
this.response = response;
}

/** HTTP status code. */
public int code() {
return code;
}

/** HTTP status message. */
public String message() {
return message;
}

/**
* The full HTTP response. This may be null if the exception was serialized.
*/
public Response<?> response() {
return response;
super(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import retrofit2.CallAdapter;
import retrofit2.HttpException;
import retrofit2.Response;
import retrofit2.Retrofit;
import rx.Completable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ interface Service {

RecordingSubscriber<Void> subscriber = subscriberRule.create();
service.completable().unsafeSubscribe(subscriber);
// Required for backwards compatibility.
subscriber.assertError(HttpException.class, "HTTP 404 Client Error");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ interface Service {

RecordingSubscriber<String> subscriber = subscriberRule.create();
service.body().unsafeSubscribe(subscriber);
// Required for backwards compatibility.
subscriber.assertError(HttpException.class, "HTTP 404 Client Error");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ interface Service {

RecordingSubscriber<String> subscriber = subscriberRule.create();
service.body().unsafeSubscribe(subscriber);
// Required for backwards compatibility.
subscriber.assertError(HttpException.class, "HTTP 404 Client Error");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,10 @@

import retrofit2.Response;

/** Exception for an unexpected, non-2xx HTTP response. */
public final class HttpException extends Exception {
private static String getMessage(Response<?> response) {
if (response == null) throw new NullPointerException("response == null");
return "HTTP " + response.code() + " " + response.message();
}

private final int code;
private final String message;
private final transient Response<?> response;

/** @deprecated Use {@link retrofit2.HttpException}. */
@Deprecated
public final class HttpException extends retrofit2.HttpException {
public HttpException(Response<?> response) {
super(getMessage(response));
this.code = response.code();
this.message = response.message();
this.response = response;
}

/** HTTP status code. */
public int code() {
return code;
}

/** HTTP status message. */
public String message() {
return message;
}

/**
* The full HTTP response. This may be null if the exception was serialized.
*/
public Response<?> response() {
return response;
super(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import retrofit2.CallAdapter;
import retrofit2.HttpException;
import retrofit2.Response;
import retrofit2.Retrofit;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ interface Service {

RecordingCompletableObserver observer = observerRule.create();
service.completable().subscribe(observer);
// Required for backwards compatibility.
observer.assertError(HttpException.class, "HTTP 404 Client Error");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ interface Service {

RecordingSubscriber<String> subscriber = subscriberRule.create();
service.body().subscribe(subscriber);
// Required for backwards compatibility.
subscriber.assertError(HttpException.class, "HTTP 404 Client Error");
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ interface Service {

RecordingMaybeObserver<String> observer = observerRule.create();
service.body().subscribe(observer);
// Required for backwards compatibility.
observer.assertError(HttpException.class, "HTTP 404 Client Error");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ interface Service {

RecordingObserver<String> observer = observerRule.create();
service.body().subscribe(observer);
// Required for backwards compatibility.
observer.assertError(HttpException.class, "HTTP 404 Client Error");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ interface Service {

RecordingSingleObserver<String> observer = observerRule.create();
service.body().subscribe(observer);
// Required for backwards compatibility.
observer.assertError(HttpException.class, "HTTP 404 Client Error");
}

Expand Down
52 changes: 52 additions & 0 deletions retrofit/src/main/java/retrofit2/HttpException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (C) 2016 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package retrofit2;

/** Exception for an unexpected, non-2xx HTTP response. */
public class HttpException extends Exception {
private static String getMessage(Response<?> response) {
if (response == null) throw new NullPointerException("response == null");
return "HTTP " + response.code() + " " + response.message();
}

private final int code;
private final String message;
private final transient Response<?> response;

public HttpException(Response<?> response) {
super(getMessage(response));
this.code = response.code();
this.message = response.message();
this.response = response;
}

/** HTTP status code. */
public int code() {
return code;
}

/** HTTP status message. */
public String message() {
return message;
}

/**
* The full HTTP response. This may be null if the exception was serialized.
*/
public Response<?> response() {
return response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package retrofit2.adapter.rxjava;
package retrofit2;

import org.junit.Test;
import retrofit2.Response;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
Expand Down

0 comments on commit 4546d5d

Please sign in to comment.