Consider this Vue 3 component using fetch inside onMounted. What will the user see if the HTTP request returns a 404 error?
import { ref, onMounted } from 'vue'; export default { setup() { const data = ref(null); const error = ref(null); onMounted(async () => { try { const response = await fetch('https://api.example.com/data'); if (!response.ok) { throw new Error('Network response was not ok'); } data.value = await response.json(); } catch (err) { error.value = err.message; } }); return { data, error }; }, template: `<div> <p v-if="error">Error: {{ error }}</p> <p v-else-if="data">Data loaded</p> <p v-else>Loading...</p> </div>` };
Think about what happens when response.ok is false and how the error is handled.
The code checks response.ok and throws an error if false. The catch block sets error.value, so the template shows the error message.
Identify the option that properly throws an error when the HTTP response is not successful.
async function fetchData() { const response = await fetch('https://api.example.com/items'); // Error handling here const data = await response.json(); return data; }
Check which condition correctly detects an error and throws an exception.
Option A throws an error when response.ok is false, which is the standard way to handle HTTP errors.
Look at the code below. The HTTP call fails but the error message never appears. What is the cause?
import { ref, onMounted } from 'vue'; export default { setup() { const error = ref(null); onMounted(async () => { try { const response = await fetch('https://api.example.com/data'); if (!response.ok) { return; } const data = await response.json(); } catch (e) { error.value = e.message; } }); return { error }; }, template: `<div> <p v-if="error">Error: {{ error }}</p> <p v-else>Loading...</p> </div>` };
Consider when the catch block runs and how errors from HTTP status codes are handled.
fetch() does not throw for HTTP error status codes (4xx/5xx); the promise resolves but response.ok is false. The if block returns early without setting error.value or throwing, so catch is skipped, error remains null, and 'Loading...' is shown indefinitely.
Given this code snippet, what will be the value of error.value after the fetch call if the server returns a 500 status?
import { ref } from 'vue'; const error = ref(null); async function loadData() { try { const response = await fetch('https://api.example.com/data'); if (!response.ok) { throw new Error(`Error: ${response.status}`); } const data = await response.json(); } catch (e) { error.value = e.message; } } await loadData();
Look at the error message thrown when response.ok is false.
The code throws an error with message including the status code. For a 500 status, the message is 'Error: 500'.
Choose the option that best describes a robust way to handle HTTP errors in Vue components to provide clear feedback to users.
Think about how to handle both network and HTTP errors gracefully in a reactive UI.
Option D describes the best practice: throwing errors on bad HTTP status, catching them, updating reactive state, and showing user-friendly messages.