Adi shukla
Adi shukla

Reputation: 303

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

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

Upvotes: 1

Views: 3414

Answers (1)

Poorya Hosseini
Poorya Hosseini

Reputation: 222

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

Upvotes: 2

Related Questions