Try Catch in Apigee to handle errors

ekumar
New Member

Hi

I have a requirement where I was sending multiple requests using "Post" method to a backend service.

If I get an error when I post a particular request, I want to catch that payload of request and post to backend service by "Patch" method and after completing the operation again come to "Post" a new request.

So, I want to handle it like Try and catch. If i get an error it will go to catch block and the code in try block runs.

Can anyone please help me in this

0 3 712
3 REPLIES 3

If I get an error when I post a particular request

In which scenario are you getting this error? Which payload parameters will give an error?

We don't have any OOTB Try-Catch block. If you know which payload parameter will give an error, you can use a set of extract variable and assign message policies to change the Verb to Patch and hit the target.

or

Use Try-catch in Javascript callout with httpClient,

try{
  < ..JS CODE..>
} catch (err) {
  < ..JS CODE..>
}

Hi Siddharth,

I can't find my payload because my backend service will store the name in my payload and throws an error if it have the same name already in it. And I was getting all the names in a array and posting at once using single rest call. So, I think it was difficult to get which name throws an error.

And even if I tried java script code using http.client I think it was difficult to get the failed payload because I was not sending multiple calls to send the payload, I was sending the all the payload at once in a single call.

Can you please provide me a simple http.client call by JavaScript in apige....I tried it but I was getting errors in packages. Can you help me here please

Thanks in advance

If you're having "trouble with packages" I suspect you are trying to use nodejs / npm modules in the JavaScript callout. That won't work.

If you have a somewhat simple need to make http outbound calls from a JS callout you can use the builtin httpClient. There is documentation for this on the docs site. Unfortunately the nice callback mechanism that is available is not yet!!! documented. It's supported, but we haven't updated the docs due to some ball-dropping on our end. Here's how you would use it:

function onComplete(response, error) {
    if (response) {
        context.setVariable('STATUS: ' + response.status);
        // can interrogate response.status here too
    } else {
       context.setVariable('IAM', 'NOT OK ' + error);
    }
}
httpClient.get("http://foo.com", onComplete1);
<br>

You can kickoff multiple httpClient operations with distinct callbacks.

If you'd prefer to use npm modules then I direct you to "hosted targets" in which you can write modern nodejs code to handle the external call logic.


I think it was difficult to get the failed payload because I was not sending multiple calls to send the payload, I was sending the all the payload at once in a single call.

I don't understand what that means. If you still want help with that, then you'll need to explain in a little more detail what is going on, what the problem is, and I'll try to help. Maybe show some specific code, and point out the line where you couldn't get the payload.


And I was getting all the names in a array and posting at once using single rest call. So, I think it was difficult to get which name throws an error.

This is a problem with the backend service. If the service throws an error and doesn't provide a specific-enough error message to indicate to you (the developer calling the service) which name is wrong, then the backend service needs to be improved.