storyblok-js-client: UnhandledPromiseRejection when trying to retrieve a story that doesn't exist

I originally found this in storyblok/react using this code:

const storyblokApi = getStoryblokApi();
const { data } = await storyblokApi.get('/cdn/stories/nonexistent', {
    version: 'draft'
});

Expected Behavior

I would expect the above to throw an error or return a not-found. Using curl and hitting the api directly, I get:

curl -L "https://api.storyblok.com/v2/cdn/stories/${slug}?token=${token}&version=${version}" -X GET \
-H "Accept: application/json" -H "Content-Type: application/json"
[
    "This record could not be found"
]
with a 404 status.

Actual Behavior

Instead it just hangs. I waited 45 minutes, so I don’t think it’s a timeout issue.

So I went down this lower-level JS client to debug:

const Storyblok = new StoryblokClient({
  accessToken: 'my-token',
});

Storyblok.get('cdn/stories/nonexistent', {
  version: 'draft',
}).then((response) => {
  console.log(response?.data);
}).catch((ex) => {
  console.error(ex);
});

This is the response:

node:internal/process/promises:279
            triggerUncaughtException(err, true /* fromPromise */);
            ^

[UnhandledPromiseRejection: This error originated either by throwing inside of an async function \
without a catch block, or by rejecting a promise which was not handled with .catch(). The promise \
rejected with the reason "#<Object>".] \
{
  code: 'ERR_UNHANDLED_REJECTION'
}

This started in between 5.3.3 and 5.3.4. 5.3.3 correctly returns a not found. 5.3.4 displays the error above. I suspect storyblok/react is not handling the error, but it seems like the real problem is in storyblok-js-client.

Updated after testing 5.3.0-4. I don’t see the problem until 5.3.4.

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 3
  • Comments: 18 (2 by maintainers)

Most upvoted comments

@thiagosaife makes sense 👍

Still seeing the issue in 5.3.5:

 $ grep storyblok-js yarn.lock
storyblok-js-client@^5.3.5:
  resolved "https://registry.yarnpkg.com/storyblok-js-client/-/storyblok-js-client-5.3.5.tgz#327a043aa25914e62613005955d0179517c78d18"
sb $ cat test.js|sed "s/Token: '.*'/Token: 'My token'/"
require('isomorphic-fetch');
const StoryblokClient = require('storyblok-js-client')
 
const Storyblok = new StoryblokClient({
  accessToken: 'My token',
});

Storyblok.get('cdn/stories/reviews1', {
  version: 'draft',
}).then((response) => {
  console.log(response?.data);
}).catch((ex) => {
  console.error(ex);
});
sb $ node test.js
node:internal/process/promises:279
            triggerUncaughtException(err, true /* fromPromise */);
            ^

[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "#<Object>".] {
  code: 'ERR_UNHANDLED_REJECTION'
}