@Override public void failure(RetrofitError error) { String json = new String(((TypedByteArray)error.getResponse().getBody()).getBytes()); Log.v("failure", json.toString()); }
@Override public void failure(RetrofitError exception) { Toast.makeText(getActivity(), exception.getMessage(), Toast.LENGTH_LONG).show(); Log.e(getClass().getSimpleName(), "Exception from Retrofit request to StackOverflow", exception); }
@Override public void failure(RetrofitError error) { error.printStackTrace(); System.err.println(error.getResponse().getUrl()); responseLatch.countDown(); } });
public class RetryStrategy implements Func1<Observable<? extends Throwable>, Observable<?>> { public RetryStrategy() {} @Override public Observable<?> call(Observable<? extends Throwable> attempts) { return attempts.flatMap((throwable) -> { if (throwable instanceof RetrofitError) { RetrofitError error = (RetrofitError) throwable; if (error.getKind() == RetrofitError.Kind.HTTP) { if (error.getResponse().getStatus() == 401) { // This is where you attempt to recover return someRecoveryObservable???; } } } // Bubble any other errors back up, e.g. connection loss. return Observable.error(throwable); }); } }
@Override public void failure(RetrofitError error) { try { if(error.getResponse().getStatus() == 401){ //your service for refreshing the token } else{ JsonElement json = (JsonElement) error.getBodyAs(JsonElement.class); String err = json.getAsJsonObject().get("err").getAsString(); } } catch (Exception e) { // handle the exception } }
@Override public Throwable handleError(RetrofitError cause) { Throwable returnThrowable = cause; if (cause.getKind() == RetrofitError.Kind.HTTP) { if (cause.getResponse() != null) { GitterApiErrorResponse errorResponse = (GitterApiErrorResponse) cause.getBodyAs(GitterApiErrorResponse.class); if (errorResponse != null) { returnThrowable = new GitterApiException(errorResponse); returnThrowable.setStackTrace(cause.getStackTrace()); } } } return returnThrowable; } });
@Override public Throwable handleError(RetrofitError error) { retrofit.client.Response r = error.getResponse(); if (r != null && r.getStatus() == 401) { Log.e("ErrorHandler", "---------> access deny code=401"); return new AccessDenyException(error.getMessage()); } else if (error.getKind() == RetrofitError.Kind.NETWORK) { Log.e("ErrorHandler", "---------> An IOException occurred while communicating to the server"); //return new NetworkException(cause.getMessage()); } else if (error.getKind() == RetrofitError.Kind.HTTP) { Log.e("ErrorHandler", "---------> A non-200 HTTP status code was received from the server"); //return new Non200HttpException(cause.getMessage()); } else if (error.getKind() == RetrofitError.Kind.CONVERSION) { Log.e("ErrorHandler", "---------> An exception was thrown while (de)serializing a body"); return new ConversionException(error.getMessage()); } else if (error.getKind() == RetrofitError.Kind.UNEXPECTED) { Log.e("ErrorHandler", "---------> An internal error occurred while attempting to execute a request. " + "It is best practice to re-throw this exception so your application crashes."); return new UnKnowException(error.getMessage()); } return error.getCause(); } }
public class RetrofitCallback<S> implements Callback<S> { private static final String TAG = RetrofitCallback.class.getSimpleName(); @Override public void success(S s, Response response) { } @Override public void failure(RetrofitError error) { Log.e(TAG, "Failed to make http request for: " + error.getUrl()); Response errorResponse = error.getResponse(); if (errorResponse != null) { Log.e(TAG, errorResponse.getReason()); if (errorResponse.getStatus() == 500) { Log.e(TAG, "Handle Server Errors Here"); } } } }
public class ErrorUtil implements ErrorHandler { public ErrorUtil() { } public Throwable handleError(RetrofitError cause) { String errorDescription = null; Log.e("NON FATAL ERROR: ", cause.getLocalizedMessage()); if (cause.getKind().equals(RetrofitError.Kind.NETWORK)) { if (!isNetworkAvailable()) { errorDescription = ErrorConstants.ERROR_CONNECTION_OFFLINE; } else if (cause.getMessage().contains("Unable to resolve host") || cause.getMessage().contains("Invalid sequence") || cause.getMessage().contains("Illegal character") || cause.getMessage().contains("was not verified")) { errorDescription = ErrorConstants.ERROR_UNABLE_TO_RESOLVE_HOST; } else if (cause.getLocalizedMessage().contains("timeout") || cause.getLocalizedMessage().contains("timed out") || cause.getLocalizedMessage().contains("failed to connect")) { errorDescription = ErrorConstants.ERROR_CONNECTION_TIMEOUT; } else if (cause.getLocalizedMessage().equals("Connection closed by peer") || cause.getLocalizedMessage().equals("host == null") || cause.getLocalizedMessage().contains("CertPathValidatorException")) { errorDescription = ErrorConstants.ERROR_UNABLE_TO_RESOLVE_HOST; } else { errorDescription = ErrorConstants.ERROR_NETWORK; } } else if (cause.getKind().equals(RetrofitError.Kind.CONVERSION)) { errorDescription = ErrorConstants.ERROR_JSON_RESPONSE_CONVERSION; } else if (cause.getResponse() == null) { errorDescription = ErrorConstants.ERROR_NO_RESPONSE; } else { errorDescription = ErrorConstants.ERROR_UNKNOWN; } Log.d("ERRORUTILReturn", errorDescription); return new Exception(errorDescription); }
final RetrofitError.Kind kind = retrofitError.getKind(); HodError hodError = (HodError) retrofitError.getBodyAs(HodError.class); final Response response = retrofitError.getResponse(); final int status = response.getStatus();
private UpstreamBadRequest(RetrofitError cause) { super(cause.getMessage(), cause); status = cause.getResponse().getStatus(); url = cause.getUrl(); }
private void register(final boolean legacy) { try { if (legacy) { collectorRegistrationService.legacyRegister(this.collectorId, this.collectorRegistrationRequest); } else { collectorRegistrationService.register(this.collectorId, this.collectorRegistrationRequest); } } catch (RetrofitError e) { final Response response = e.getResponse(); if (response != null) { if (!legacy && response.getStatus() == 404) { // Try again with the Graylog 1.x URL if we didn't try yet. register(true); } else { LOG.warn("Unable to send heartbeat to Graylog server, result was: {} - {}", response.getStatus(), response.getReason()); } } else { final String message; if (e.getCause() != null) { message = e.getCause().getClass().getSimpleName() + ": " + e.getCause().getMessage(); } else { message = e.getClass().getSimpleName() + ": " + e.getMessage(); } LOG.warn("Unable to send heartbeat to Graylog server: {}", message); } } }
private static String createExceptionMessage(RetrofitError retrofitError) { if (retrofitError.getMessage() != null) { return retrofitError.getMessage(); } if (retrofitError.getResponse() != null) { return "Status: " + retrofitError.getResponse().getStatus(); } return "unknown error"; }
String errorDescription; if (cause.isNetworkError()) { errorDescription = mContext.getString(R.string.error_network); } else { if (cause.getResponse() == null) { errorDescription = mContext.getString(R.string.error_no_response); } else { ErrorResponse errorResponse = (ErrorResponse) cause.getBodyAs(ErrorResponse.class); errorDescription = errorResponse.error.data.message; } catch (Exception ex) { try { errorDescription = mContext.getString(R.string.error_network_http_error, cause.getResponse().getStatus()); } catch (Exception ex2) { DebugTool.logD("handleError: " + ex2.getLocalizedMessage());
discard = true; } catch (RetrofitError e) { Log.e(TAG, "send: Request to " + e.getUrl() + " failed: " + e.getClass() + ": " + e.getLocalizedMessage()); if (e.getResponse() != null && e.getResponse().getStatus() == 500) { Log.e(TAG, "send: discarding oplog that has failed to be sent to " + e .getUrl()); discard = true;
public static boolean retry(Activity act, RetrofitError error, AtomicInteger retries, String uploadErrorMsg){ // this is the first half of the retry check Response r = error.getResponse(); if (r != null && r.getStatus() == STATUS_CODE_RETRY){ Log.v(TAG, "STATUS_CODE_RETRY!"); ... return true; } // this is the second half of the retry check if (error.isNetworkError()) { if (error.getCause() instanceof SocketTimeoutException) {//connection timeout check if(retries.incrementAndGet() < NUM_RETRIES){ // retry if you can return true; } else { // if you can't retry anymore retries.set(0); Log.i(TAG, act.getClass().getSimpleName() + " has no more retries " + act.getString(R.string.timeout_msg)); return false; } } else {//no connection check retries.set(0); Log.i(TAG, act.getClass().getSimpleName() + " " + act.getString(R.string.timeout_msg)+ " " + uploadErrorMsg); return false; } } else { //non network error check retries.set(0); Log.i(TAG, act.getClass().getSimpleName() + " " + act.getString(R.string.err_msg)+ " " + uploadErrorMsg); return false; } }
@Override public void failure(RetrofitError error) { setProgressBarIndeterminateVisibility(false); if (error.getResponse() == null) { showDialog(error.getCause().getMessage()); } else { showDialog("Unable to get a client token. Response Code: " + error.getResponse().getStatus() + " Response body: " + error.getResponse().getBody()); } } });
stepMilli, 0, true, program); } catch (RetrofitError e) { ErrorResponse errorResponse = (ErrorResponse) e.getBodyAs(ErrorResponse.class); throw new SignalFxRequestError(errorResponse, program, startEpochMilli, endEpochMilli, stepMilli, metricsAccountName);
if (e.getCause() instanceof ConnectException) { AnsiUi.error(e.getCause().getMessage()); AnsiUi.remediation("Is your daemon running?"); System.exit(1); AnsiUi.error(e.getMessage()); AnsiUi.remediation("Try the command again with the --debug flag."); System.exit(1);
public int getStatusCode(RetrofitError error) { if (error.isNetworkError()) { return 503; // Use another code if you'd prefer } return error.getResponse().getStatus(); }