Awesome @Catch Decorator
$ npm install awesome-catch-decorator
Signature
Catch(ErrorType: Error, handler: (e) => any)
handler result value will replace method's return if exception is raised.
Usage
@ static { return JSON } // will always return an objectAnyES6ClassAnyES6ClassAnyES6Class
I just want to catch all errors!
// create a new decoratorconst CatchAll = @CatchAll { ; } // no exception will be raised
I just want to catch errors and trigger different actions for each one!
@ @ async { await this; await this; await this; }
code above do the following:
-
if
mayThrowCustomError1
throws aCustomError1
, thenhandleError1
will be executed -
if
mayRejectCustomError2
returns a promise which rejects withCustomError2
, thenhandleError2
will be executed -
if another kind of exception is raised, lets say a
TypeError
, then exception will be thrown.
How
This library was easily made with kaop-ts with provides hooks to attach behaviors on ES6 classes.
Credits
@lukeed for improving the initial idea.