Handling Uncaught Promise Rejection in React 18 with Axios
I'm working on a personal project and I'm trying to debug I'm deploying to production and I'm working on a personal project and I've looked through the documentation and I'm still confused about I'm working on a React 18 application and I've noticed that my error handling for Axios requests isn't functioning as expected... Specifically, I'm trying to catch errors for a POST request made in an async function, but I'm getting an uncaught promise rejection warning in the console, which makes it seem like my error handling isn't being triggered at all. I've set up an Axios instance and tried using both `.catch()` and a `try/catch` block around my async function, but neither works as intended. Here's the relevant part of my code: ```javascript import axios from 'axios'; const api = axios.create({ baseURL: 'https://myapi.com', }); async function submitData(data) { try { const response = await api.post('/submit', data); console.log(response.data); } catch (error) { console.error('Error submitting data:', error); // Here, I expect to catch the error, but it doesnโt work as expected } } const handleSubmit = async (event) => { event.preventDefault(); const formData = { name: 'John Doe' }; // Sample data await submitData(formData); }; // Inside a React component return <button onClick={handleSubmit}>Submit</button>; ``` When I trigger the submit, if the server is down or the endpoint is incorrect, I still see an uncaught promise rejection in my console. I'm using Axios version 0.26.1 and React version 18.0.0. I've also tried wrapping the `handleSubmit` function in a `try/catch` block, but it doesnโt seem to catch the errors from the `submitData` function either. Is there a specific pattern I should use for handling these kinds of errors in React combined with Axios? Any insights would be appreciated! This is part of a larger API I'm building. Has anyone else encountered this? I'm working on a service that needs to handle this. What's the best practice here? This is for a service running on Debian. Any pointers in the right direction? I'm working on a desktop app that needs to handle this. Has anyone dealt with something similar? I'm developing on Ubuntu 22.04 with Javascript. Any help would be greatly appreciated!