Skip to content

Conversation

@vkarpov15
Copy link
Collaborator

Summary

Re: mongoosejs/kareem#39. With this change, we get async stack traces for validation errors in 9.0 branch 👍

Error.stackTraceLimit = 20;

const mongoose = require("mongoose");
mongoose.connect("mongodb://localhost:27017/mongoose_test");


const userSchema = new mongoose.Schema({
  name: { type: String, required: true, validate: v => v.length > 3 },
  age: Number,
});
userSchema.pre('validate', function() {
  if (this.name === 'secret') {
    throw new Error('name cannot be secret');
  }
});

const User = mongoose.model("User", userSchema);

async function runDemo() {
  const doc = new User({ name: 'A' });
  // await doc.validate().catch(err => console.log('Validate', err));
  await doc.save().catch(err => console.log('Save', err));
}

runDemo().catch(err => console.log(err.stack));

Output:

Save ValidationError: User validation failed: name: Validator failed for path `name` with value `A`
    at Document.invalidate (/home/v/Desktop/MongoDB/mongoose/lib/document.js:3319:32)
    at validatePath (/home/v/Desktop/MongoDB/mongoose/lib/document.js:3094:13)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Promise.all (index 0)
    at async model.$__validate (/home/v/Desktop/MongoDB/mongoose/lib/document.js:3035:3)
    at async model.validate (/home/v/Desktop/MongoDB/mongoose/lib/document.js:2645:5)
    at async model.validateBeforeSave (/home/v/Desktop/MongoDB/mongoose/lib/plugins/validateBeforeSave.js:34:7)
    at async Kareem.execPre (/home/v/Desktop/MongoDB/mongoose/node_modules/kareem/index.js:95:11)
    at async model.$__save (/home/v/Desktop/MongoDB/mongoose/lib/model.js:498:5)
    at async model.save (/home/v/Desktop/MongoDB/mongoose/lib/model.js:654:5)
    at async runDemo (/home/v/Desktop/MongoDB/troubleshoot-mongoose/gh-14250.js:23:3) {
  errors: {
    name: ValidatorError: Validator failed for path `name` with value `A`
        at SchemaString.doValidate (/home/v/Desktop/MongoDB/mongoose/lib/schemaType.js:1392:13)
        at validatePath (/home/v/Desktop/MongoDB/mongoose/lib/document.js:3086:24)
        at model.$__validate (/home/v/Desktop/MongoDB/mongoose/lib/document.js:3032:21)
        at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
        at async model.validate (/home/v/Desktop/MongoDB/mongoose/lib/document.js:2645:5)
        at async model.validateBeforeSave (/home/v/Desktop/MongoDB/mongoose/lib/plugins/validateBeforeSave.js:34:7)
        at async Kareem.execPre (/home/v/Desktop/MongoDB/mongoose/node_modules/kareem/index.js:95:11)
        at async model.$__save (/home/v/Desktop/MongoDB/mongoose/lib/model.js:498:5)
        at async model.save (/home/v/Desktop/MongoDB/mongoose/lib/model.js:654:5)
        at async runDemo (/home/v/Desktop/MongoDB/troubleshoot-mongoose/gh-14250.js:23:3) {
      properties: [Object],
      kind: 'user defined',
      path: 'name',
      value: 'A',
      reason: undefined,
      [Symbol(mongoose#validatorError)]: true
    }
  },
  _message: 'User validation failed'
}

Examples

Copy link
Collaborator

@hasezoey hasezoey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me in its current state, though some small style nitpicks here and there and a documentation note.

@vkarpov15 vkarpov15 merged commit 4cc7af6 into 9.0 Apr 2, 2025
63 checks passed
@hasezoey hasezoey deleted the vkarpov15/async-savesubdocs branch April 3, 2025 09:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants