Last active
March 28, 2016 08:16
-
-
Save ytRino/e0055ec03458a8ba7916 to your computer and use it in GitHub Desktop.
RetrofitSingleSubscriber
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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