Merge pull request #29709 from stablio/master

@types/mongoose Add missing typings for MongooseError constructors
This commit is contained in:
Armando Aguirre
2018-11-05 11:33:25 -08:00
committed by GitHub
2 changed files with 226 additions and 21 deletions
+139 -10
View File
@@ -15,6 +15,7 @@
// Fazendaaa <https://github.com/Fazendaaa>
// Norman Perrin <https://github.com/NormanPerrin>
// Dan Manastireanu <https://github.com/danmana>
// stablio <https://github.com/stablio>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
@@ -478,15 +479,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
@@ -511,6 +503,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<any>;
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<any>);
}
}
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;
+87 -11
View File
@@ -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 = <mongoose.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 = <mongoose.MongooseDocument> {};
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');