Как в express-validaor вернуть не стандартый объект ошибки, а мою кастомную ошибку

К примеру:

функция которая будет в custom()

const isOrganizationExistsByName = async (name: string) => {
    const latch = await getOrganization({ name });
    if (latch) {
        throw new CustomError( `The organization with the same name - ${ name } is already exists!`, 401);
    }
    return true;
};

Сама валидация

const nameValidation = body('name')
                        .exists()
                        .withMessage('The field name is required.')
                        .isString()
                        .bail()
                        .custom(isOrganizationExistsByName);
                        

export const postOrganizationValidator = () => ([
    nameValidation
]);

Выводит:

"errors": [
        {
            "target": "body",
            "property": "name",
            "value": "solsssdlxkkxsme name",
            
            
            // можно было бы сюда ( как один из вариантов ) ?
            
            "statusCode": 401 
            
            
            "messages": [
                "The organization with the same name - solsssdlxkkxsme name is already exists!"
            ]
        }
    ]

Ошибка наследуется от:

export class CustomError extends Error {
    public property: string;
    public value: string;
    public messages: string;
    public target: any;
    public statusCode: number;


    constructor(messages, statusCode, target = {}, value = '', property = '') {
        super(messages);
        this.target = target;
        this.property = property;
        this.value = value;
        this.messages = messages;
        this.statusCode = statusCode;
    }
}

Нужно чтобы если кастомная ошибка -- то прокидывалась дальше, такая как есть


Ответы (0 шт):