Forum Documentation Showcase Pricing Learn more

How to catch a 400 error from API connector?

I’m using a (kind of) API for Duolingo, which, when an invalid username parameter is supplied, returns a 400 (Bad Request) status code and a load of HTML, which then throws an uncaught error in the API connector, in the console:

POST https://forumdemo987.bubbleapps.io/version-test/apiservice/doapicallfromserver 400 (Bad Request)

x1:98 Uncaught {statusCode: 400, message: "Error parsing data from Apiconnector2: <!DOCTYPE h…Listener("load",t.captureLoad.bind(t)),r.addEvent", _original_request: {…}, stack: "locationservice.coffee", args: {…}, …}

I would have thought this would kick off the “An unhandled error occurs” event in workflows, but it doesn’t seem to. I’ve also tried checking for when the value of the API’s response is empty with no luck. Any ideas about how to catch when this occurs so I can display an appropriate error message to the user?

Here’s a demo of the issue: https://bubble.io/page?type=page&name=api400&id=forumdemo987&tab=tabs-1

So, I found a solution to this in case anyone else is encountering the same thing. Add the same API call as a second API call, this time setting the Data Type to “Text”. This will return an object resulting in the string “[object Object]” whatever the result- whether it’s a properly formatted result or the 400 error page. So now you can check for a condition where API 2’s result is [object Object] and API 1’s result is empty, meaning the 400 error happened.

1 Like

Thanks for this solution! Hopefully it will work in my situation.

Two questions.

  1. is this still the most effective solution that you’re aware of?
  2. do you have an example of the implementation that i could look at?

@team is there a better way in 2020?

2 Likes