如何避免重复尝试阻止

时间:2016-03-03 10:28:30

标签: java

我正在使用aws在Java中编写Web服务,并且在许多方法中我需要一个try catch块,它可以实际记录在每个公开方法的执行中可能发生的任何错误。

@WebMethod(operationName = "listingBucket")
public String listingBucket() {
    String message = "";        
    try {
        message = "Listing buckets";            
        for (Bucket bucket : s3.listBuckets()) {
            message += " - " + bucket.getName();
        }
    } catch (AmazonServiceException ase) {
        message += "Caught an AmazonServiceException, which means your request made it "
                + "to Amazon S3, but was rejected with an error response for some reason.";
        message += "Error Message:    " + ase.getMessage();
        message += "HTTP Status Code: " + ase.getStatusCode();
        message += "AWS Error Code:   " + ase.getErrorCode();
        message += "Error Type:       " + ase.getErrorType();
        message += "Request ID:       " + ase.getRequestId();
    } catch (AmazonClientException ace) {
        message += "Caught an AmazonClientException, which means the client encountered "
                + "a serious internal problem while trying to communicate with S3, "
                + "such as not being able to access the network.";
        message += "Error Message: " + ace.getMessage();
    }
    return message;
}
@WebMethod(operationName = "addObjectToBucket")
public String addObjectToBucket(String bucketName, String objectName, File file) throws IOException{
    if ( file == null ){
        file = createSampleFile();
    }
    String message = "";        
    try {
        message += "Uploading a new object to S3 from a file\n";     
        s3.putObject(new PutObjectRequest(bucketName, objectName, file));
    } catch (AmazonServiceException ase) {
        message += "Caught an AmazonServiceException, which means your request made it "
                + "to Amazon S3, but was rejected with an error response for some reason.";
        message += "Error Message:    " + ase.getMessage();
        message += "HTTP Status Code: " + ase.getStatusCode();
        message += "AWS Error Code:   " + ase.getErrorCode();
        message += "Error Type:       " + ase.getErrorType();
        message += "Request ID:       " + ase.getRequestId();
    } catch (AmazonClientException ace) {
        message += "Caught an AmazonClientException, which means the client encountered "
                + "a serious internal problem while trying to communicate with S3, "
                + "such as not being able to access the network.";
        message += "Error Message: " + ace.getMessage();
    }
    return message;        
}

如何避免重复此尝试catch块抛出所有使用此类东西的方法?

感谢您的帮助!

编辑:其实我修改了代码:

private String parseError(AmazonServiceException ase) {
    String message;
    message = "Caught an AmazonServiceException, which means your request made it "
            + "to Amazon S3, but was rejected with an error response for some reason.";
    message += "Error Message:    " + ase.getMessage();
    message += "HTTP Status Code: " + ase.getStatusCode();
    message += "AWS Error Code:   " + ase.getErrorCode();
    message += "Error Type:       " + ase.getErrorType();
    message += "Request ID:       " + ase.getRequestId();
    return message;
}

private String parseError(AmazonClientException ace) {
    String message;
    message += "Caught an AmazonClientException, which means the client encountered "
            + "a serious internal problem while trying to communicate with S3, "
            + "such as not being able to access the network.";
    message += "Error Message: " + ace.getMessage();
    return message;
}

@WebMethod(operationName = "listingBucket")
public String listingBucket() {
    String message = "";
    try {
        message = "Listing buckets";
        for (Bucket bucket : s3.listBuckets()) {
            message += " - " + bucket.getName();
        }
    } catch (AmazonServiceException exc) {
        message += parseError(exc);
    } catch (AmazonClientException exc) {
        message += parseError(exc);
    }
    return message;
}
确实更清楚! :)

我只是看看命令模式,看看我是否可以将它用于这种应用程序。

2 个答案:

答案 0 :(得分:3)

这里有两个方面。

有一件事是关于catch块中的代码重复;这很容易变成像

这样的东西
public class ExceptionHandler {

public String buildMessageFor(AmazonServiceException ase) {
... }

public String buildMessageFor(AmazonClientException ase) {
... }
...

你甚至可以非常轻松地对这个东西进行单元测试(“命名”可以改进;但我想这个例子应该足以让你顺利)。

这也将使将来更容易从“纯字符串”消息转变为其他消息。您知道,源代码中的硬编码用户消息并不是最明智的做法。

另一部分是try / catch本身;不知何故取决于。你看,try / catch是你操作的重要部分;很多人会争辩说你只是在你的代码中保留了这个结构。唯一的选择是定义某种类型的界面,如

public interface RunAmazonOperation {
public void run() throws Amazon...
}

然后,您可以将所有操作记录为实现该接口的小类;由一些为你做try / catch的框架调用。如果值得的话......取决于你的申请。

换句话说:如果你转向“命令”模式;您可能会发现定义各种“命令”很有用;实现该接口;从而大大减少了尝试/捕获的地方数量。

答案 1 :(得分:1)

用方法做吧。一种可能性如下:

String parseError(AmazonServiceException ase){
    String message;
    message = "Caught an AmazonServiceException, which means your request made it "
            + "to Amazon S3, but was rejected with an error response for some reason.";
    message += "Error Message:    " + ase.getMessage();
    message += "HTTP Status Code: " + ase.getStatusCode();
    message += "AWS Error Code:   " + ase.getErrorCode();
    message += "Error Type:       " + ase.getErrorType();
    message += "Request ID:       " + ase.getRequestId();
    return message;
}

String parseError(AmazonClientException ace){
    String message;
    message = "Caught an AmazonClientException, which means the client encountered "
            + "a serious internal problem while trying to communicate with S3, "
            + "such as not being able to access the network.";
    message += "Error Message: " + ace.getMessage();
    return message;
}

现在你可以写:

catch(AmazonServiceException exc){
    message=parseError(exc);
}
catch(AmazonClientException exc){
    message=parseError(exc);
}