如何在 apache camel rest api 中进行自定义错误处理? [英] How to do custom error handling in an apache camel rest api?

查看:31
本文介绍了如何在 apache camel rest api 中进行自定义错误处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an apache camel rest api which downloads a file from S3. I send json input(key, bucketname, accessKey, secretKey, region) in order to write the URI. The code looks like this:

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 输入.

I am able to get the respective json input in the places mentioned in the above code by using a JSONObject as the body.

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

If suppose, I enter a wrong value in my json input(for ex accessKey), I get an error like

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

which is not very user friendly. How can I write a custom error handler for this which will just return an error message for the above case like Wrong Access Key

推荐答案

你需要使用onException"条款并在那里的处理器中创建您自己的响应.请参阅:https://camel.apache.org/manual/latest/exception-子句.html

You need to use "onException" clause and create your own response in a processor there. See: 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")

这篇关于如何在 apache camel rest api 中进行自定义错误处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆