Best answer

Return an empty array if z.response.status is 404

  • 26 April 2023
  • 1 reply
  • 199 views

Badge +1

I’m creating an integration that interacts with a REST API.  I’ve built a search action that wraps an API endpoint.  If a resource isn’t found, the API returns a 404.

I’m trying to test for a 404 error then return an empty array.  For some reason, this doesn’t work:

return z.request(options)
.then((response) => {

if (response.status === 200) {
// a search action requires an array to be returned, so wrap with []
return [ response.json['data'] ];
}
else if (response.status === 404) {
return [];
}
else {
response.throwForStatus();
}

});

What am I missing?

icon

Best answer by benjamink9 26 April 2023, 18:20

View original

This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

1 reply

Badge +1

This seems to work:

return z.request(options)
.then((response) => {

// a search action requires an array to be returned, so wrap with []
return [ response.json['data'] ];

})
.catch((error) => {
const message = JSON.parse(error.message)

if ( message.status === 404 ) { return []; }
else {throw error;}

});