Search code examples
typescriptexpress-validator

Typescript: What is the error type of 'throw' function in express-validator?


I need to detect the error type that this function throws:

validationResult(req).throw()

This is the defenition of throw function:

throw() {
    if (!this.isEmpty()) {
        throw Object.assign(new Error(), utils_1.bindAll(this));
    }
}

And this is the utils_1.bindAll function:

exports.bindAll = (object) => {
    const protoKeys = Object.getOwnPropertyNames(Object.getPrototypeOf(object));
    protoKeys.forEach(key => {
        const maybeFn = object[key];
        if (typeof maybeFn === 'function' && key !== 'constructor') {
            object[key] = maybeFn.bind(object);
        }
    });
    return object;
};

It seems that throw() function doesn't throw an error with a specific type but I need to somehow find it because I want to handle express-validator errors in a particular way.


Solution

  • According to the source code of express-validators:

    throw() {
        if (!this.isEmpty()) {
          throw Object.assign(new Error(), bindAll(this));
        }
      }
    

    So it's basically just a "normal" Error Type.