Skip to content

Instantly share code, notes, and snippets.

@tregusti
Created October 20, 2015 12:08
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tregusti/0b37804798a7634bc49c to your computer and use it in GitHub Desktop.
Save tregusti/0b37804798a7634bc49c to your computer and use it in GitHub Desktop.
function NotImplementedError(message) {
this.message = message || "";
}
NotImplementedError.prototype = Object.create(Error.prototype, {
constructor: { value: NotImplementedError },
name: { value: 'NotImplementedError' },
stack: { get: function() {
return new Error().stack;
}},
});
@jvorssel
Copy link

jvorssel commented Sep 4, 2017

A little contribution:
Shows what method threw the Error.

/**
 * @summary A error thrown when a method is defined but not implemented (yet).
 * @param {any} message An additional message for the error.
 */
function NotImplementedError(message) {
    ///<summary>The error thrown when the given function isn't implemented.</summary>
    const sender = (new Error)
        .stack
        .split('\n')[2]
        .replace(' at ','')
        ;

    this.message = `The method ${sender} isn't implemented.`;

    // Append the message if given.
    if (message)
        this.message += ` Message: "${message}".`;

    let str = this.message;

    while (str.indexOf('  ') > -1) {
        str = str.replace('  ', ' ');
    }

    this.message = str;
}

@erayerdin
Copy link

As an arrow function which exports:

/**
 * @summary A error thrown when a method is defined but not implemented (yet).
 * @param {any} message An additional message for the error.
 */
exports.NotImplementedError = (message) => {
  /// <summary>The error thrown when the given function isn't implemented.</summary>
  const sender = (new Error())
    .stack
    .split('\n')[2]
    .replace(' at ', '');
  this.message = `The method ${sender} isn't implemented.`;

  // Append the message if given.
  if (message) { this.message += ` Message: "${message}".`; }

  let str = this.message;

  while (str.indexOf('  ') > -1) {
    str = str.replace('  ', ' ');
  }

  this.message = str;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment