Skip to content

Instantly share code, notes, and snippets.

@slavafomin
Last active June 3, 2025 17:54
Show Gist options
  • Select an option

  • Save slavafomin/b164e3e710a6fc9352c934b9073e7216 to your computer and use it in GitHub Desktop.

Select an option

Save slavafomin/b164e3e710a6fc9352c934b9073e7216 to your computer and use it in GitHub Desktop.

Revisions

  1. slavafomin revised this gist Nov 18, 2021. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion nodejs-custom-es6-errors.md
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,8 @@ module.exports = class AppError extends Error {
    // Saving class name in the property of our custom error as a shortcut.
    this.name = this.constructor.name;

    // Capturing stack trace, excluding constructor call from it.
    // Capturing stack trace, excluding constructor call from it
    // (this is probably no longer required in node >=8, see the comments)
    Error.captureStackTrace(this, this.constructor);

    // You can use any additional properties you want.
  2. slavafomin revised this gist Nov 18, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion nodejs-custom-es6-errors.md
    Original file line number Diff line number Diff line change
    @@ -95,4 +95,4 @@ try {

    # Feedback

    It works great for my application, however cirtisim is welcomed.
    It works great for my application, however criticism is welcomed.
  3. slavafomin revised this gist Oct 9, 2017. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions nodejs-custom-es6-errors.md
    Original file line number Diff line number Diff line change
    @@ -14,11 +14,11 @@ module.exports = class AppError extends Error {
    // Calling parent constructor of base Error class.
    super(message);

    // Capturing stack trace, excluding constructor call from it.
    Error.captureStackTrace(this, this.constructor);

    // Saving class name in the property of our custom error as a shortcut.
    this.name = this.constructor.name;

    // Capturing stack trace, excluding constructor call from it.
    Error.captureStackTrace(this, this.constructor);

    // You can use any additional properties you want.
    // I'm going to use preferred HTTP status for this error types.
  4. Slava Fomin II revised this gist Nov 28, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion nodejs-custom-es6-errors.md
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,7 @@ module.exports = class AppError extends Error {
    this.name = this.constructor.name;

    // You can use any additional properties you want.
    // I'm going to use preffered HTTP status for this error types.
    // I'm going to use preferred HTTP status for this error types.
    // `500` is the default value if not specified.
    this.status = status || 500;

  5. Slava Fomin II revised this gist Nov 28, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion nodejs-custom-es6-errors.md
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@ I've tried to make it as lean and unobtrusive as possible.
    module.exports = class AppError extends Error {
    constructor (message, status) {

    // Calling parent constrcutor of base Error class.
    // Calling parent constructor of base Error class.
    super(message);

    // Capturing stack trace, excluding constructor call from it.
  6. Slava Fomin II created this gist Sep 3, 2016.
    98 changes: 98 additions & 0 deletions nodejs-custom-es6-errors.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,98 @@

    Here's how you could create custom error classes in Node.js using latest ES6 / ES2015 syntax.

    I've tried to make it as lean and unobtrusive as possible.

    # Defining our own base class for errors

    **errors/AppError.js**

    ```js
    module.exports = class AppError extends Error {
    constructor (message, status) {

    // Calling parent constrcutor of base Error class.
    super(message);

    // Capturing stack trace, excluding constructor call from it.
    Error.captureStackTrace(this, this.constructor);

    // Saving class name in the property of our custom error as a shortcut.
    this.name = this.constructor.name;

    // You can use any additional properties you want.
    // I'm going to use preffered HTTP status for this error types.
    // `500` is the default value if not specified.
    this.status = status || 500;

    }
    };
    ```

    # Defining specific error types

    **errors/EmailTakenError.js**

    ```js
    module.exports = class EmailTakenError extends require('./errors/AppError') {
    constructor (message) {
    // Providing default message and overriding status code.
    super(message || 'Specified E-Mail is already taken', 400);
    }
    };
    ```

    **errors/RequestValidationError.js**

    ```js
    module.exports = class extends require('./AppError') {
    constructor (fields) {
    // Overriding both message and status code.
    super('Request validation failed', 400);
    // Saving custom property.
    this.fields = fields || {};
    }
    };
    ```

    # Throwing and catching

    ```js

    const AppError = require('./../api/errors/AppError');
    const EmailTakenError = require('./../api/errors/EmailTakenError');
    const RequestValidationError = require('./../api/errors/RequestValidationError');


    try {
    // Throwing EmailTakenError exception.
    throw new EmailTakenError();
    } catch (error) {
    // Catching exception by class.
    if (error instanceof EmailTakenError) {
    console.log('E-Mail validation failed!', error);
    } else {
    // If failed to catch, throwing it higher.
    console.log('Unknown error', error);
    throw error;
    }
    }


    try {
    // Throwing RequestValidationError exception.
    throw new RequestValidationError();
    } catch (error) {
    // Catching error by base (parent) class.
    if (error instanceof AppError) {
    console.log('Some application error occurred!', error);
    } else {
    console.log('Unknown error', error);
    throw error;
    }
    }
    ```

    # Feedback

    It works great for my application, however cirtisim is welcomed.