✅ – Script – 17-21 Nov 20

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);
}
};
S
ScriptyChris515d ago
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)
S
Script515d ago
that makes sense oh an error is been thrown in my case though
UU
Unknown User514d ago