Example usage for com.google.api.client.googleapis.json GoogleJsonError getCode

List of usage examples for com.google.api.client.googleapis.json GoogleJsonError getCode

Introduction

In this page you can find the example usage for com.google.api.client.googleapis.json GoogleJsonError getCode.

Prototype

public final int getCode() 

Source Link

Document

Returns the HTTP status code of this response or null for none.

Usage

From source file:ch.cyberduck.core.googledrive.DriveBatchDeleteFeature.java

License:Open Source License

@Override
public void delete(final List<Path> files, final LoginCallback prompt, final Callback callback)
        throws BackgroundException {
    final BatchRequest batch = session.getClient().batch();
    final List<BackgroundException> failures = new ArrayList<>();
    for (Path file : files) {
        try {//  w w  w. j a v  a2  s. c  o  m
            session.getClient().files().delete(new DriveFileidProvider(session).getFileid(file)).queue(batch,
                    new JsonBatchCallback<Void>() {
                        @Override
                        public void onFailure(final GoogleJsonError e, final HttpHeaders responseHeaders)
                                throws IOException {
                            log.warn(String.format("Failure deleting %s. %s", file, e.getMessage()));
                            failures.add(new HttpResponseExceptionMappingService()
                                    .map(new HttpResponseException(e.getCode(), e.getMessage())));
                        }

                        @Override
                        public void onSuccess(final Void aVoid, final HttpHeaders responseHeaders)
                                throws IOException {
                            callback.delete(file);
                        }
                    });
        } catch (IOException e) {
            throw new DriveExceptionMappingService().map("Cannot delete {0}", e, file);
        }
    }
    try {
        batch.execute();
    } catch (IOException e) {
        throw new DriveExceptionMappingService().map(e);
    }
    for (BackgroundException e : failures) {
        throw e;
    }
}

From source file:com.google.cloud.BaseServiceException.java

License:Open Source License

public BaseServiceException(IOException exception, boolean idempotent) {
    super(message(exception), exception);
    int code = UNKNOWN_CODE;
    String reason = null;/*  ww  w  .  ja  va 2  s .c  o  m*/
    String location = null;
    String debugInfo = null;
    Boolean retryable = null;
    if (exception instanceof GoogleJsonResponseException) {
        GoogleJsonError jsonError = ((GoogleJsonResponseException) exception).getDetails();
        if (jsonError != null) {
            Error error = new Error(jsonError.getCode(), reason(jsonError));
            code = error.code;
            reason = error.reason;
            retryable = isRetryable(idempotent, error);
            if (reason != null) {
                GoogleJsonError.ErrorInfo errorInfo = jsonError.getErrors().get(0);
                location = errorInfo.getLocation();
                debugInfo = (String) errorInfo.get("debugInfo");
            }
        } else {
            code = ((GoogleJsonResponseException) exception).getStatusCode();
        }
    }
    this.retryable = MoreObjects.firstNonNull(retryable, isRetryable(idempotent, exception));
    this.code = code;
    this.reason = reason;
    this.idempotent = idempotent;
    this.location = location;
    this.debugInfo = debugInfo;
}

From source file:com.google.cloud.BaseServiceException.java

License:Open Source License

public BaseServiceException(GoogleJsonError googleJsonError, boolean idempotent) {
    super(googleJsonError.getMessage());
    Error error = new Error(googleJsonError.getCode(), reason(googleJsonError));
    this.code = error.code;
    this.reason = error.reason;
    this.retryable = isRetryable(idempotent, error);
    if (this.reason != null) {
        GoogleJsonError.ErrorInfo errorInfo = googleJsonError.getErrors().get(0);
        this.location = errorInfo.getLocation();
        this.debugInfo = (String) errorInfo.get("debugInfo");
    } else {// w  w w.j  av  a 2 s.c  o  m
        this.location = null;
        this.debugInfo = null;
    }
    this.idempotent = idempotent;
}

From source file:com.google.cloud.dataflow.sdk.util.ApiErrorExtractor.java

License:Open Source License

/**
 * Determines if the given GoogleJsonError indicates 'item not found'.
 *//*from  ww w.  j  av a2s  .c  om*/
public boolean itemNotFound(GoogleJsonError e) {
    return e.getCode() == HttpStatusCodes.STATUS_CODE_NOT_FOUND;
}

From source file:com.google.cloud.genomics.api.client.commands.BaseCommand.java

License:Open Source License

public static String getErrorMessage(GoogleJsonResponseException e) {
    String message = e.getStatusMessage();
    GoogleJsonError details = e.getDetails();
    if (details != null) {
        message = details.getMessage() + " (" + details.getCode() + ")";
    }/*from  w  ww.ja  v  a2s . c  o m*/
    return message;
}

From source file:com.google.cloud.hadoop.util.ApiErrorExtractor.java

License:Open Source License

/**
 * Determines if the given GoogleJsonError indicates 'precondition not met'
 *//*  w  w w . ja  v a2 s  .c  o  m*/
public boolean preconditionNotMet(GoogleJsonError e) {
    return e.getCode() == STATUS_CODE_PRECONDITION_FAILED;
}

From source file:com.google.cloud.http.BaseHttpServiceException.java

License:Apache License

private static ExceptionData makeExceptionData(IOException exception, boolean idempotent,
        Set<BaseServiceException.Error> retryableErrors) {
    int code = UNKNOWN_CODE;
    String reason = null;/*from  w  ww  .  j  a  v a2 s  .  com*/
    String location = null;
    String debugInfo = null;
    Boolean retryable = null;
    if (exception instanceof HttpResponseException) {
        if (exception instanceof GoogleJsonResponseException) {
            GoogleJsonError jsonError = ((GoogleJsonResponseException) exception).getDetails();
            if (jsonError != null) {
                BaseServiceException.Error error = new BaseServiceException.Error(jsonError.getCode(),
                        reason(jsonError));
                code = error.getCode();
                reason = error.getReason();
                retryable = error.isRetryable(idempotent, retryableErrors);
                if (reason != null) {
                    GoogleJsonError.ErrorInfo errorInfo = jsonError.getErrors().get(0);
                    location = errorInfo.getLocation();
                    debugInfo = (String) errorInfo.get("debugInfo");
                }
            } else {
                code = ((GoogleJsonResponseException) exception).getStatusCode();
                retryable = BaseServiceException.isRetryable(code, null, idempotent, retryableErrors);
            }
        } else {
            // In cases where an exception is an instance of HttpResponseException but not
            // an instance of GoogleJsonResponseException, check the status code to determine whether it's retryable
            code = ((HttpResponseException) exception).getStatusCode();
            retryable = BaseServiceException.isRetryable(code, null, idempotent, retryableErrors);
        }
    }
    return ExceptionData.newBuilder().setMessage(message(exception)).setCause(exception)
            .setRetryable(MoreObjects.firstNonNull(retryable,
                    BaseServiceException.isRetryable(idempotent, exception)))
            .setCode(code).setReason(reason).setLocation(location).setDebugInfo(debugInfo).build();
}

From source file:com.google.cloud.http.BaseHttpServiceException.java

License:Apache License

private static ExceptionData makeExceptionData(GoogleJsonError googleJsonError, boolean idempotent,
        Set<BaseServiceException.Error> retryableErrors) {
    int code = googleJsonError.getCode();
    String reason = reason(googleJsonError);

    ExceptionData.Builder exceptionData = ExceptionData.newBuilder();
    exceptionData.setMessage(googleJsonError.getMessage()).setCause(null)
            .setRetryable(BaseServiceException.isRetryable(code, reason, idempotent, retryableErrors))
            .setCode(code).setReason(reason);
    if (reason != null) {
        GoogleJsonError.ErrorInfo errorInfo = googleJsonError.getErrors().get(0);
        exceptionData.setLocation(errorInfo.getLocation());
        exceptionData.setDebugInfo((String) errorInfo.get("debugInfo"));
    } else {/*from   w ww.  j a  v a2s. co m*/
        exceptionData.setLocation(null);
        exceptionData.setDebugInfo(null);
    }
    return exceptionData.build();
}

From source file:com.google.gcloud.BaseServiceException.java

License:Open Source License

public BaseServiceException(GoogleJsonError error, boolean idempotent) {
    super(error.getMessage());
    this.code = error.getCode();
    this.reason = reason(error);
    this.idempotent = idempotent;
    this.retryable = idempotent && isRetryable(error);
    this.location = null;
    this.debugInfo = null;
}

From source file:com.google.gcloud.BaseServiceException.java

License:Open Source License

protected static Error error(GoogleJsonError error) {
    return new Error(error.getCode(), reason(error));
}