1.0.2 • Published 6 months ago

enhanced-exception v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

Have you come recently from Java or C# world into JavaScript and you start wondering how the hell we are living here with the basic Error constructor? Yea, our stack traces aren't well descriptive, BUT here it is - Exception class just for you! Dive deep into the Quick showcase and see how this little lib can significantly improve your stack traces today.

Tip of the day - If you are coding in TypeScript, remember to output source maps and also enable them in Node.

Table of contents

Quick showcase

For those who don't like to read a lot.

Code

async function accessFileSystemFile() {
  // some neat code here...
  throw new Error('I/O Error');
}

async function readLatestLog() {
  try {
    // some neat code here...
    return await accessFileSystemFile();
  } catch(e: unknown) {
    throw new Exception('Unable to read log file.', e);
  }
}

async function bootstrap() {
  try {
    await readLatestLog();
  } catch (e: unknown) {
    throw new Exception('Failed to do some dangerous stuff.', e);
  }
}

void bootstrap();

Stack trace

Exception: Failed to do some dangerous stuff.
    at file:///path/to/project/src/real-world-example.js:26:19
    at Generator.throw (<anonymous>)
    at rejected (/path/to/project/node_modules/tslib/tslib.js:113:69)
Caused by: Exception: Unable to read log file.
    at file:///path/to/project/src/real-world-example.js:16:19
    at Generator.throw (<anonymous>)
    at rejected (/path/to/project/node_modules/tslib/tslib.js:113:69)
Caused by: Error: I/O Error
    at file:///path/to/project/src/real-world-example.js:6:15
    at Generator.next (<anonymous>)
    at /path/to/project/node_modules/tslib/tslib.js:115:75
    at new Promise (<anonymous>)
    at __awaiter (/path/to/project/node_modules/tslib/tslib.js:111:16)
    at accessFileSystemFile (file:///path/to/project/src/real-world-example.js:4:12)
    at file:///path/to/project/src/real-world-example.js:13:26
    at Generator.next (<anonymous>)
    at /path/to/project/node_modules/tslib/tslib.js:115:75
    at new Promise (<anonymous>)

Installation

npm install --save enhanced-exception

or

yarn add enhanced-exception

API

Exception class
elementsignaturecomment
constructor(message?: string, previous?: unknown)Creates new Exception class object.
.messagestringException message, the same as in classic Error object.
.namestringException class name, the same as in classic Error object.
.previousunknownPrevious error, provided via constructor.

Custom exceptions

Thanks to the fact that PlayerNotFoundException class extends Exception class, it also inherits all it's capabilities.

class PlayerNotFoundException extends Exception {
  playerName: string;

  constructor(playerName: string, previous?: unknown) {
    super(`Player "${playerName}" not found.`, previous);
    this.playerName = playerName;
  }
}
try {
  // some neat code here...
  throw new Error('Database record not found');
} catch(e: unknown) {
  throw new PlayerNotFoundException('IdkMan2', e);
}
PlayerNotFoundException: Player "IdkMan2." not found.
    at file:///path/to/project/src/real-world-example.js:13:11
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25)
Caused by: Error: Database record not found
    at file:///path/to/project/src/real-world-example.js:10:11
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25) {
  playerName: 'Failed to do some dangerous stuff.'
}