Error, Snag handling & Bug reporting and more

2020/08/10
error, snag, bug, expert, glue, library, module, service, script
Error is basic data, throw & catch is a way to send event; An Error thrown can be a Snag or Bug, expected or unexpected; Expert code do Snag handling while Glue code don't; code packed as Module/Library/Service/Script required different focus;

More thought on Error, after Exot, Error handling, and Bugs May need more sorting, since not all topic is directly & naturally linked. For now may be the ".md" version will be easier to read.

Error, throw, and try-catch

Error is a special kind of event, contains a message and stacktrace.

The keyword throw cause the error to bubble up the callstack.

If the code thrown Error is inside the try block of try {} catch (error) {}, the catch block will get the error and run, else if the top callstack is reached, the program will log the error and exit.

Using try-catch and throw have effect similar to label and goto, and often the use of goto is considered bad, unlike the more limited if-else, for it can cause the code to skip/jump past hugh amount of code scopes and callstacks, and most bug/leak happen when the skipped code contain destructor/clear-up. Interestingly, some may abuse this goto behavior and use it for additional control flow.

And instead of rely on throw and rewind the callstack to try-catch to process Error, there's alternative to just return/pass Error to next function and if-else to do the handling, like in most callback (error, result) => {} and functional programming.

In the end, Error is still just basic data, it is us who decided to honor some contract to treat it specially. And Error should always be well-defined, classified, can be enumerated easily, it's a comment prepared for later debugging.

Whatever the method can reduce the complexity is fine, but since most language choose try-catch and throw, later discuss will also adopt this form of contract.

Snag & Bug

First a sync of words, we leave existing words used in code as-is: Error, throw, try-catch, and use the following words for the topic:

We need Snag to be clearly defined and correctly handled, and try eliminate Bug all the time.

Whether an Error is Snag or Bug depends on the actual code. If some check code throw a well-defined Error, and outer code already setup try-catch to receive it and correctly retry, this Error is a Snag. But if for the same situation, the outer code forget to trap the error, causing it leaks to higher stack, or even to the top callstack and cause an exit, the same Error is a Bug. So for an Error end up to be Snag but not Bug, the good Error throw code need to pair with in-time try-catch code.

And for the record:

For the expected and unexpected Error, we use different strategy: (Error handling)

Not all code need Error handling in the first place, most function with no external resource (IO) involved can be made 100% correct. And it's simpler to limit and reduce the Snag & Bug scope, less work is better. On the other side, more checks and Error do make Bug surface quicker, keep a balance. Often to apply Snag handling to IO related code:

And for general code from low to high:

And in short:

Expert & Glue

Next we divide the code quality:

Both quality standard have some use case, and most of the time:

Note that event Glue code should have clear structure and direct logic, and though there's less Snag handled, don't mean it should have Bugs. Messy Bugged code is just failed Bad code, and problem to be fixed.

Module, Library, Service, and Script

For different usage, we group code as:

For most project, 85% Expert code in Library/Module + 15% Glue code on top Service/Script may be a good choice.

Tie it all up

So Error can end up being Snag or Bug, in both Expert or Glue code, packed as Module/Library/Service/Script.

Suppose we are creating a repo for:

reference

https://en.wikipedia.org/wiki/Exception_handling#In_software