Skip to main content
Best answer

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

  • April 26, 2023
  • 1 reply
  • 250 views

Forum|alt.badge.img+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?

Best answer by benjamink9Best answer by benjamink9

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;}

  });

 

View original
Did this topic help you find an answer to your question?
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

Forum|alt.badge.img+1
  • Author
  • Beginner
  • 14 replies
  • Answer
  • April 26, 2023

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;}

  });