gpt4 book ai didi

java - Play Framework - ErrorHandler 未被调用

转载 作者:行者123 更新时间:2023-12-03 11:17:57 29 4
gpt4 key购买 nike

我在尝试使用特定场景中的 HttpErrorHandler 机制处理异常时遇到了 Play framework (2.6) 中的问题。

我在根包中有一个简单的ErrorHandler:

import play.Logger;
import play.http.HttpErrorHandler;
import play.mvc.Http;
import play.mvc.Result;
import play.mvc.Results;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;

public class ErrorHandler implements HttpErrorHandler {

@Override
public CompletionStage<Result> onClientError(Http.RequestHeader request, int statusCode, String message) {
Logger.error("client error");
return CompletableFuture.completedFuture(Results.status(statusCode, "A client error occurred: " + message));
}

@Override
public CompletionStage<Result> onServerError(Http.RequestHeader request, Throwable exception) {
Logger.error("server error");

return CompletableFuture.completedFuture(
Results.internalServerError("A server error occurred: " + exception.getMessage()));
}
}

我创建了一个自定义 Controller 来处理 Assets :

package controllers;

import org.apache.commons.lang3.StringUtils;
import play.api.mvc.Action;
import play.api.mvc.AnyContent;
import play.mvc.Controller;

import javax.inject.Inject;

public class AssetsCustomController extends Controller {

private final Assets assets;

@Inject
public AssetsCustomController(Assets assets) {
this.assets = assets;
}

public Action<AnyContent> at(String path, String file) {
if (StringUtils.isEmpty(path)) {
throw new IllegalArgumentException();
}
return assets.at(path, file, false);
}
}

我已经检查了 ErrorHandler 并且在所有情况下都工作正常,除非从 AssetsCustomController 中的此方法抛出异常:

public Action<AnyContent> at(String path, String file) {
if (StringUtils.isEmpty(path)) {
throw new IllegalArgumentException();
}
return assets.at(path, file, false);
}

例如,当 IllegalArgumentException 被抛出时,我的 ErrorHandler 被忽略并且 Play 使用他的本地错误处理程序。这是堆栈跟踪:

play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[IllegalArgumentException: null]]
at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:255)
at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:182)
at play.api.http.DefaultHttpErrorHandler$.onServerError(HttpErrorHandler.scala:286)
at play.core.server.Server.logExceptionAndGetResult$1(Server.scala:53)
at play.core.server.Server.getHandlerFor(Server.scala:83)
at play.core.server.Server.getHandlerFor$(Server.scala:49)
at play.core.server.AkkaHttpServer.getHandlerFor(AkkaHttpServer.scala:42)
at play.core.server.AkkaHttpServer.getHandler(AkkaHttpServer.scala:215)
at play.core.server.AkkaHttpServer.handleRequest(AkkaHttpServer.scala:195)
at play.core.server.AkkaHttpServer.$anonfun$createServerBinding$3(AkkaHttpServer.scala:107)
Caused by: java.lang.IllegalArgumentException: null
at controllers.AssetsCustomController.at(AssetsCustomController.java:20)
at router.Routes$$anonfun$routes$1.$anonfun$applyOrElse$10(Routes.scala:183)
at play.core.routing.HandlerInvokerFactory$$anon$6$$anon$7.call(HandlerInvoker.scala:61)
at play.core.routing.GeneratedRouter$$anon$2.call(GeneratedRouter.scala:251)
at router.Routes$$anonfun$routes$1.$anonfun$applyOrElse$9(Routes.scala:183)
at play.core.routing.GeneratedRouter.$anonfun$call$5(GeneratedRouter.scala:99)
at scala.util.Either.fold(Either.scala:118)
at play.core.routing.GeneratedRouter.call(GeneratedRouter.scala:99)
at router.Routes$$anonfun$routes$1.applyOrElse(Routes.scala:182)
at router.Routes$$anonfun$routes$1.applyOrElse(Routes.scala:154)

[解决方案]
多亏了答案 Flo354,我才能够解决问题,AssetsCustomController 是这样的:

package controllers;

import akka.stream.Materializer;
import org.springframework.util.StringUtils;
import play.mvc.Controller;
import play.mvc.Result;

import javax.inject.Inject;
import java.util.concurrent.CompletionStage;

public class AssetsCustomController extends Controller {

private final Assets assets;
private final Materializer materializer;

@Inject
public AssetsCustomController(Materializer materializer, Assets assets) {
this.materializer = materializer;
this.assets = assets;
}

public CompletionStage<Result> at(String path, String file) {
if (StringUtils.isEmpty(path)) {
throw new IllegalArgumentException();
}
return assets.at(path, file, false).asJava().apply(request()).run(materializer);
}
}

最佳答案

你的 Action<AnyContent>是 Scala API 的一部分,这意味着它不使用 Java 部分。

您的自定义错误处理程序仅适用于 Java 源代码。查看您的堆栈跟踪

play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[IllegalArgumentException: null]]
at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:255)
at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:182)
at play.api.http.DefaultHttpErrorHandler$.onServerError(HttpErrorHandler.scala:286)
....

play.api.something可以看出是scala

您可以通过发回 CompletionStage<Result> 来解决此问题

public CompletionStage<Result> test(String path, String file) {
if (StringUtils.isEmpty(path)) {
throw new IllegalArgumentException();
}

return assets.at(path, file, false).asJava().apply(request()).run(materializer);
}

在你的 Controller 中,不要忘记注入(inject) akka.stream.Materializer 的一个实例

希望对您有所帮助!

关于java - Play Framework - ErrorHandler 未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45850081/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com