While working on a project, you found out that the API you are fetching data from sends relevant error messages in the body of the response, even when the request fails. How do you extract and use this error message in JavaScript?
- Check the response.ok property to determine success and use response.statusText for error messages.
- Use the response.error property to access the error message.
- Parse the entire response body as a JSON object and access the error message field.
- Create a custom error handler function to retrieve the error message from the API.
To extract and use error messages from the API response, you should check the response.ok property to determine success or failure. If it's a failure, you can access the error message using response.statusText. The other options are not the standard way to extract error messages from a fetch response.
Loading...
Related Quiz
- In a do-while loop, when is the condition checked?
- What does the pop() method do to a JavaScript array?
- To add elements to an array at a specific index, you might use _______.
- How can you add a new item to the beginning of an array?
- Imagine you're reading a book about the history of web development. The chapter on JavaScript mentions a language that was developed almost simultaneously and competed with JavaScript in the early days. What is the name of this language?