From a321d88ac20e7c761c2de53c65f1160b371fb28b Mon Sep 17 00:00:00 2001 From: JulioJu Date: Tue, 12 Feb 2019 00:57:27 +0100 Subject: [PATCH 1/2] @types/mongoose `Error.ValidationError.errors: {[path: string]: ValidatorError}` Source: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/21cd41db1ad200ccc3b096b04a556ab1a32ae02f/types/mongoose/index.d.ts#L594 --- types/mongoose/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/mongoose/index.d.ts b/types/mongoose/index.d.ts index d9f9d789f0..428530ff85 100644 --- a/types/mongoose/index.d.ts +++ b/types/mongoose/index.d.ts @@ -573,7 +573,7 @@ declare module "mongoose" { * An instance of this error class will be returned when [validation](http://mongoosejs.com/docs/validation.html) failed. */ export class ValidationError extends Error { - errors: any; + errors: {[path: string]: ValidatorError}; constructor(instance: MongooseDocument); From aea2991e8b6d20734abaf7983ce88fcab4bceb1e Mon Sep 17 00:00:00 2001 From: JulioJu Date: Tue, 12 Feb 2019 21:27:10 +0100 Subject: [PATCH 2/2] @types/mongoose improve MongooseError definitions --- types/mongoose/index.d.ts | 44 ++++++++++++++++++++------------ types/mongoose/mongoose-tests.ts | 36 +++++++++++++------------- 2 files changed, 46 insertions(+), 34 deletions(-) diff --git a/types/mongoose/index.d.ts b/types/mongoose/index.d.ts index 428530ff85..21c83e3bed 100644 --- a/types/mongoose/index.d.ts +++ b/types/mongoose/index.d.ts @@ -167,16 +167,7 @@ declare module "mongoose" { export function startSession(options?: mongodb.SessionOptions, cb?: (err: any, session: mongodb.ClientSession) => void): Promise; - class CastError extends Error { - /** - * The Mongoose CastError constructor - * @param type The name of the type - * @param value The value that failed to cast - * @param path The path a.b.c in the doc where this cast error occurred - * @param reason The original error that was thrown - */ - constructor(type: string, value: any, path: string, reason?: NativeError); - } + export type CastError = Error.CastError; /* * section connection.js @@ -509,6 +500,11 @@ declare module "mongoose" { * http://mongoosejs.com/docs/api.html#error-js */ class Error extends global.Error { + + // "MongooseError" for instances of the current class, + // an other string for instances of derived classes. + name: "MongooseError" | string; + /** * MongooseError constructor * @param msg Error message @@ -544,6 +540,10 @@ declare module "mongoose" { * the document. */ export class DocumentNotFoundError extends Error { + name: 'DocumentNotFoundError'; + filter: any; + query: any; + constructor(filter: any); } /** @@ -554,10 +554,11 @@ declare module "mongoose" { * cast a value. */ export class CastError extends Error { + name: 'CastError'; stringValue: string; kind: string; - path: string; value: any; + path: string; reason?: any; model?: any; @@ -569,13 +570,18 @@ declare module "mongoose" { /** * section error/validation.js * https://mongoosejs.com/docs/api.html#mongooseerror_MongooseError.ValidationError + + * An instance of this error class will be returned when [validation](/docs/validation.html) failed. + * The `errors` property contains an object whose keys are the paths that failed and whose values are + * instances of CastError or ValidationError. * - * An instance of this error class will be returned when [validation](http://mongoosejs.com/docs/validation.html) failed. */ export class ValidationError extends Error { - errors: {[path: string]: ValidatorError}; + name: 'ValidationError'; - constructor(instance: MongooseDocument); + errors: {[path: string]: ValidatorError | CastError}; + + constructor(instance?: MongooseDocument); /** Console.log helper */ toString(): string; @@ -594,13 +600,14 @@ declare module "mongoose" { * A `ValidationError` has a hash of `errors` that contain individual `ValidatorError` instances */ export class ValidatorError extends Error { - properties: any; + name: 'ValidatorError'; + properties: {message: string, type?: string, path?: string, value?: any, reason?: any}; kind: string; path: string; value: any; reason: any; - constructor(properties: any); + constructor(properties: {message?: string, type?: string, path?: string, value?: any, reason?: any}); formatMessage(msg: string | Function, properties: any): string; @@ -616,6 +623,7 @@ declare module "mongoose" { * the [`versionKey` option](http://mongoosejs.com/docs/guide.html#versionKey) for more information. */ export class VersionError extends Error { + name: 'VersionError'; version: any; modifiedPaths: Array; @@ -631,6 +639,7 @@ declare module "mongoose" { * information. */ export class ParallelSaveError extends Error { + name: 'ParallelSaveError'; constructor(doc: MongooseDocument); } @@ -642,6 +651,7 @@ declare module "mongoose" { * See [the FAQ about `OverwriteModelError`](http://mongoosejs.com/docs/faq.html#overwrite-model-error). */ export class OverwriteModelError extends Error { + name: 'OverwriteModelError'; constructor(name: string); } @@ -652,6 +662,7 @@ declare module "mongoose" { * Thrown when you try to access a model that has not been registered yet */ export class MissingSchemaError extends Error { + name: 'MissingSchemaError'; constructor(name: string); } @@ -663,6 +674,7 @@ declare module "mongoose" { * and then modified the array in an unsafe way. */ export class DivergentArrayError extends Error { + name: 'DivergentArrayError'; constructor(paths: Array); } } diff --git a/types/mongoose/mongoose-tests.ts b/types/mongoose/mongoose-tests.ts index 30b5b51a84..c2e15248bf 100644 --- a/types/mongoose/mongoose-tests.ts +++ b/types/mongoose/mongoose-tests.ts @@ -173,9 +173,9 @@ const getDB = async (tenant: string)=> { * http://mongoosejs.com/docs/api.html#error-js */ var mongooseError: mongoose.Error = new mongoose.Error('error'); +mongooseError.name; /* inherited properties */ mongooseError.message; -mongooseError.name; mongooseError.stack; /* static properties */ mongoose.Error.messages.hasOwnProperty(''); @@ -186,21 +186,22 @@ mongoose.Error.Messages.hasOwnProperty(''); * https://mongoosejs.com/docs/api.html#mongooseerror_MongooseError.CastError */ var castError: mongoose.Error.CastError = new mongoose.Error.CastError('', '', ''); -castError.setModel('foo'); +castError.name; castError.stringValue; castError.kind; castError.path; castError.value; +castError.setModel('foo'); /* inherited properties */ -castError.name; -castError.stack; castError.message; +castError.stack; /* * section error/validator.js * https://mongoosejs.com/docs/api.html#mongooseerror_MongooseError.ValidatorError */ -var validatorError: mongoose.Error.ValidatorError = new mongoose.Error.ValidatorError({ foo: 'bar' }) +var validatorError: mongoose.Error.ValidatorError = new mongoose.Error.ValidatorError({ message: 'bar' }) +validatorError.name; validatorError.properties; validatorError.kind; validatorError.path; @@ -209,9 +210,8 @@ validatorError.toString().toLowerCase(); validatorError.formatMessage('foo', {}); validatorError.formatMessage('foo', (bar: any)=>{ return bar; }); /* inherited properties */ -validatorError.name; -validatorError.stack; validatorError.message; +validatorError.stack; /* * section error/validation.js @@ -219,54 +219,54 @@ validatorError.message; */ var doc = {}; var validationError: mongoose.Error.ValidationError = new mongoose.Error.ValidationError(doc); +validationError.name; validationError.toString().toLowerCase(); validationError.inspect(); validationError.toJSON().hasOwnProperty(''); validationError.addError('foo', validatorError) /* inherited properties */ -validationError.name; -validationError.stack; validationError.message; +validationError.stack; /* * section error/parallelSave.js * https://mongoosejs.com/docs/api.html#mongooseerror_MongooseError.ParallelSaveError */ var parallelSaveError: mongoose.Error.ParallelSaveError = new mongoose.Error.ParallelSaveError(doc); -/* inherited properties */ parallelSaveError.name; -parallelSaveError.stack; +/* inherited properties */ parallelSaveError.message; +parallelSaveError.stack; /* * section error/overwriteModel.js * https://mongoosejs.com/docs/api.html#mongooseerror_MongooseError.OverwriteModelError */ var overwriteModelError: mongoose.Error.OverwriteModelError = new mongoose.Error.OverwriteModelError('foo'); -/* inherited properties */ overwriteModelError.name; -overwriteModelError.stack; +/* inherited properties */ overwriteModelError.message; +overwriteModelError.stack; /* * section error/missingSchema.js * https://mongoosejs.com/docs/api.html#mongooseerror_MongooseError.MissingSchemaError */ var missingSchemaError: mongoose.Error.MissingSchemaError = new mongoose.Error.MissingSchemaError('foo'); -/* inherited properties */ missingSchemaError.name; -missingSchemaError.stack; +/* inherited properties */ missingSchemaError.message; +missingSchemaError.stack; /* * section error/divergentArray.js * https://mongoosejs.com/docs/api.html#mongooseerror_MongooseError.MissingSchemaError */ -var missingSchemaError: mongoose.Error.DivergentArrayError = new mongoose.Error.DivergentArrayError(['foo','bar']); -/* inherited properties */ +var divergentArrayError: mongoose.Error.DivergentArrayError = new mongoose.Error.DivergentArrayError(['foo','bar']); missingSchemaError.name; -missingSchemaError.stack; +/* inherited properties */ missingSchemaError.message; +missingSchemaError.stack; const pluralize = mongoose.pluralize(); const plural: string = pluralize('foo');