问题
new Error()
?
发行
我有一些在Mongoose模式上运行的验证。我有一个预保存功能,我用它来加密密码
bcrypt
error.errors
从Mongoose返回的以下格式的对象:
// Schema
const MySchema = new Schema(
{
myField: {
type: String,
minlength: [3, "My Field must be 3-30 characters."],
maxlength: [30, "My Field must be 3-30 characters."],
required: [true, "My Field is required."],
trim: true
},
)
// Errors returned in the following format:
errors: {
myField: {
message: "Custom error warns foo must be bar."
},
// ... more error'd fields here
}
我想添加一个相同格式的自定义错误,以便
错误。错误
我以JSON格式发送回我的前端的对象很容易被读取。现在,我要
错误,并将我自己的对象格式化为类似的格式,但这不是一种非常优雅的方式。
New Error("My custom error here")
代码
有没有更清洁的利用方法
New Error()
使我的错误处理变得更好,而不必以这种方式格式化我自己的错误对象?