我是Play的新手,并且能够在某些请求上实现Action来处理PersistentExceptions。我正在为它添加新的例外,问题是它们没有被捕获,我无法理解为什么。
我还尝试使用play的github中的示例转移到更具功能性的编码,我得到了以下错误处理程序:
public class ErrorHandler extends Action.Simple {
public CompletionStage<Result> call(Http.Context ctx) {
try {
return delegate.call(ctx);
} catch (NoResultException e) {
logger.error("Error", e);
return CompletableFuture.supplyAsync(
() -> notFound(new RecordNotFoundResponse(ctx.session().get("lang")).toJson()));
} catch (PersistenceException e) {
logger.error("Error", e);
return CompletableFuture.supplyAsync(
() -> status(409, (new ConstraitsViolatedResponse(ctx.session().get("lang")).toJson())));
}
// Tried also to catch Exception with no success
catch (UnexpectedException e) {
logger.error("Internal server error", e);
return CompletableFuture.supplyAsync(
() -> internalServerError(new InternalServerErrorResponse(ctx.session().get("lang")).toJson()));
}
控制器:
@Authorization(userRoles = {UserAccount.ROLE_ADMIN})
@Audit(operation = ActivityLog.OPERATION_TYPE_DELETE, message = "Endpoint deleted")
public CompletionStage<Result> delete(Long id) {
return EndpointOps.deleteEndpointById(id)
.thenApplyAsync(aLong -> ok(new SuccessResponse(session("lang")).toJson()), ec.current());
}
数据库访问:
public static CompletableFuture<Optional<Long>> deleteEndpointById(Long id) {
return CompletableFuture.supplyAsync(() -> {
Optional<Endpoint> endpoint = Optional.ofNullable(Ebean.find(Endpoint.class, id));
endpoint.orElseThrow(NoResultException::new);
endpoint.ifPresent(Ebean::delete);
return endpoint.map(Endpoint::getId);
});
}
当我尝试删除不存在的Endpoint并抛出NoResultException时,它永远不会被我的操作捕获并获取此日志文件:
2018-02-01 14:58:37 +0000 - [错误] - 来自application-akka.actor.default-dispatcher-4中的应用程序
! @ 76o8h094n - 内部服务器错误,(DELETE)[/ rest / v1 / endpoints / 20] - &gt;
play.api.UnexpectedException:意外异常[CompletionException:javax.persistence.NoResultException]