-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
useAsyncStoryblok
status code checking on response, throws an error on error.value?.response.status
#329
Comments
We've already merged the fix from @SebbeJohansson 🎉 In case you face any other issue, let us know creating a new one <3 |
Hi @Dawntraoz, thanks for the update. I updated to the latest version of the Here's the screenshot of the diff, for the new So, as per my suggestion on the issue, integrating the PR #316 of @SebbeJohansson, this snippets will solve the problem, with the addition of having the compatibility with older and latest versions of Nuxt: const status = error.value?.response?.status || error.value?.status || error.value?.statusCode || false;
if (status >= 400 && status < 600) {
const message = error.value?.message.message || error.value?.message || error.value?.statusMessage || 'Something went wrong when fetching from storyblok.';
throw createError({ statusCode: status, statusMessage: message });
} Thanks! |
huh interesting! What exact versions did you test this on and do you have some code I can test it with? |
I can replicate it!
Number 3 might be something that should be changed in the js-client, but the others we can probably handle in this! My suggestion would be something like: const { data, error } = await useAsyncData<ISbResult, ISbError>(
`${uniqueKey}-asyncdata`,
() => storyblokApiInstance.get(`cdn/stories/${url}`, apiOptions),
);
const status = error.value?.response?.status || error.value?.status || (error.value as any)?.statusCode || 500;
if (status >= 400 && status < 600) {
const message = error.value?.message?.message || error.value?.message?.name || `${error.value?.message}` || 'Something went wrong when fetching from storyblok.';
throw createError({ statusCode: status, statusMessage: message });
} The error object for a proper 404 from storyblok would be: This still doesn't fix the issue where we are not returning the correct error type. I need to think more about this. What do you think? I added a fallback to 500 since that's just a general server error that can correspond to "Something went wrong when fetching from storyblok." since we don't know what happened. @Dawntraoz Can you open this again? |
@SebbeJohansson reopened, are you planning to work on it, or shall I? Let me know if you need any help. PS: It will be also nice to document the error handling in the README.md, if you can add it to your PR that would be amazing! |
@Dawntraoz Im trying to look at it, but I think this might be too much for me 😅 |
@SebbeJohansson yes I think it would be a correct approach, but onestly the only thing I'm not sure about it's the So inside the I cannot reproduce the example in which there's no ❯ npm ls nuxt
└── [email protected]
❯ npm ls @storyblok/nuxt
└── @storyblok/[email protected] I don't know if I understand correctly what you have written, but why do you need to cast the |
@Dru-S we have the same versions so that's good! I can reproduce the same issue using just purely the js-client without the nuxt3 module so I think The casting seems to be completely useless since Maybe it will have to be fine with just returning statusCode and the message? |
Then, I will ask the Nuxt 3 team for their input on our composable's error handling. I will keep you posted with the PR. |
@Dawntraoz Sounds like a good idea! I did some more testing and I couldn't get it to work! 😭 |
I got an answer from Daniel, he suggested me to use $fetch directly instead of asyncData. Also, that we wouldn't directly throw an error but return an error ref which gets filled up if there is an error. So I will work on a PR with the changes and send it to you to test it locally before merging 🫂 Let's improve the composable to be as good as we want/need 💪 |
Hi! Sorry for the late response, but been on a trip and then gotten ill sadly. Hopefully I'll be back in the next day or two. Using pure Sounds good. I'll try to test as soon as something is available. 🥳 |
Been Following up on this I may have a similar instance
I want to catch 404 urls how will i do that with the useAsynStoryblok |
@Maxino22 Until Storyblok has decided on how to officially do it, I think the best idea is to try-catch it.
|
Hey, @SebbeJohansson but
and the
Therefore nothing will be thrown. |
Moreover, it looks that
Of course, a string doesn't have a
wouldn't catch it. One more thing, just FYI. Let's take a look once again at the error returned from the
property
you can find it here https://github.com/storyblok/storyblok-js-client/blob/main/src/sbFetch.ts#L162 |
Hry @sumerokr for a case of using useAsyncStoryblok the solution to create a 404 error would it be to try catch the undefined returned or i do not quite understand?? |
@Maxino22 based on my experience that I have described above, 404 error will not be thrown. Therefore it cannot be catched.
|
@Dawntraoz any updates on this? |
Expected Behavior
The
status
property must be checked not only inside theerror.value.response
object, but also directly inside theerror.value
property of the response:Current Behavior
The
status
property is only checked inside theerror.value.response
, throwing acaught (in promise) TypeError: Cannot read properties of undefined (reading 'status')
error.Steps to Reproduce
useAsyncStoryblok
Additional informations
The text was updated successfully, but these errors were encountered: