문제

I'm not sure how to create an error in javascript with multiple messages.

This is what I'm currently doing, it only reports the first most error, and if you have multiple, it's going to go one at a time down the list, not very user-friendly.

middleware.validate = function(req, res, next) {
    if (!req.form.name) return next(new Error("missing form input form name"));
    if (!req.form.shop) return next(new Error("missing form input shop"));
    if (!req.form.active) return next(new Error("missing form input active"));
    if (!req.action) return next(new Error("missing form input action"));
    return next();
}

Here's some alternatives that return more then one messag:

middleware.validate = function(req, res, next) {
    var errors = [];
    if (!req.form.name) errors.push(new Error("missing form input form name"));
    if (!req.form.shop) errors.push(new Error("missing form input shop"));
    if (!req.form.active) errors.push(new Error("missing form input active"));
    if (!req.action) errors.push(new Error("missing form input action"));
    if(errors.length > 0){
        var err = new Error("validation failed");
        err.multiple = errors;
        return next(err);
    };
    return next();
}

or

middleware.validate = function(req, res, next) {
    var errors = [];
    if (!req.form.name) errors.push("missing form input form name");
    if (!req.form.shop) errors.push("missing form input shop");
    if (!req.form.active) errors.push("missing form input active");
    if (!req.action) errors.push("missing form input action");
    if(errors.length > 0) return next(new Error(errors.join("/"));
    return next();
}

Any standard / best practice way of doing this?

I hate the joined array error, as well as adding items to the err object. =[

This might be the best answer:

middleware.validate = function(req, res, next) {
    var errors = [];
    if (!req.form.name) errors.push("missing form input form name");
    if (!req.form.shop) errors.push("missing form input shop");
    if (!req.form.active) errors.push("missing form input active");
    if (!req.action) errors.push("missing form input action");
    if(errors.length > 0){
        var err = new Error("validation failed");
        err.messages = errors;
        return next(err);
    };
    return next();
}
도움이 되었습니까?

해결책

I created an npm module to solve this problem error-suite.

import MultiError from 'error-suite'

let multierror = new MultiError('Invalid Form.')
multierror.push('Invalid Name')
multierror.push('Invalid Email')
multierror.push('Invalid Phone Number')

console.log(multierror.errors) // [ [Error: Invalid Name], [Error: Invalid Email], [Error: Invalid Phone Number] ]
console.log(multierror.messages) // [ 'Invalid Name', 'Invalid Email', 'Invalid Phone Number' ]
console.log(multierror.errorsPresent) // true
console.log(multierror.shouldThrow) // true
if (multierror.shouldThrow) throw multierror // Error: Invalid Form. | Invalid Name, Invalid Email, Invalid Phone Number

https://github.com/reggi/error-suite/blob/master/src/multi-error.js

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top