Wednesday 24 June 2020

[Ask] Should You Centralize Error Handling?

I was wondering about this, and forgive me if I misunderstood it, but there seems to be a difference in opinion on handling errors.My question is:Should both operational and programmer errors be handled in a centralized place?References:Against Central Error Handling (or at least I interpreted it that way)Handling operational errorsJust like performance and security, error handling isn’t something that can be bolted onto a program that has no error handling already. Nor can you centralize all error handling in one part of the program, the same way you can’t centralize “performance” in one part of the program. Any code that does anything which might possibly fail (opening a file, connecting to a server, forking a child process, and so on) has to consider what happens when that operation fails. That includes knowing how it may fail (the failure mode) and what such a failure would indicate. More on this later, but the key point here is that error handling has to be done in a fine-grained way because the impact and response depend on exactly what failed and why.You may end up handling the same error at several levels of the stack. This happens when lower levels can’t do anything useful except propagate the error to their caller, which propagates the error to its caller, and so on. Often, only the top-level caller knows what the appropriate response is, whether that’s to retry the operation, report an error to the user, or something else. But that doesn’t mean you should try to report all errors to a single top-level callback, because that callback itself can’t know in what context the error occurred, what pieces of an operation have successfully completed, and which ones actually failed.- https://www.joyent.com/node-js/production/design/errors​For Central Error Handling2.4 Handle errors centrally, not within an Express middlewareTL;DR: Error handling logic such as mail to admin and logging should be encapsulated in a dedicated and centralized object that all endpoints (e.g. Express middleware, cron jobs, unit-testing) call when an error comes inOtherwise: Not handling errors within a single place will lead to code duplication and probably to improperly handled errors- https://github.com/goldbergyoni/nodebestpractices#-24-handle-errors-centrally-not-within-an-express-middleware

Submitted June 24, 2020 at 01:26PM by codingwithmanny

No comments:

Post a Comment