Difference between Fault, Error, and Failure explained with JavaScript

January 17, 2021 · 1 min read
Scrabble pieces showing the sentence: "THIS SENTENCE CONTAINS THREEE ERORS"
Photo by Brett Jordan on Unsplash

You've heard of the terms fault, error, and failure.

And as software developers, we use them every day. But what do they mean?

One popular definition is this (see “Fundamental Concepts of Dependability”, Avižienis et al.):

  • Fault is the cause of an error
  • Error is the incorrect state of the system that may cause a subsequent failure
  • Failure is when the system does not deliver the expected result

To better understand this definition, let's look at a concrete example.

Imagine a JS function that calls an external API to get search results.

const result = JSON.parse(response.payload)

Let's assume the response object is always present.

But when there is no payload property, a SyntaxError when running that code:

  • The fault is that response.payload is not checked to be a JSON-parsable type
  • The error that appears is a SyntaxError, because we try to call JSON.parse(undefined)
  • A failure will now appear if the error is not caught and either the app stops working, or the user gets no results and no error message

This example is a bit trickier than we see at first glance:

  • There will be no failure if the error is caught using try and catch and handled appropriately
  • If the error is caught, you can't really speak of a fault, because the program works as expected
  • But even without fault and failure, the error will still exist for a brief moment, before being caught

Now the big question, where does a bug fit into this?

The bug can be the fault or the failure, depending on context.

  • Bug as fault: “this code has a bug, it will throw an error when the list has no items. Better add an if to handle this case.”
  • Bug as failure: “the software has a bug when I click ‘delete’ with no items on the list.”

So a bug can actually be two things! Who would've thought 🤓