Error Handling in TS-JS

logging and try catching

console.logs should be eliminated (at least in production env). They can give insights to how the app works to malicious users and serve no purpose to real users
a common thing to do is to use a console wrapper that implements all methods (log, error, warn) and add an if statement that checks current env (logger.log)

what happens then to all the catch statements that just log and do nothing else?
catch statements should never just log and do nothing else bc the app may be in a unpredictable state.
catch blocks should focus on 2 critical actions: reporting and failing gracefully. The action depends on whether the error is recoverable or critical.

Instead of dumping logs on browser console you should report the error to a centralized monitoring service.

After reporting the catch block should decide on how to handle the user xp:

Critical (unrecoverable) errors:
If the error means the current operation cannot complete (e.g., failed to fetch critical user data), the flow must stop.

Recoverable Errors:
If the error is minor and the application can continue (e.g., failed to load a non-essential widget), you can handle it more lightly.

Exceptions vs Errors

Feature Error Exception
Severity Critical Moderate
Recoverable? Usually not Often
Examples OutOfMemoryError, SyntaxError FileNotFound, ZeroDivision
Handling Typically not handled Use try / catch / except
When occurs Compile-time or runtime Runtime

JS Error handling patterns

https://betterprogramming.pub/to-throw-or-not-to-throw-error-propagation-in-js-and-ts-68aaabe30e30
https://medium.com/front-end-weekly/error-propagation-in-javascript-with-error-translation-pattern-78cf7178fe92

monad pattern and Effect lib

The most important function in my codebase - Theo t3.gg
This vid goes over the monad pattern implementation Theo uses for his code base
and goes over other solutions as well

Goes over with examples:

tryCatch() -> neverthrow -> Effect is a good progression to incrementally add to my code
tryCatch() to start thinking about explicitly handling errors and typing
neverthrow for when the layering (nested error returns) of tryCatch wrappers get more and more complex

Effect you get functional programming pilled, complex wrappers for fetch and logs