2016-03-03 77 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