Skip to content

Instantly share code, notes, and snippets.

@ytRino
Last active March 28, 2016 08:16
Show Gist options
  • Save ytRino/e0055ec03458a8ba7916 to your computer and use it in GitHub Desktop.
Save ytRino/e0055ec03458a8ba7916 to your computer and use it in GitHub Desktop.
RetrofitSingleSubscriber
public abstract class RetrofitSingleSubscriber<T> extends SingleSubscriber<T> {
public abstract void onHttpError(HttpException e);
public abstract void onNonHttpError(Throwable e);
@Override public void onError(Throwable error) {
if (error instanceof HttpException) {
onHttpError((HttpException) error);
} else {
onNonHttpError(error);
}
}
}
public abstract class RetrofitSingleSubscriber<T> extends SingleSubscriber<T> {
private final Converter<ResponseBody, ErrorResponse> converter;
public RetrofitSingleSubscriber(Converter<ResponseBody, ErrorResponse> converter) {
this.converter = converter;
}
public abstract void onHttpError(HttpException e, ErrorResponse response);
public abstract void onNonHttpError(Throwable e);
@Override public void onError(Throwable error) {
if (error instanceof HttpException) {
final HttpException hex = (HttpException) error;
onHttpError(hex, getErrorResponse(hex));
} else {
onNonHttpError(error);
}
}
private ErrorResponse getErrorResponse(HttpException ex) {
try {
return converter.convert(ex.response().errorBody());
} catch (IOException e) {
return null;
}
}
}
public abstract class RetrofitSingleSubscriber<T> extends SingleSubscriber<T> {
public abstract void onHttpError(HttpException e, ErrorResponse response);
public abstract void onNonHttpError(Throwable e);
@Override public void onError(Throwable error) {
if (error instanceof HttpException) {
final HttpException hex = (HttpException) error;
onHttpError(hex, getErrorResponse(hex));
} else {
onNonHttpError(error);
}
}
private ErrorResponse getErrorResponse(HttpException ex) {
try {
return new Gson().fromJson(ex.response().errorBody().string(), ErrorResponse.class);
} catch (IOException e) {
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment