DEV Community

Discussion on: How to catch the body of an AXIOS error

Collapse
 
mohdahmad1 profile image
Mohd Ahmad • Edited

try

JS

 try {
  // with destructuring 
  const res = await axios.get('https://example.com/someApi');

  // without destructuring 
   const res = await axios.get('https://example.com/someApi');

    return res.data;
  } catch (err) {
    console.log(`Error: ${err?.response?.data}`);
  }
Enter fullscreen mode Exit fullscreen mode

For TypeScript

 try {
  // with destructuring 
  const res = await axios.get<{ feild: type} >('https://example.com/someApi');

  // without destructuring 
   const res = await axios.get<{ feild: type} >('https://example.com/someApi');

    return res.data;
  } catch (err) {
    console.log(`Error: ${(err as AxiosError)?.response?.data}`);
  }
Enter fullscreen mode Exit fullscreen mode
Collapse
 
anttibull profile image
anttibull

Thank you!