The error module provides a custom Error
object meant to be used by
filter functions. This is mostly useful to catch errors related to
attribute value filtering.
For all API functions listed:
T
represents any data type.AttrName
is a string and represents an attribute name.AttrValue
is a string or boolean and represents a filtered attribute value.
The FilterError
object represents an error with a value being filtered.
new FilterError(message: string, options?: object)
A static method to format the attribute name or value.
This method is used by the static creator method to generate a descriptor for the placeholder token.
FilterError.describeAttr(value?: T, name?: AttrName): string
FilterError.describeAttr(null);
// → 'null'
FilterError.describeAttr(undefined);
// → 'undefined'
FilterError.describeAttr(true);
// → 'boolean'
FilterError.describeAttr(42);
// → 'number'
FilterError.describeAttr([ 1, 2, 3 ]);
// → 'Array'
FilterError.describeAttr({});
// → 'Object'
FilterError.describeAttr(new Date());
// → 'Date'
FilterError.describeAttr('hello');
// → 'string'
FilterError.describeAttr('hello', 'class');
// → 'attribute [class]'
A static method to create a new error object with a message where {attr}
is a placeholder that will be substituded with a name or value.
FilterError.create(
message: string,
value: T,
name?: AttrName,
options?: object
): FilterError
function filterNumber(value, name) {
if (typeof value === 'number') {
return value;
}
throw FilterError.create('{attr} is not a number', value, name);
}
filterNumber(true);
// → Uncaught FilterError: boolean is not a number