0

I'm really frustrated for not figuring out how to handle the error. I've tried many methods on the internet like the try catch one, and .catch, but nothing worked. I'm just trying to handle an error that will occur in an async function. The last method I tried was this one:

async function getData(key) {
data = await fetch(key);
data = await data.json()
alert('done');
}

async function checkData(key) {
try{
    await getData(key)
} catch(err) {
    alert('error')
}

In the end, all I want is to notify the user about the error with alert and also not show that error in the console (Like it does without the handling). And by error, I mean errors that comes back from the server like a 404 Not Found or a Bad Request.

1
  • fetch does not throw depending on the status of responses received from the server. I believe it only throws for a network error. Check documentation for how to determine if a request failed. Commented Apr 8, 2023 at 23:36

1 Answer 1

-1

When you catch error, you can get message by err.message. And you can write switch by error types. All depends what API you use and what error responses this API have. Some API have error codes (err.code) and user-friendly error responses.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.