Skip to content

Async Stack Traces support #15317

@vkarpov15

Description

@vkarpov15

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the issue has not already been raised

Issue

Followup ticket to #15298, #15312: ensuring we use async functions all the way through for improved stack traces. The goal is for the following script to show a full async stack trace with a pointer to the line of the original doc.save() call.

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));

And ensure support for similar cases:

  1. find()
  2. findOne()
  3. updateOne()
  4. findOneAndUpdate()
  5. insertMany()
  6. bulkWrite()
  7. Model.validate()
  8. save() with user-defined pre hook that throws an error
  9. save() with user-defined post hook that throws an error

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions