Error Handling Responsibility Principles

stoft

Rasmus Larsson

Posted on December 11, 2020

Error Handling Responsibility Principles

Principle 1

The code closest to the source of the error is responsible for providing as much detail about the error and if possible classifying it if such is needed.

Motivation: The further you get from the error the harder this becomes.

Principle 2

Code further up in the call stack is responsible for deciding on how to act on the error given the current context.

Motivation: Called code has no idea of the context.

Example

A DB error is thrown. The code closest to the error classifies it as a socket time out. This is bubbled up in the call stack which then decides to not retry because a user has decided that the query is no longer relevant.

Special Note

These principles apply not only within an in-memory call stack but also across e.g. micro-service boundaries.

Do you disagree? I would love your input.

💖 💪 🙅 🚩
stoft
Rasmus Larsson

Posted on December 11, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related