ouroboros-network icon indicating copy to clipboard operation
ouroboros-network copied to clipboard

Inspect use of `error`

Open coot opened this issue 3 years ago • 0 comments
trafficstars

Unrecoverable errors, such as raised by error, might fall into various categories (at least)

  1. use in ’unreachable code’ / impossible to reach.
  2. invariants between various functions & modules have been violated.
  3. unexpected input from users, the environment, etc. Upon first glance, it appears all the instances of error fall into category 1. Suggestions:
  • When feasible, rewrite to eliminate the call to error. Of course, this is sometimes much more trouble than it’s worth.
  • When rewriting away is infeasible, use a panic (a la GHC source code, for practical purposes the same as error) to indicate this is ”category 1”. And even better, insert a comment as to ”why we believe it’s impossible vs why ghc doesn’t think it’s impoossible” (so the next coder who looks at the code doesn’t need to figure this all out for himself).

The result of the above would be the following:

  • No calls to error in program.
  • Unreachable/impossible code is clearly marked so.
  • Category 2 errors are caught with assert
  • Category 3 errors are all embedded into SomeException (as appears to be the case).

coot avatar Jun 27 '22 06:06 coot