DEV Community

Cover image for Fault-Tolerance: An NPM Package to Format and Normalize Errors
Joe
Joe

Posted on

1

Fault-Tolerance: An NPM Package to Format and Normalize Errors

Developers don't spend enough time thinking about their errors. I see too many projects that throw Error('invalid data') or even worse throw 'invalid data' 😱. That isn't useful! Give me some context, bud.

But we (myself included) aren't usually thinking about the failure case. We are thinking about the solution and (at best) are simply guarding against some bad input. That needs to change. Errors need context to be useful. String interpolation isn't good enough - it still requires thought. An error databag is what we need.

So when I was tired of seeing less than helpful errors at work, I set out to create something that was effortless to use, and would make log messages incrementally better. What I ended up with is a project I am calling Fault-Tolerance. The concept behind it is trivial - extend the default Error object to format the output better.

In the most basic example you can throw new Fault('Move along'). In reality, though, that kind of error isn't as helpful as you want. Errors have context and we don't want to lose that.

function checkpoint(droids:[]){
  try{
    droids.forEach(d => {
      if(isWanted(d)) {
        if(jediIsPresent) {
          throw new Error('These are not the droids you are looking for');
        }
        detain(d);
      }
    })
  }
  catch(e) {
    // a Fault will preserve the original stacktrace
    throw new Fault(e, {droids: droids});
  }
}
Enter fullscreen mode Exit fullscreen mode

The output from that would look like:

Error: These are not the droids you are looking for
    at ......
    at ......
    at ......
metadata:
{
  "droids": [
      {
       "name": "R2D2",
       "owner": "Skywalker",
       "purpose": "astromech"
      },
      {
       "name": "C3PO",
       "owner": "Skywalker",
       "purpose": "comic relief" 
      }
  ]
}
Enter fullscreen mode Exit fullscreen mode

This gives you a much better way to include additional information with the added benefit of a consistent log format.

Check out the Fault-Tolerance on Gitlab for more examples. It is also available as an NPM package.

Photo by Pankaj Patel on Unsplash

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay