storyblok / storyblok-js

JavaScript SDK to connect Storyblok with your favourite framework that we don't have an official SDK for.
http://storyblok.com/
MIT License
39 stars 20 forks source link

Trying to load a slug that doesn't exist causes a UnhandledPromiseRejection #229

Closed lucasmerlin closed 1 year ago

lucasmerlin commented 1 year ago

With @storyblok/js 2.0.10-2.0.12, trying to load a story with a slug that doesn't exist, storyblok client causes a UnhandledPromiseRejection.


Expected Behavior

A error with statusCode 404 should be thrown.

Current Behavior

NodeJS crashes with a UnhandledPromiseRejection:

(node:76604) ExperimentalWarning: The Fetch API is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
node:internal/process/promises:288
            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'
}

Node.js v18.12.1
error Command failed with exit code 1.

Steps to Reproduce

Minimal reproduction (call with node main.mjs):

import {storyblokInit, apiPlugin} from "@storyblok/js";

const {storyblokApi} = storyblokInit({
    accessToken: "token",
    use: [apiPlugin],
});

storyblokApi.get("non-existing-slug")
    .catch((e) => {
        console.log("Caught error with status: ", e.response.status);
    })

Tried this with node 18.12.1 and 19.5.0.

Workaround

Installing @storyblok/js v2.0.9 fixes this

kb1995 commented 1 year ago

I'm getting similar errors while using 2.0 version storyblok-react which uses this library under the hood. I'll need to downgrade to v1 until this is fixed since currently all 404 pages result in 500

fgiuliani commented 1 year ago

Hi @lucasmerlin @kb1995, we introduced some fixes in the latest versions of the SDK. Can you update the SDK version and check if the error is still happening to you?

lucasmerlin commented 1 year ago

Seems like this has been working fine for us with recent versions, so I think this issue can be closed