The Strange World of Throwing Errors on Non-OK HTTP Responses
The Hypnotic Trend
Can anyone think of a good reason why the masses of JavaScripters decided that the norm is to throw an error on non-OK HTTP responses? Because I can’t. To me, this is a terrible practice, and everyone just seems hypnotized by it, like moths to the flame.
The Consequences of Throwing Errors
When a non-OK HTTP response is received, the browser throws an error, which can have severe consequences. For instance, it can:
- Crash the application: When an error is thrown, the application may crash, taking the user’s progress and unsaved work with it.
- Freeze the browser: If the error is not handled properly, the browser may freeze, leaving the user stuck in an endless loading loop.
- Leak memory: Unhandled errors can lead to memory leaks, causing the browser to consume more and more memory, leading to poor performance and eventual crashes.
Why Not Just Ignore the Error?
Why not just ignore the error and move on? Ignoring the error would allow the application to continue running, minimizing the risk of crashing or freezing. This approach would also:
- Improve user experience: By ignoring the error, the application can continue to function, providing a better user experience.
- Reduce errors: Ignoring the error reduces the likelihood of errors propagating up the call stack, making it easier to debug and maintain the application.
The Misconception of "Error-First" Approach
The "error-first" approach, where errors are thrown on non-OK HTTP responses, is often justified by the need to "fail fast" and "fail early." However, this approach is misguided. Failure is not the goal; it’s how we handle failure that matters.
Conclusion
The practice of throwing errors on non-OK HTTP responses is a relic of the past, a holdover from a time when error handling was not as sophisticated. It’s time to rethink this approach and adopt a more modern, user-centric approach to error handling. By ignoring errors and moving on, we can improve the user experience, reduce errors, and create more robust applications.
Frequently Asked Questions
Q: Why do developers still throw errors on non-OK HTTP responses?
A: Many developers are stuck in the "error-first" mindset, believing that throwing errors is the best way to handle non-OK HTTP responses.
Q: Is it possible to ignore errors and move on?
A: Yes, it is possible to ignore errors and move on, providing a better user experience and reducing the risk of errors propagating up the call stack.
Q: What are the benefits of ignoring errors?
A: Ignoring errors can improve user experience, reduce errors, and make it easier to debug and maintain the application.

