我在春天Webstart的應用處理工作...Java的異常重複在一個單獨的方法
我有2個(可能更多)的方法是處理多層次的免責條款,如:
...
try {
employeeService.updateEmployeePartner(employeeId, partner);
LOG.info("partner details updated for partner id {}", employeeId);
result = new ResponseEntity<>(partner.getId(), HttpStatus.OK);
} catch (EmployeePartnerNotFoundException ex) {
LOG.error(ex.getMessage() + " employee id: ", employeeId);
errorResponse = new ErrorResponse("500", ex.getMessage());
} catch (ReadOperationDeniedException ex) {
LOG.error("User doesn't have permissions to update employee's {} details: {}", employeeId, ex.getMessage());
errorResponse = new ErrorResponse("403", "User doesn't have permissions to update employee's details");
} catch (Exception ex) {
LOG.error("something went wrong while updating employee's {} partner details: {}", employeeId, ex.getMessage());
errorResponse = new ErrorResponse("500", "unspecified server error");
} finally {
result = (result != null) ? result : new ResponseEntity<>(errorResponse, HttpStatus.I_AM_A_TEAPOT); // should be INTERNAL_SERVER_ERROR
}
...
另一種方法幾乎相同,除了這種變化: employeeService.updateEmployeePartner(employeeId, partner);
=> employeeService.createEmployeePartner(employeeId, partner);
並在該塊中捕獲EmployeePartnerAlreadyExistsException
。
現在,爲了減少代碼重複,我希望將所有的錯誤在一個地方(方法)處理代碼,所以我取代了上面的代碼與下面的
...
try {
employeeService.updateEmployeePartner(employeeId, partner);
LOG.info("partner details updated for partner id {}", employeeId);
result = new ResponseEntity<>(partner.getId(), HttpStatus.OK);
} catch (Exception ex) {
errorResponse = processException(ex, employeeId, "update");
} finally {
result = (result != null) ? result : new ResponseEntity<>(errorResponse, HttpStatus.I_AM_A_TEAPOT); // should be INTERNAL_SERVER_ERROR
}
...
private ErrorResponse processException(Exception ex, Long employeeId, String operation) {
ErrorResponse errorResponse;
if (ex.getClass().equals(EmployeePartnerNotFoundException.class) ||
ex.getClass().equals(EmployeePartnerExistsException.class)) {
LOG.error(ex.getMessage() + " employee id: ", employeeId);
errorResponse = new ErrorResponse("500", ex.getMessage());
} else if (ex.getClass().isInstance(ReadOperationDeniedException.class)) {
LOG.error("User doesn't have permissions to " + operation + " employee's {} details: {}", employeeId, ex.getMessage());
errorResponse = new ErrorResponse("403", "User doesn't have permissions to " + operation + " employee's details");
} else { // Exception
LOG.error("something went wrong while trying to " + operation + " employee's {} partner details: {}", employeeId, ex.getMessage());
errorResponse = new ErrorResponse("500", "unspecified server error");
}
return errorResponse;
}
那是一個足夠好的方法或者是通過將處理外包給一個單獨的方法/類,有沒有在上述場景中處理異常的模式?
因爲它是一個Spring應用,我也考慮使用Spring的異常處理程序,如:
@ExceptionHandler(Exception.class)
,但將只包括我的要求的一部分。
ControllerAdvice比在同一控制器中定義異常處理程序要好。因爲ExceptionHandler註釋方法只對該特定控制器有效,而不是整個應用程序的全局。當然,將它添加到每個控制器使其不適合用於一般的異常處理機制。 –
只需查看http://www.baeldung.com/exception-handling-for-rest-with-spring –