Search in sources :

Example 1 with COSClientIOException

use of com.ibm.stocator.fs.cos.exception.COSClientIOException in project stocator by CODAIT.

the class COSUtils method translateException.

/**
 * Translate an exception raised in an operation into an IOException. The
 * specific type of IOException depends on the class of
 * {@link AmazonClientException} passed in, and any status codes included in
 * the operation. That is: HTTP error codes are examined and can be used to
 * build a more specific response.
 *
 * @param operation operation
 * @param path path operated on (may be null)
 * @param exception amazon exception raised
 * @return an IOE which wraps the caught exception
 */
@SuppressWarnings("ThrowableInstanceNeverThrown")
public static IOException translateException(String operation, String path, AmazonClientException exception) {
    String message = String.format("%s%s: %s", operation, path != null ? (" on " + path) : "", exception);
    if (!(exception instanceof AmazonServiceException)) {
        if (containsInterruptedException(exception)) {
            return (IOException) new InterruptedIOException(message).initCause(exception);
        }
        return new COSClientIOException(message, exception);
    } else {
        IOException ioe;
        AmazonServiceException ase = (AmazonServiceException) exception;
        // this exception is non-null if the service exception is an COS one
        AmazonS3Exception s3Exception = ase instanceof AmazonS3Exception ? (AmazonS3Exception) ase : null;
        int status = ase.getStatusCode();
        switch(status) {
            case 301:
                if (s3Exception != null) {
                    if (s3Exception.getAdditionalDetails() != null && s3Exception.getAdditionalDetails().containsKey(ENDPOINT_KEY)) {
                        message = String.format("Received permanent redirect response to " + "endpoint %s.  This likely indicates that the COS endpoint " + "configured in %s does not match the region containing " + "the bucket.", s3Exception.getAdditionalDetails().get(ENDPOINT_KEY), ENDPOINT_URL);
                    }
                    ioe = new COSIOException(message, s3Exception);
                } else {
                    ioe = new COSServiceIOException(message, ase);
                }
                break;
            // permissions
            case 401:
            case 403:
                ioe = new AccessDeniedException(path, null, message);
                ioe.initCause(ase);
                break;
            // the object isn't there
            case 404:
            case 410:
                ioe = new FileNotFoundException(message);
                ioe.initCause(ase);
                break;
            // a shorter one while it is being read.
            case 416:
                ioe = new EOFException(message);
                break;
            default:
                // no specific exit code. Choose an IOE subclass based on the class
                // of the caught exception
                ioe = s3Exception != null ? new COSIOException(message, s3Exception) : new COSServiceIOException(message, ase);
                break;
        }
        return ioe;
    }
}
Also used : COSClientIOException(com.ibm.stocator.fs.cos.exception.COSClientIOException) InterruptedIOException(java.io.InterruptedIOException) AccessDeniedException(java.nio.file.AccessDeniedException) COSIOException(com.ibm.stocator.fs.cos.exception.COSIOException) COSServiceIOException(com.ibm.stocator.fs.cos.exception.COSServiceIOException) AmazonServiceException(com.amazonaws.AmazonServiceException) FileNotFoundException(java.io.FileNotFoundException) EOFException(java.io.EOFException) COSServiceIOException(com.ibm.stocator.fs.cos.exception.COSServiceIOException) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) COSIOException(com.ibm.stocator.fs.cos.exception.COSIOException) COSClientIOException(com.ibm.stocator.fs.cos.exception.COSClientIOException) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception)

Example 2 with COSClientIOException

use of com.ibm.stocator.fs.cos.exception.COSClientIOException in project stocator by SparkTC.

the class COSUtils method translateException.

/**
 * Translate an exception raised in an operation into an IOException. The
 * specific type of IOException depends on the class of
 * {@link AmazonClientException} passed in, and any status codes included in
 * the operation. That is: HTTP error codes are examined and can be used to
 * build a more specific response.
 *
 * @param operation operation
 * @param path path operated on (may be null)
 * @param exception amazon exception raised
 * @return an IOE which wraps the caught exception
 */
@SuppressWarnings("ThrowableInstanceNeverThrown")
public static IOException translateException(String operation, String path, AmazonClientException exception) {
    String message = String.format("%s%s: %s", operation, path != null ? (" on " + path) : "", exception);
    if (!(exception instanceof AmazonServiceException)) {
        if (containsInterruptedException(exception)) {
            return (IOException) new InterruptedIOException(message).initCause(exception);
        }
        return new COSClientIOException(message, exception);
    } else {
        IOException ioe;
        AmazonServiceException ase = (AmazonServiceException) exception;
        // this exception is non-null if the service exception is an COS one
        AmazonS3Exception s3Exception = ase instanceof AmazonS3Exception ? (AmazonS3Exception) ase : null;
        int status = ase.getStatusCode();
        switch(status) {
            case 301:
                if (s3Exception != null) {
                    if (s3Exception.getAdditionalDetails() != null && s3Exception.getAdditionalDetails().containsKey(ENDPOINT_KEY)) {
                        message = String.format("Received permanent redirect response to " + "endpoint %s.  This likely indicates that the COS endpoint " + "configured in %s does not match the region containing " + "the bucket.", s3Exception.getAdditionalDetails().get(ENDPOINT_KEY), ENDPOINT_URL);
                    }
                    ioe = new COSIOException(message, s3Exception);
                } else {
                    ioe = new COSServiceIOException(message, ase);
                }
                break;
            // permissions
            case 401:
            case 403:
                ioe = new AccessDeniedException(path, null, message);
                ioe.initCause(ase);
                break;
            // the object isn't there
            case 404:
            case 410:
                ioe = new FileNotFoundException(message);
                ioe.initCause(ase);
                break;
            // a shorter one while it is being read.
            case 416:
                ioe = new EOFException(message);
                break;
            default:
                // no specific exit code. Choose an IOE subclass based on the class
                // of the caught exception
                ioe = s3Exception != null ? new COSIOException(message, s3Exception) : new COSServiceIOException(message, ase);
                break;
        }
        return ioe;
    }
}
Also used : COSClientIOException(com.ibm.stocator.fs.cos.exception.COSClientIOException) InterruptedIOException(java.io.InterruptedIOException) AccessDeniedException(java.nio.file.AccessDeniedException) COSIOException(com.ibm.stocator.fs.cos.exception.COSIOException) COSServiceIOException(com.ibm.stocator.fs.cos.exception.COSServiceIOException) AmazonServiceException(com.amazonaws.AmazonServiceException) FileNotFoundException(java.io.FileNotFoundException) EOFException(java.io.EOFException) COSServiceIOException(com.ibm.stocator.fs.cos.exception.COSServiceIOException) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) COSIOException(com.ibm.stocator.fs.cos.exception.COSIOException) COSClientIOException(com.ibm.stocator.fs.cos.exception.COSClientIOException) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception)

Aggregations

AmazonServiceException (com.amazonaws.AmazonServiceException)2 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)2 COSClientIOException (com.ibm.stocator.fs.cos.exception.COSClientIOException)2 COSIOException (com.ibm.stocator.fs.cos.exception.COSIOException)2 COSServiceIOException (com.ibm.stocator.fs.cos.exception.COSServiceIOException)2 EOFException (java.io.EOFException)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 InterruptedIOException (java.io.InterruptedIOException)2 AccessDeniedException (java.nio.file.AccessDeniedException)2