import { catchError, CatchCallback } from 'catch-error-decorator';
const logFunction: CatchCallback = (errMessage: string, errStack: string, funcName: string, className: string, context: any, args: any[]) => {
logger.log({level: 'error', message: JSON.stringify({errMessage, funcName, className, context})});
}
class Foo {
@catchError(logFunction, false, 'defaultValue')
method1(): string {
throw new Error('bar');
}
@catchError(logFunction, false, 'defaultValue')
method2(): Promise<string> {
return Promise.reject(new Error('bar'));
}
@catchError({ catchFunction: logFunction, throwErr: true })
async method(): Promise<any> {
const value = await (async function() { throw new Error('baz'); })();
return value;
}
@catchError(logFunction)
get property() {
throw new Error('deprecated method');
}
@catchError(logFunction, false)
static Method() {
return Promise.reject(new Error('baz'));
}
@catchError(logFunction)
static get Property() {
return 123;
}
}
function throwAlarm(arg1, arg2) {
const value = this.Property;
console.log(value);
throw new Error('alarm');
}
const catchErrorOptions = {
catchFunction: logFunction,
throwErr: false,
defaultValueOnError: 'baz',
bindContext: Foo
};
const result = catchError(catchErrorOptions)(throwAlarm)('000', '123');