Last active
July 23, 2019 22:55
-
-
Save alukach/f7643e4b573a28abb7d3253dd179cf71 to your computer and use it in GitHub Desktop.
Typescript Favorites
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
In this file, we subclass the built-in Error object to create custom errors. | |
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error | |
*/ | |
// tslint:disable:max-classes-per-file | |
abstract class CustomError extends Error { | |
constructor(...params: any[]) { | |
// Pass remaining arguments (including vendor specific ones) to parent constructor | |
super(...params); | |
// Maintains proper stack trace for where our error was thrown (only available on V8) | |
if (Error.captureStackTrace) { | |
Error.captureStackTrace(this, CustomError); | |
} | |
} | |
get name() { | |
// Get error name from class name | |
return this.constructor.name; | |
} | |
} | |
export class Foo extends CustomError {} | |
throw new Foo("oops!"); | |
/* | |
▶ ts-node src/lib/errors.ts | |
/Users/alukach/Projects/devseed/maap/maap-api-query-service/src/lib/errors.ts:20 | |
export class Foo extends CustomError {} | |
^ | |
Foo: oops! | |
at new Foo (/Users/alukach/Projects/devseed/maap/maap-api-query-service/src/lib/errors.ts:20:1) | |
at Object.<anonymous> (/Users/alukach/Projects/devseed/maap/maap-api-query-service/src/lib/errors.ts:22:7) | |
at Module._compile (internal/modules/cjs/loader.js:701:30) | |
at Module.m._compile (/Users/alukach/.config/yarn/global/node_modules/ts-node/src/index.ts:439:23) | |
at Module._extensions..js (internal/modules/cjs/loader.js:712:10) | |
at Object.require.extensions.(anonymous function) [as .ts] (/Users/alukach/.config/yarn/global/node_modules/ts-node/src/index.ts:442:12) | |
at Module.load (internal/modules/cjs/loader.js:600:32) | |
at tryModuleLoad (internal/modules/cjs/loader.js:539:12) | |
at Function.Module._load (internal/modules/cjs/loader.js:531:3) | |
at Function.Module.runMain (internal/modules/cjs/loader.js:754:12) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment