I often see repeatable errors in my console that I'd like to programmatically respond to but I can't figure out how to access and parse them in my React application. In certain situations when the source of the error is known, I know error boundaries or try/catch blocks can work. There are many situations where I have no idea where the error originates from though, so I don't know what code to wrap in an error boundary.
As one specific example, I often see this SSL related networking error in my console: NET::ERR_CERT_AUTHORITY_INVALID.
When that error appears, I want to popup instructions to the user on our working fix. It's not clear exactly what code is generating that log though, so I'd like to simply detect when it pops up in the log and respond. Is this possible and is there a better approach?
Edit: Here's an example where I attempt a simple axios fetch to a site with a known network problem. In this case the error has no information about what the error is, yet in the console.log I can clearly see the error was ERR_CERT_AUTHORITY_INVALID. Is there no way for me to just retrieve that error message from the console?
const fetch = async () => {
try {
const response = await axios.get('https://self-signed.badssl.com/');
console.log(response);
} catch (error) {
if (axios.isAxiosError(error)) {
console.log(error.toJSON());
return error.message;
} else {
console.log('unexpected error: ', error);
return 'An unexpected error occurred';
}
};
Image of resulting console log.
Here's a codesandbox with the above code in a basic react application. Codesandbox doesn't show the error though so it needs to be run locally so google chrome dev tools can be used to see the error.
https://codesandbox.io/s/silly-babbage-jl11wb?file=/src/App.js