【问题标题】:How to do custom error handling in an apache camel rest api?如何在 apache camel rest api 中进行自定义错误处理?
【发布时间】:2020-10-26 23:56:30
【问题描述】:

我有一个从 S3 下载文件的 apache camel rest api。我发送 json input(key, bucketname, accessKey, secretKey, region) 以编写 URI。代码如下所示:

public static class HelloRoute extends RouteBuilder {
       
   @Override
   public void configure() {
     rest("/")
     .post("file-from-s3")
     .route()
     .setHeader(AWS2S3Constants.KEY, key)
     .to("aws2-s3://bucketname?accessKey=INSERT&secretKey=INSERT&region=INSERT&operation=getObject")
      .endRest();
          }
}

通过使用JSONObject 作为正文,我能够在上述代码中提到的位置获取相应的 json 输入。

如果假设,我在我的 json 输入中输入了错误的值(例如 accessKey),我会收到类似的错误

The AWS Access Key Id you provided does not exist in our records. (Service: S3, Status Code: 403, Request ID: 6923BEC55C1FD5F1, Extended Request ID: re5Rb7I76j9jvGqhSQXgjUoMwOZqsprg22bOGD+BDbuj0zrRrf0FceLaapvpR4KcNDY6GntNiF0=)

这不是很用户友好。我怎样才能为此编写一个自定义错误处理程序,它只会为上述情况返回一条错误消息,如 Wrong Access Key

【问题讨论】:

  • 您能否发布完整的堆栈跟踪以查看异常是直接AmazonS3Exception 还是包含在另一个异常中?
  • @Bedla 我收到此错误org.apache.camel.ResolveEndpointFailedException: Failed to resolve endpoint: aws2-s3://accesskey=AKIARGSIOJPSQRMGNS5&secretkey=xxxxxx&bucketname=jcnu-2020-swarnim-shukla&region=us-east-1&operation=getObject due to: useIAMCredentials is set to false, AmazonS3Client or accessKey and secretKey must be specified

标签: java exception error-handling apache-camel


【解决方案1】:

您需要使用“onException”子句并在那里的处理器中创建您自己的响应。见:https://camel.apache.org/manual/latest/exception-clause.html

onException(TheThrownException.class)
    .handled(...) //--> depend on your need true or false
    .continued(...) //--> depend on your need true or false
    .process("processor1")
    .to("direct:error-handling-endpoint")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 2011-11-22
    • 2021-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多