How handle error 404: Not Found from within a helper function in Node js express and go to 404 page

I have two Javascript helper function to fetch some data from a GraphQL endpoint and creates a url. This all works fine:

const getData = async (targetUrl: string) => {
  const options = {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      query: GET_DATA,
      variables: {
        targetUrl,
      },
    }),
  };

  const response = await fetch(process.env.ENDPOINT, options);

  if (!response.ok) {
    throw new Error('Unable to parse json response');
  }

  const result = await response.json();

  if (result.errors?.length) {
    console.log('foo', result.errors);
    throw new Error('Something went wrong');
  }
  return result.data.page;
};

export default getData;

The small helper function which creates the url:

const getRedirectUrl = async (requestUrl: string) => {
  if (requestUrl.includes('/foo')) {
    const { content } = await getData(requestUrl);
    if (content.type === 'MyPage') {
      return `/my-page/?id=${content.id}`;
    }
  }
  return null;
};

export default getRedirectUrl;

Then in my server side route handler I use it like:

  if (redirectUrl) {
    res.redirect(redirectUrl);
    return;
  }

But when I request a non existing url (which contains /foo, I get a blank screen and it doesn’t go to 404 page. In terminal I get an error from getData() function:

Error: Something went wrong

When I inspect result.errors it gives:

[
  {
    message: '404: Not Found',
    extensions: {
      code: 'INTERNAL_SERVER_ERROR',
      serviceName: 'node',
      response: [Object],
      exception: [Object]
    }
  }
]

The same I see in GraphQL playground. The problem is that below query isn’t working because the targetUrl is not found.

query GetData($targetUrl: String!) {
    page (targetUrl: $targetUrl){
      content {
        ...on MyPage {
          id
          type
        }
      }
    }
  }

How to fix this? And how to somehow get out of the getData() function or go to 404 page?