Skip to content

Instantly share code, notes, and snippets.

@janbaer
Last active November 2, 2020 17:31
Show Gist options
  • Save janbaer/f9a8a109f59a95904fdb8b00131b9e03 to your computer and use it in GitHub Desktop.
Save janbaer/f9a8a109f59a95904fdb8b00131b9e03 to your computer and use it in GitHub Desktop.

Revisions

  1. janbaer revised this gist Nov 2, 2020. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions javascript-improved-error-handling.js
    Original file line number Diff line number Diff line change
    @@ -33,14 +33,14 @@ class ServerError extends Error {
    }

    class DatabaseError extends ServerError {
    constructor(args) {
    super(args)
    }

    }

    function dbFunction() {
    throw new DatabaseError('Unexpected error while doing something with the database.');
    try {
    throw new Error('Mongo was throwing an error');
    } catch (err) {
    throw new DatabaseError('Unexpected error while doing something with the database.', err);
    }
    }

    function callDbFunction() {
    @@ -68,9 +68,9 @@ function serverFunction() {
    }

    try {
    // callDbFunction();
    callDbFunction();
    // handleAnyError();
    serverFunction();
    // serverFunction();
    } catch (err) {
    console.error(err.name, err.message, os.EOL, err.stack);
    // console.log(err);
  2. janbaer revised this gist Nov 2, 2020. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions javascript-improved-error-handling.js
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,7 @@ class ServerError extends Error {
    this.name = this.constructor.name;
    this.innerError = innerError;

    // Borrowed from: https://stackoverflow.com/questions/35392675/how-to-override-error-stack-getter
    Object.defineProperty(this, 'message', {
    get: () => {
    if (this.innerError) {
  3. janbaer created this gist Nov 2, 2020.
    76 changes: 76 additions & 0 deletions javascript-improved-error-handling.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,76 @@
    const os = require('os');

    class ServerError extends Error {
    constructor(message, innerError) {
    super();
    this.name = this.constructor.name;
    this.innerError = innerError;

    Object.defineProperty(this, 'message', {
    get: () => {
    if (this.innerError) {
    return `${this._message}: ${this.innerError.name} - ${this.innerError.message}`;
    }
    return this._message;
    },
    set: value => this._message = value
    });
    this.message = message;

    Object.defineProperty(this, 'stack', {
    get: () => {
    const lines = (this.innerError ? this.innerError.stack : this._stack).split(os.EOL);
    while (lines[0].startsWith('Error') || lines[0].startsWith(' at new ')) {
    lines.splice(0, 1);
    }
    return lines.join(os.EOL);
    },
    set: value => this._stack = value
    });
    this.stack = new Error().stack;
    }
    }

    class DatabaseError extends ServerError {
    constructor(args) {
    super(args)
    }

    }

    function dbFunction() {
    throw new DatabaseError('Unexpected error while doing something with the database.');
    }

    function callDbFunction() {
    try {
    dbFunction();
    } catch (err) {
    throw new ServerError('Something went wrong in the server', err);
    }
    }

    function letsThrowNormalError() {
    throw new Error('This is a simple error');
    }

    function handleAnyError() {
    try {
    letsThrowNormalError();
    } catch (err) {
    throw new ServerError('A normal error occurred', err);
    }
    }

    function serverFunction() {
    throw new ServerError('Our server won\'t work anymore.');
    }

    try {
    // callDbFunction();
    // handleAnyError();
    serverFunction();
    } catch (err) {
    console.error(err.name, err.message, os.EOL, err.stack);
    // console.log(err);
    }