Skip to content

Instantly share code, notes, and snippets.

@okovalov
Last active July 13, 2020 16:30
Show Gist options
  • Select an option

  • Save okovalov/8a85c75f7c963ca7806220e90fe7b517 to your computer and use it in GitHub Desktop.

Select an option

Save okovalov/8a85c75f7c963ca7806220e90fe7b517 to your computer and use it in GitHub Desktop.

Revisions

  1. okovalov revised this gist Jul 13, 2020. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion index.js
    Original file line number Diff line number Diff line change
    @@ -25,4 +25,3 @@ try {
    }

    // from https://flaviocopes.com/javascript-custom-errors/

  2. okovalov revised this gist Jul 13, 2020. 1 changed file with 28 additions and 1 deletion.
    29 changes: 28 additions & 1 deletion index.js
    Original file line number Diff line number Diff line change
    @@ -1 +1,28 @@
    ‎‎
    class OutOfFuelError extends Error {
    constructor(message) {
    super(message)
    this.name = "OutOfFuelError"
    }
    }

    class FlatTireError extends Error {}

    try {
    const car = new Car() //imagine we have a Car object

    if (!car.fuel) {
    throw new OutOfFuelError('No fuel!')
    }
    if (car.flatTire) {
    throw new FlatTireError('Flat tire!')
    }
    } catch (err) {
    if (err instanceof OutOfFuelError) {
    //handle error
    } else if (err instanceof FlatTireError) {
    //handle error
    }
    }

    // from https://flaviocopes.com/javascript-custom-errors/

  3. okovalov created this gist Jul 13, 2020.
    1 change: 1 addition & 0 deletions index.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    ‎‎