fetch가 네트워크 오류가 발생했을 때만 예외를 발생시킨다
즉, HTTP 요청이 서버에 도달하고 서버가 응답을 보낼 수 있는 상태라면, HTTP 응답 상태 코드가 4xx 또는 5xx 범위라 할지라도 fetch는 예외를 발생시키지 않음
==> 이는 fetch가 네트워크 실패와 HTTP 오류를 다르게 취급한다
fetch('https://api.example.com/data')
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
})
.then(data => {
console.log(data); // 응답 데이터 처리
})
.catch(error => {
console.error('Network error or HTTP error:', error);
});