How to do custom error handling in an apache camel rest api?

孤者浪人 提交于 2020-07-16 08:00:29

问题


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();
          }
}

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

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=)

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


回答1:


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")


来源:https://stackoverflow.com/questions/62763173/how-to-do-custom-error-handling-in-an-apache-camel-rest-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!