Best answer

How do I parse ResponeError

  • 16 April 2024
  • 1 reply
  • 10 views

I have a Zapier CLI app.

 

My app uses an API which returns HTTP 4xx responses when incorrect requests are sent.

Zapier intercepts these responses, and instead of letting the flow of code continue, throws any non HTTP 200 response as an error.

I want to be able to get the 4xx responses myself, parse the error messages, and handle it more gracefully.

 

I tried putting the network request in a try-catch block. The error object in the catch block seems to be an object of `ResponseError` with the error messages returned by the API, but I can’t parse it properly.

 

Here’s what the `error` object looks like inside the catch block (non-beautified):

ResponseError: {"status":400,"headers":{"content-type":"application/json; charset=utf-8","retry-after":null},"content":"{\"ok\":false,\"errors\":[{\"code\":\"400\",\"message\":\"entity does not exist\",\"field\":\"parcel\"}]}","request":{"url":"https://api.example.com/entities"}} at _throwForStatus (/var/task/node_modules/zapier-platform-core/src/http-middlewares/after/prepare-response.js:13:11) at outResp.throwForStatus (/var/task/node_modules/zapier-platform-core/src/http-middlewares/after/prepare-response.js:68:5) at throwForStatusMiddleware (/var/task/node_modules/zapier-platform-core/src/http-middlewares/after/throw-for-status.js:5:14) at Object.<anonymous> (/var/task/node_modules/zapier-platform-core/src/middleware.js:80:37) From previous event: at /var/task/node_modules/zapier-platform-core/src/middleware.js:77:26 at Array.reduce (<anonymous>) at afterMiddleware (/var/task/node_modules/zapier-platform-core/src/middleware.js:76:21) at Object.<anonymous> (/var/task/node_modules/zapier-platform-core/src/middleware.js:94:18) From previous event: at Object.<anonymous> (/var/task/node_modules/zapier-platform-core/src/middleware.js:92:10) From previous event: at /var/task/node_modules/zapier-platform-core/src/middleware.js:89:45 at /var/task/node_modules/zapier-platform-core/src/tools/create-lambda-handler.js:212:18 at bound (node:domain:433:15) at runBound (node:domain:444:12) at process.processImmediate (node:internal/timers:476:21) at process.topLevelDomainCallback (node:domain:161:15) at process.callbackTrampoline (node:internal/async_hooks:126:24) From previous event: at Domain.<anonymous> (/var/task/node_modules/zapier-platform-core/src/tools/create-lambda-handler.js:196:10) at Domain.run (node:domain:389:15) at Runtime.handler (/var/task/node_modules/zapier-platform-core/src/tools/create-lambda-handler.js:192:19) at Runtime.handleOnceNonStreaming (file:///var/runtime/index.mjs:1173:29) { doNotContextify: true } 

 

I have two questions around how do I:

  1. Either stop Zapier from throwing an exception when a non-200 response is received so I can handle it myself, or:
  2. How do I parse the `ResponseError` properly to access the JSON in the `content` field, parse it, and use the fields accordingly?

Thanks in advance!

icon

Best answer by aman_trolley 16 April 2024, 06:41

View original

1 reply

Never mind, found it.

 

try{
...
}catch(error){

responseErrorJson = JSON.parse(error.message)
apiErrorJson = JSON.parse(responseErrorJson.content)
apiErrorArray = apiErrorJson.errors

//iterate through apiErrorArray
for (let i = 0; i < apiErrorArray.length; i++) {
...
}

}

 

Reply