safe-decorator
v0.1.2
Published
decorator function to catch and return default value
Downloads
4
Readme
safe-decorator
decorator function to catch and return default value
##usage
###@safe
or @safe()
method with @safe
or @safe()
will return null when exception thrown.
import safe from 'safe-decorator';
class Subject {
@safe
subjectMethod() {
throw new Error('error');
}
}
const subject = new Subject();
expect(subject.subjectMethod()).toBe(null);
###@safe(defaultValue)
method with @safe(defaultValue)
return defaultValue
when exception thrown.
import safe from 'safe-decorator';
class Subject {
@safe('default value')
subjectMethod() {
throw new Error('error');
}
}
const subject = new Subject();
expect(subject.subjectMethod()).toBe('default value');