{"id":730,"hash":"05a2d8b5406ce9a42093d6e53e5eb7811e633326b1664cf6b3957abc5529a0d1","pattern":"Axios. How to get error response even when api return 404 error, in try catch finally","full_message":"for e.g.\n\n(async() => {\n  let apiRes = null;\n  try {\n    apiRes = await axios.get('https://silex.edgeprop.my/api/v1/a');\n  } catch (err) {\n    console.error(err);\n  } finally {\n    console.log(apiRes);\n  }\n})();\n\nin finally, apiRes will return null. \n\nEven when the api get a 404 response, there is still useful information in the response that I would like to use.\n\nHow can I use the error response in finally when axios throws error.\n\nhttps://jsfiddle.net/jacobgoh101/fdvnsg6u/1/","ecosystem":"npm","package_name":"error-handling","package_version":null,"solution":"According to the documentation, the full response is available as a response property on the error.\n\nSo I'd use that information in the catch block:\n\n(async() => {\n  let apiRes = null;\n  try {\n    apiRes = await axios.get('https://silex.edgeprop.my/api/v1/a');\n  } catch (err) {\n    console.error(\"Error response:\");\n    console.error(err.response.data);    // ***\n    console.error(err.response.status);  // ***\n    console.error(err.response.headers); // ***\n  } finally {\n    console.log(apiRes);\n  }\n})();\n\nUpdated Fiddle\n\nBut if you want it in finally instead, just save it to a variable you can use there:\n\n(async() => {\n  let apiRes = null;\n  try {\n    apiRes = await axios.get('https://silex.edgeprop.my/api/v1/a');\n  } catch (err) {\n    apiRes = err.response;\n  } finally {\n    console.log(apiRes); // Could be success or error\n  }\n})();","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/48298890/axios-how-to-get-error-response-even-when-api-return-404-error-in-try-catch-fi","votes":91,"created_at":"2026-04-19T04:51:33.562539+00:00","updated_at":"2026-04-19T04:51:33.562539+00:00"}