From 26288f843b2e76cbc10e974b583ab036450cc470 Mon Sep 17 00:00:00 2001 From: stabilo Date: Sun, 14 Oct 2018 18:56:45 +1000 Subject: [PATCH] @types/mongoose Add missing typings for MongooseError constructors --- types/mongoose/index.d.ts | 149 ++++++++++++++++++++++++++++--- types/mongoose/mongoose-tests.ts | 98 +++++++++++++++++--- 2 files changed, 226 insertions(+), 21 deletions(-) diff --git a/types/mongoose/index.d.ts b/types/mongoose/index.d.ts index 19551684e0..b0b6727b49 100644 --- a/types/mongoose/index.d.ts +++ b/types/mongoose/index.d.ts @@ -13,6 +13,7 @@ // Idan Dardikman // Dominik Heigl // Fazendaaa +// stablio // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.3 @@ -479,15 +480,6 @@ declare module "mongoose" { static STATES: any; } - /* - * section error/validation.js - * http://mongoosejs.com/docs/api.html#error-validation-js - */ - class ValidationError extends Error { - /** Console.log helper */ - toString(): string; - } - /* * section error.js * http://mongoosejs.com/docs/api.html#error-js @@ -512,6 +504,143 @@ declare module "mongoose" { /** For backwards compatibility. Same as mongoose.Error.messages */ static Messages: any; + + } + + module Error { + + /** + * section error/notFound.js + * https://mongoosejs.com/docs/api.html#mongooseerror_MongooseError.DocumentNotFoundError + * + * An instance of this error class will be returned when `save()` fails + * because the underlying + * document was not found. The constructor takes one parameter, the + * conditions that mongoose passed to `update()` when trying to update + * the document. + */ + export class DocumentNotFoundError extends Error { + } + + /** + * section error/cast.js + * https://mongoosejs.com/docs/api.html#mongooseerror_MongooseError.CastError + * + * An instance of this error class will be returned when mongoose failed to + * cast a value. + */ + export class CastError extends Error { + stringValue: string; + kind: string; + path: string; + value: any; + reason?: any; + model?: any; + + constructor(type: string, value: any, path: string, reason?: NativeError); + + setModel(model: any): void; + } + + /** + * section error/validation.js + * https://mongoosejs.com/docs/api.html#mongooseerror_MongooseError.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: any; + + constructor(instance: MongooseDocument); + + /** Console.log helper */ + toString(): string; + + inspect(): object; + + toJSON(): object; + + addError(path: string, error: any): void; + } + + /** + * section error/validator.js + * https://mongoosejs.com/docs/api.html#mongooseerror_MongooseError.ValidatorError + * + * A `ValidationError` has a hash of `errors` that contain individual `ValidatorError` instances + */ + export class ValidatorError extends Error { + properties: any; + kind: string; + path: string; + value: any; + reason: any; + + constructor(properties: any); + + formatMessage(msg: string | Function, properties: any): string; + + toString(): string; + } + + /** + * section error/version.js + * https://mongoosejs.com/docs/api.html#mongooseerror_MongooseError.VersionError + * + * An instance of this error class will be returned when you call `save()` after + * the document in the database was changed in a potentially unsafe way. See + * the [`versionKey` option](http://mongoosejs.com/docs/guide.html#versionKey) for more information. + */ + export class VersionError extends Error { + version: any; + modifiedPaths: Array; + + constructor(doc: MongooseDocument, currentVersion: any, modifiedPaths: any); + } + + /** + * section error/parallelSave.js + * https://mongoosejs.com/docs/api.html#mongooseerror_MongooseError.ParallelSaveError + * + * An instance of this error class will be returned when you call `save()` multiple + * times on the same document in parallel. See the [FAQ](http://mongoosejs.com/docs/faq.html) for more + * information. + */ + export class ParallelSaveError extends Error { + constructor(doc: MongooseDocument); + } + + /** + * section error/overwriteModel.js + * https://mongoosejs.com/docs/api.html#mongooseerror_MongooseError.OverwriteModelError + * + * Thrown when a model with the given name was already registered on the connection. + * See [the FAQ about `OverwriteModelError`](http://mongoosejs.com/docs/faq.html#overwrite-model-error). + */ + export class OverwriteModelError extends Error { + constructor(name: string); + } + + /** + * section error/missingSchema.js + * https://mongoosejs.com/docs/api.html#mongooseerror_MongooseError.MissingSchemaError + * + * Thrown when you try to access a model that has not been registered yet + */ + export class MissingSchemaError extends Error { + constructor(name: string); + } + + /** + * section error/divergentArray.js + * https://mongoosejs.com/docs/api.html#mongooseerror_MongooseError.DivergentArrayError + * + * An instance of this error will be returned if you used an array projection + * and then modified the array in an unsafe way. + */ + export class DivergentArrayError extends Error { + constructor(paths: Array); + } } interface EachAsyncOptions { @@ -1166,7 +1295,7 @@ declare module "mongoose" { * @param kind optional kind property for the error * @returns the current ValidationError, with all currently invalidated paths */ - invalidate(path: string, errorMsg: string | NativeError, value: any, kind?: string): ValidationError | boolean; + invalidate(path: string, errorMsg: string | NativeError, value: any, kind?: string): Error.ValidationError | boolean; /** Returns true if path was directly set and modified, else false. */ isDirectModified(path: string): boolean; diff --git a/types/mongoose/mongoose-tests.ts b/types/mongoose/mongoose-tests.ts index 20b24ba887..87d61cbdd4 100644 --- a/types/mongoose/mongoose-tests.ts +++ b/types/mongoose/mongoose-tests.ts @@ -166,17 +166,6 @@ const getDB = async (tenant: string)=> { return conn1.useDb(tenant); }; - -/* - * section error/validation.js - * http://mongoosejs.com/docs/api.html#error-validation-js - */ -var validationError = {}; -validationError.toString().toLowerCase(); -/* inherited properties */ -validationError.stack; -validationError.message; - /* * section error.js * http://mongoosejs.com/docs/api.html#error-js @@ -190,6 +179,93 @@ mongooseError.stack; mongoose.Error.messages.hasOwnProperty(''); mongoose.Error.Messages.hasOwnProperty(''); +/* + * section error/cast.js + * https://mongoosejs.com/docs/api.html#mongooseerror_MongooseError.CastError + */ +var castError: mongoose.Error.CastError = new mongoose.Error.CastError('', '', ''); +castError.setModel('foo'); +castError.stringValue; +castError.kind; +castError.path; +castError.value; +/* inherited properties */ +castError.name; +castError.stack; +castError.message; + +/* + * section error/validator.js + * https://mongoosejs.com/docs/api.html#mongooseerror_MongooseError.ValidatorError + */ +var validatorError: mongoose.Error.ValidatorError = new mongoose.Error.ValidatorError({ foo: 'bar' }) +validatorError.properties; +validatorError.kind; +validatorError.path; +validatorError.value; +validatorError.toString().toLowerCase(); +validatorError.formatMessage('foo', {}); +validatorError.formatMessage('foo', (bar: any)=>{ return bar; }); +/* inherited properties */ +validatorError.name; +validatorError.stack; +validatorError.message; + +/* + * section error/validation.js + * https://mongoosejs.com/docs/api.html#mongooseerror_MongooseError.ValidationError + */ +var doc = {}; +var validationError: mongoose.Error.ValidationError = new mongoose.Error.ValidationError(doc); +validationError.toString().toLowerCase(); +validationError.inspect(); +validationError.toJSON().hasOwnProperty(''); +validationError.addError('foo', validatorError) +/* inherited properties */ +validationError.name; +validationError.stack; +validationError.message; + +/* + * 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; +parallelSaveError.message; + +/* + * 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; +overwriteModelError.message; + +/* + * 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; +missingSchemaError.message; + +/* + * 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 */ +missingSchemaError.name; +missingSchemaError.stack; +missingSchemaError.message; + const pluralize = mongoose.pluralize(); const plural: string = pluralize('foo');