2016-05-25 33 views
1

我在嘗試處理負責數據插入的休息服務上的服務器錯誤時遇到問題。使用runAsync時播放框架處理錯誤

public CompletableFuture<Result> insertSomething() throws IOException { 
    JsonNode jsNode = request().body().asJson(); 
    format json node to be used 
    } 
    return CompletableFuture.runAsync(() -> { 
     try { 
      service.insertSomething(something); 
     } catch (ParseException e) { 
      internalServerError(); 
     } 
    }) 
      .thenApply(future -> created("data inserted")) 
      .exceptionally(ex -> internalServerError()); 
} 

internalServerError永遠不會被拋出,它一直說「數據插入」。即使我發送一些拋出ParseException的數據。而在調試模式下,我看到它傳入了catch,但沒有拋出。

提前致謝

回答

1

我已經找到了答案,我只是以實例Throwable類型的對象在catch這樣的:

public CompletableFuture<Result> insertSomething() throws IOException { 
JsonNode jsNode = request().body().asJson(); 
format json node to be used 
} 
return CompletableFuture.runAsync(() -> { 
    try { 
     service.insertSomething(something); 
    } catch (ParseException e) { 
     new Throwable(e.getMessage()); 
    } 
}) 
     .thenApply(future -> created("data inserted")) 
     .exceptionally(ex -> internalServerError()); 

}

+0

其實,你只是消音的'ParseException'。你打算在'catch'塊中寫一個'throw'語句嗎?如果是這樣,你需要拋出一個'RuntimeException',這裏'CompletionException'是最合適的,例如'拋出新的CompletionException(e);'。 – acelent

0

這是因爲異常被捕獲。如果你想拋出一個錯誤,你不需要去捕捉它。

0

當然沒有什麼會被拋出 - 你抓住它? exceptionally只會在runAsync內部執行的異常時被代碼處理