2016-03-03 74 views
1

我正在使用aws在Java中编写webservice,并且在许多方法中,我需要有一个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;   
} 

如何避免重复此try 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; 
} 

更清晰哉! :)

我只是看看命令模式,看看我是否可以使用它的这种类型的应用程序。

+0

您需要的Java 8 Lambda表达式干净地做到这一点。这是一个选择吗? –

+0

注意:'message ='*替换*以前的值。你只需要做一次,并且不止一次地丢弃之前的值。 –

+0

其实是的,我使用Java 8,我正在寻找Lambda,如果你有一个解决方案,我很好奇,看看它! :) – Slater

回答

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的框架调用。如果这是值得的价格......取决于您的应用程序。换句话说:如果你转向“命令”模式;您可能会发现定义各种“命令”很有用;实现该接口;从而大大减少了尝试/捕捉地点的数量。

+0

这是重复catch部分的* body *的改进。 –

+0

是的我知道纯字符串消息并不聪明,但实际上它只是用于测试目的。这只是为了能够构建一个使用Amazon的SDK的简单Web服务。 我稍后会改进这部分,因为它是后端web服务。将有一个AngularJS Webapp使用该Web服务。 – Slater

+0

我一定会看看命令模式! :) – Slater

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); 
} 
+0

是的,我忘记了“=”x)之前的+)我的这个坏! – Slater

+1

好的,这发生了。第一次在捕捉中你会看到它:-)。 – ctst