Help to handle API 404 error

Hello Wapplers,

I’m try to handle external API call when there is 404 error.
I have Condition after API call api.resposne==200 which handle good when external API return 200, but when there is 404 error I see this message in output: {"errorCode":404,"message":"Device not found for specified identifier","activityId":"8f1925a7-dc7e-4d6e-bd82-955714eb0012"}
image
And errors in WebLogs:

  server-connect:server Got error? '{"errorCode":404,"message":"Device not found for specified identifier","activityId":"b7b1c30a-70b0-426c-8360-94593a182025"}' +13s
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at new NodeError (node:internal/errors:387:5)
    at ServerResponse.setHeader (node:_http_outgoing:644:11)
    at ServerResponse.header (/opt/node_app/node_modules/express/lib/response.js:794:10)
    at ServerResponse.send (/opt/node_app/node_modules/express/lib/response.js:174:12)
    at ServerResponse.json (/opt/node_app/node_modules/express/lib/response.js:278:15)
    at /opt/node_app/lib/server.js:81:29
    at Layer.handle_error (/opt/node_app/node_modules/express/lib/router/layer.js:71:5)
    at trim_prefix (/opt/node_app/node_modules/express/lib/router/index.js:326:13)
    at /opt/node_app/node_modules/express/lib/router/index.js:286:9
    at Function.process_params (/opt/node_app/node_modules/express/lib/router/index.js:346:12)

How then to design handle when 404 error appears?

Add a try/catch step and call the API within the try block.

Is this what you’re suggesting?
image

Unfortunately same result.

have you unchecked ‘Pass errors’ on your API action ?

Are you aware this condition is (apparently) specific to your API? The correct variable for HTTP status code would be api.status

Yes, I’m sure.
When status is 200 - condition works.

image

Restart Wappler and show screenshot of the Pass errors checkbox unticked so we can believe you :yum:

And you should use api.status in your condition, Try/Catch won’t work due to Wappler design choices (there are feature requests on this issue)

1 Like

I’ve unchecked- Pass Errors and now all works as I expected with conditions.

@Apple Can you please explain - what this options stands for? I’ve never tried to play with this parameter.

It’s an option it’ll stop your script if the API replies a non-200 HTTP status code, it’s kind of implemented wrongly in my opinion. You can read past comments of mine about it:


1 Like

Thank you for the detailed explanation!
Much appriciate!