Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript instanceof mongoose.Error #11838

Closed
amorimfilipe opened this issue May 26, 2022 · 5 comments · Fixed by #11844 or #11850
Closed

Typescript instanceof mongoose.Error #11838

amorimfilipe opened this issue May 26, 2022 · 5 comments · Fixed by #11844 or #11850
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@amorimfilipe
Copy link

Do you want to request a feature or report a bug?

bug

What is the current behavior?

Following the example in https://mongoosejs.com/docs/api/error.html#error_Error

const Model = mongoose.model('Test', new mongoose.Schema({ answer: Number }))
const doc = new Model({ answer: 'not a number' })
const err = doc.validateSync()

err instanceof mongoose.Error // true

I got the error in err instanceof mongoose.Error

namespace Error
The right-hand side of an 'instanceof' expression must be of type 'any' or of a type assignable to the 'Function' interface type.ts(2359)

If the current behavior is a bug, please provide the steps to reproduce.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

node 16.14.2
mongoose 6.3.4

@IslandRhythms IslandRhythms added the typescript Types or Types-test related issue / Pull Request label May 27, 2022
@Uzlopak
Copy link
Collaborator

Uzlopak commented May 28, 2022

It is a documentation error. You have to check against mongoose.Error.ValidationError

@Uzlopak Uzlopak closed this as completed May 28, 2022
@Uzlopak
Copy link
Collaborator

Uzlopak commented May 28, 2022

Accidently closed

@Uzlopak Uzlopak reopened this May 28, 2022
@vkarpov15 vkarpov15 added this to the 6.3.5 milestone May 28, 2022
@amorimfilipe
Copy link
Author

@Uzlopak Maybe I am misunderstanding something, but from code

// mongoose\lib\index.js

/**
 * The [MongooseError](#error_MongooseError) constructor.
 *
 * @method Error
 * @api public
 */

Mongoose.prototype.Error = require('./error/index');
// mongoose\lib\error\index.js

/**
 * MongooseError constructor. MongooseError is the base class for all
 * Mongoose-specific errors.
 *
 * #### Example:
 *     const Model = mongoose.model('Test', new Schema({ answer: Number }));
 *     const doc = new Model({ answer: 'not a number' });
 *     const err = doc.validateSync();
 *
 *     err instanceof mongoose.Error; // true
 *
 * @constructor Error
 * @param {String} msg Error message
 * @inherits Error https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error
 */

const MongooseError = require('./mongooseError');

module.exports = exports = MongooseError;
// mongoose\lib\error\mongooseError.js

class MongooseError extends Error { }

Object.defineProperty(MongooseError.prototype, 'name', {
  value: 'MongooseError'
});

module.exports = MongooseError;

So shouldn't instanceof mongoose.Error work or I am missing something? I am trying to validate against MongooseError instead of every single error.

@Uzlopak
Copy link
Collaborator

Uzlopak commented May 29, 2022

@amorimfilipe

Thanks for the feedback. It should be now fixed in master.

@amorimfilipe
Copy link
Author

@Uzlopak Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
4 participants