삽질북

axios 에러코드 처리

핛생3 2021. 5. 9. 05:19
axios({
    url: `$requestUrl`
  }).then(response => {
    callback(parentId, id);
  }).catch(response => {
    console.log(response);
    errorCallback(response);
  });

 

axios 호출시 catch 인자로 들어오는 것은 데이터가 아니라 error 자체이다. console.log 찍어보면 다음과 같다.

 

Error: Request failed with status code 404

at createError (createError.js:16)

at settle (settle.js:17)

at XMLHttpRequest.handleLoad (xhr.js:59)

 

그래서 서버에서 내려준 데이터를 쓰고 싶을 때는 다음과 같이 써야한다. 어찌 생각하면 catch(e)가 당연한.. 

catch(error => {
  errorCallback(error.response);
})

 

 

더 자세한 axios 에러 핸들링은 아래에서 확인할 수 있다.

 

github.com/axios/axios#handling-errors

axios/axios

Promise based HTTP client for the browser and node.js - axios/axios

github.com