R
Reactiflux

✅ – Script – 17-21 Nov 20

✅ – Script – 17-21 Nov 20

SScript11/20/2022
createUser api function returns a 500 internal server error but the catch block does not catch the error I had to add a catch block after the .then method for it to catch that error whats the reason for this?
const submitProfile = async () => {
try {
createUser({ ...data, user_image: url.secure_url })
.then((res) => {
if (res) {
console.log(res)
})
} catch (err) {
console.error("THE ERR", err);
}
};
const submitProfile = async () => {
try {
createUser({ ...data, user_image: url.secure_url })
.then((res) => {
if (res) {
console.log(res)
})
} catch (err) {
console.error("THE ERR", err);
}
};
SScriptyChris11/20/2022
try..catch won't catch an error inside async function. You should await createUser() to make it work like that Promise .catch() method works, because it's attached directly to the promise, but try..catch is a sync mechanism, which is not aware of promise, so await has to be used in order to "transform" async promise to sync code (also, 500 HTTP status error may not cause fetch to throw an error https://developer.mozilla.org/en-US/docs/Web/API/fetch#exceptions , so if you don't have your own logic handling response from fetch, which transforms response codes to possible errors, then fetch most likely won't throw an error when 500 occurs)
SScript11/20/2022
that makes sense oh an error is been thrown in my case though
UUUnknown User11/21/2022
3 Messages Not Public
Sign In & Join Server To View

Looking for more? Join the community!

R
Reactiflux

✅ – Script – 17-21 Nov 20

Join Server