Script
Script2y ago

✅ – 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);
}
};
4 Replies
ScriptyChris
ScriptyChris2y 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)
Script
Script2y ago
that makes sense oh an error is been thrown in my case though
reactibot
reactibot2y ago
This question has an answer! Thank you for helping 😄 If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/1043939115579740200
reactibot
reactibot2y ago
This thread hasn’t had any activity in 12 hours, so it’s now locked. Threads are closed automatically after 12 hours. If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/1043939115579740200 Question not getting answered? Maybe it's hard to answer, or maybe you asked at a slow time. Check out these resources for help asking a good question: https://stackoverflow.com/help/how-to-ask http://wp.me/p2oIwo-26