gpt4 book ai didi

java - 如何处理异步 block java中的异常

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:37:30 26 4
gpt4 key购买 nike

我正在一个接一个地发出 2 个 https 请求。我想在发出另一个请求之前返回错误状态,以防第一个请求失败。

private CompletionStage<WSResponse> reverseGeocode(Double latitude, Double longitude) throws AuthenticationException {
WSRequest complexRequest = ws.url(SECO_URL);

return complexRequest.post(GRANT_TYPE + "=" + PASSWORD + "&" + USERNAME + "=" + SECO_USERNAME + "&" + PASSWORD
+ "=" + SECO_PASSWORD)
.thenCompose(res -> {
String authToken;
try {
authToken = res.asJson().findValue("access_token").toString().replace("\"", "");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
LOG.error(e.getMessage() + e.toString());
throw new AuthenticationException("Not able to authenticate user");
}
String url = configuration.getString("host.lbsUrl") + "/rgc/json?lat="
+ latitude + "&lon=" + longitude;
WSRequest requestToReverseGeocode = ws.url(url)
.setHeader(API_KEY, ST_SECO_X_API_KEY)
.setHeader(CONTENT_TYPE, CONTENT_TYPE_VALUE)
.setHeader(AUTHORIZATION, "Bearer " + authToken);
return requestToReverseGeocode.get();
});

}

上面的函数给我错误,身份验证错误未处理。在这种情况下如何返回错误?

以上代码无法编译,错误是

play.sbt.PlayExceptions$CompilationException: Compilation error[unreported exception org.apache.http.auth.AuthenticationException; must be caught or declared to be thrown]
at play.sbt.PlayExceptions$CompilationException$.apply(PlayExceptions.scala:27)
at play.sbt.PlayExceptions$CompilationException$.apply(PlayExceptions.scala:27)
at scala.Option.map(Option.scala:145)
at play.sbt.run.PlayReload$$anonfun$taskFailureHandler$1.apply(PlayReload.scala:49)
at play.sbt.run.PlayReload$$anonfun$taskFailureHandler$1.apply(PlayReload.scala:44)
at scala.Option.map(Option.scala:145)
at play.sbt.run.PlayReload$.taskFailureHandler(PlayReload.scala:44)
at play.sbt.run.PlayReload$.compileFailure(PlayReload.scala:40)
at play.sbt.run.PlayReload$$anonfun$compile$1.apply(PlayReload.scala:17)
at play.sbt.run.PlayReload$$anonfun$compile$1.apply(PlayReload.scala:17)
at scala.util.Either$LeftProjection.map(Either.scala:377)
at play.sbt.run.PlayReload$.compile(PlayReload.scala:17)
at play.sbt.run.PlayRun$$anonfun$playRunTask$1$$anonfun$apply$2$$anonfun$apply$3$$anonfun$2.apply(PlayRun.scala:61)
at play.sbt.run.PlayRun$$anonfun$playRunTask$1$$anonfun$apply$2$$anonfun$apply$3$$anonfun$2.apply(PlayRun.scala:61)
at play.runsupport.Reloader$$anonfun$reload$1.apply(Reloader.scala:339)
at play.runsupport.Reloader$$anon$3.run(Reloader.scala:44)
at java.security.AccessController.doPrivileged(Native Method)
at play.runsupport.Reloader$.play$runsupport$Reloader$$withReloaderContextClassLoader(Reloader.scala:40)
at play.runsupport.Reloader.reload(Reloader.scala:337)
at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1.apply(DevServerStart.scala:118)
at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1.apply(DevServerStart.scala:116)
at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24)
at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24)
at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:39)
at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:405)
at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)

最佳答案

您可以使用exceptionally 来捕捉任何可能出现的错误。

return complexRequest.post()
.thenCompose(res -> {
if (someCondition) throw new Step1Exception("Step 1 failed");
// (...)
}).thenCompose(res -> {
if (someAnotherCondition) throw new StepNException("Step N failed");
// (...)
}).exceptionally(e -> ok("Got an error: " + e.getMessage()));

请注意,如果 someCondition 为真,则不会执行第二个 thenCompose

关于java - 如何处理异步 block java中的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39277169/

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