Try/catch API error

Hello dear Wappler superstars !

I’d like to display a clean error when my stripe charges fail.

I’ve enclosed my server connect action with a try/catch block but can’t get to trigger the catch clause.

Tried with and without checking the “pass errors” checkbox.
Either I get the general “catch” with “pass errors” checked.
Or a 200 response without it but in the detailed log (lower right of the second screenshot), we can see that Stripe indeed answered with a 402 itself.

Any ideas ? :slight_smile:

Thank you very much for your help !

The api call doesn’t throw an error, the catch can only catch code errors, not api response errors. The pass errors checkbox is to pass the same status code to the client on status code >= 400. You can uncheck it and use a condition to check the status code instead of the try/catch.

Ok understood, thanks for the clarification.
Have great day :slight_smile:

@patrick

I, too, am having a hard time catching what is known to be a 504.

As you can see from the plethora of ‘catch’ conditions, I think I have covered everything!

So in order to implement your suggestion here, I added a condition after the call. Where the call is Stati, I click to form the condition, but don’t understand how to define the response.

For some reason, my brain thinks Stati.response - but that doesn’t work.

Can you offer a quick pointer on how to implement this please and thank you?

~Elle

Hey Elle,

You have a bit of confusion on how some of this works, so let me help with a few things:

  • A response action immediately stops processing of the workflow and returns the response indicated by its properties. So having a series of responses in Catch will only execute the very first one.
  • The status code (200, 504, etc) of an api action is found by reading its status. So in your case Stati.status will evaluate to 504 if that is what the external api returns.
  • The api action is not actually going to throw an error, so the Try/Catch won’t be used for this particular situation.
  • The pass errors option WILL throw a hard error response but you won’t be able to do anything with it. It is going to stop processing immediately and just send that status response back to the calling entity.

So if you are trying to do something when your api returns 504 you’ll need something like this.

You can then setup other conditions to trap for other status codes as well. The status codes are mutually exclusive so you could do them in the ELSE of the previous condition.

Take note also of that URL, which allows you to force any error code, just by changing the path. It’s a great tool for testing our your code in this use case. To see all of its options go here: https://httpstat.us/

7 Likes

thanks for posting that link! It’s an excellent little tool :slight_smile:

1 Like

@mebeingken

Thank you, Ken!

1 Like