Skip to content

Commit

Permalink
fix(types): deduplicate error typings (sequelize#14002)
Browse files Browse the repository at this point in the history
  • Loading branch information
sdepold authored and aliatsis committed Jun 2, 2022
1 parent 2062f70 commit 8d32df5
Show file tree
Hide file tree
Showing 18 changed files with 68 additions and 339 deletions.
5 changes: 3 additions & 2 deletions lib/errors/aggregate-error.ts
Expand Up @@ -3,10 +3,11 @@ import BaseError from './base-error';
/**
* A wrapper for multiple Errors
*
* @param errors Array of errors
* @param errors The aggregated errors that occurred
*/
class AggregateError extends BaseError {
errors: Array<AggregateError | Error>;
/** the aggregated errors that occurred */
readonly errors: Array<AggregateError | Error>;

constructor(errors: Array<AggregateError | Error>) {
super();
Expand Down
3 changes: 2 additions & 1 deletion lib/errors/base-error.ts
Expand Up @@ -14,11 +14,12 @@ export interface CommonErrorProperties {
}

/**
* The Base Error all Sequelize Errors inherit from.
*
* Sequelize provides a host of custom error classes, to allow you to do easier debugging. All of these errors are exposed on the sequelize object and the sequelize constructor.
* All sequelize errors inherit from the base JS error object.
*
* This means that errors can be accessed using `Sequelize.ValidationError`
* The Base Error all Sequelize Errors inherit from.
*/
abstract class BaseError extends Error {
constructor(message?: string) {
Expand Down
2 changes: 1 addition & 1 deletion lib/errors/bulk-record-error.ts
@@ -1,5 +1,5 @@
import type { Model } from '../..';
import BaseError from './base-error';
import type { Model } from '../../types/lib/model';

/**
* Thrown when bulk operation fails, it represent per record level error.
Expand Down
4 changes: 4 additions & 0 deletions lib/errors/database-error.ts
Expand Up @@ -24,6 +24,10 @@ class DatabaseError
sql: string;
parameters: object;

/**
* @param parent The database specific error which triggered this one
* @param options
*/
constructor(parent: DatabaseErrorParent, options: ErrorOptions = {}) {
super(parent.message);
this.name = 'SequelizeDatabaseError';
Expand Down
17 changes: 7 additions & 10 deletions lib/errors/database/exclusion-constraint-error.ts
@@ -1,21 +1,18 @@
import DatabaseError, { DatabaseErrorSubclassOptions } from '../database-error';

interface ExclusionConstraintErrorOptions {
constraint: string;
fields: Record<string, string | number>;
table: string;
constraint?: string;
fields?: Record<string, string | number>;
table?: string;
}

/**
* Thrown when an exclusion constraint is violated in the database
*/
class ExclusionConstraintError
extends DatabaseError
implements ExclusionConstraintErrorOptions
{
constraint: string;
fields: Record<string, string | number>;
table: string;
class ExclusionConstraintError extends DatabaseError implements ExclusionConstraintErrorOptions {
constraint: string | undefined;
fields: Record<string, string | number> | undefined;
table: string | undefined;

constructor(
options: DatabaseErrorSubclassOptions & ExclusionConstraintErrorOptions
Expand Down
18 changes: 9 additions & 9 deletions lib/errors/database/foreign-key-constraint-error.ts
Expand Up @@ -6,22 +6,22 @@ export enum RelationshipType {
}

interface ForeignKeyConstraintErrorOptions {
table: string;
fields: { [field: string]: string };
value: unknown;
index: string;
reltype: RelationshipType;
table?: string;
fields?: { [field: string]: string };
value?: unknown;
index?: string;
reltype?: RelationshipType;
}

/**
* Thrown when a foreign key constraint is violated in the database
*/
class ForeignKeyConstraintError extends DatabaseError {
table: string;
fields: { [field: string]: string };
table: string | undefined;
fields: { [field: string]: string } | undefined;
value: unknown;
index: string;
reltype: RelationshipType;
index: string | undefined;
reltype: RelationshipType | undefined;

constructor(
options: ForeignKeyConstraintErrorOptions & DatabaseErrorSubclassOptions
Expand Down
17 changes: 7 additions & 10 deletions lib/errors/database/unknown-constraint-error.ts
@@ -1,21 +1,18 @@
import DatabaseError, { DatabaseErrorSubclassOptions } from '../database-error';

interface UnknownConstraintErrorOptions {
constraint: string;
fields: Record<string, string | number>;
table: string;
constraint?: string;
fields?: Record<string, string | number>;
table?: string;
}

/**
* Thrown when constraint name is not found in the database
*/
class UnknownConstraintError
extends DatabaseError
implements UnknownConstraintErrorOptions
{
constraint: string;
fields: Record<string, string | number>;
table: string;
class UnknownConstraintError extends DatabaseError implements UnknownConstraintErrorOptions {
constraint: string | undefined;
fields: Record<string, string | number> | undefined;
table: string | undefined;

constructor(
options: UnknownConstraintErrorOptions & DatabaseErrorSubclassOptions
Expand Down
6 changes: 3 additions & 3 deletions lib/errors/optimistic-lock-error.ts
Expand Up @@ -15,9 +15,9 @@ interface OptimisticLockErrorOptions {
* Thrown when attempting to update a stale model instance
*/
class OptimisticLockError extends BaseError {
modelName?: string;
values?: Record<string, unknown>;
where?: Record<string, unknown>;
modelName: string | undefined;
values: Record<string, unknown> | undefined;
where: Record<string, unknown> | undefined;

constructor(options: OptimisticLockErrorOptions) {
options = options || {};
Expand Down
26 changes: 14 additions & 12 deletions lib/errors/validation-error.ts
@@ -1,5 +1,6 @@
import BaseError, { ErrorOptions } from './base-error';
import type { Model } from '../../types/lib/model';
import type { Model } from '../..';
import type { ErrorOptions } from './base-error';
import BaseError from './base-error';

/**
* An enum that is used internally by the `ValidationErrorItem` class
Expand Down Expand Up @@ -51,44 +52,44 @@ export class ValidationErrorItem {
/**
* An error message
*/
message: string;
readonly message: string;

/**
* The type/origin of the validation error
*/
type: keyof typeof ValidationErrorItemType | null;
readonly type: keyof typeof ValidationErrorItemType | null;

/**
* The field that triggered the validation error
*/
path: string | null;
readonly path: string | null;

/**
* The value that generated the error
*/
value: string | null;
readonly value: string | null;

origin: keyof typeof ValidationErrorItemOrigin | null;
readonly origin: keyof typeof ValidationErrorItemOrigin | null;

/**
* The DAO instance that caused the validation error
*/
instance: Model | null;
readonly instance: Model | null;

/**
* A validation "key", used for identification
*/
validatorKey: string | null;
readonly validatorKey: string | null;

/**
* Property name of the BUILT-IN validator function that caused the validation error (e.g. "in" or "len"), if applicable
*/
validatorName: string | null;
readonly validatorName: string | null;

/**
* Parameters used with the BUILT-IN validator function, if applicable
*/
validatorArgs: unknown[];
readonly validatorArgs: unknown[];

/**
* Creates a new ValidationError item. Instances of this class are included in the `ValidationError.errors` property.
Expand Down Expand Up @@ -202,7 +203,8 @@ export class ValidationErrorItem {
* @param errors Array of ValidationErrorItem objects describing the validation errors
*/
class ValidationError extends BaseError {
errors: ValidationErrorItem[];
/** Array of ValidationErrorItem objects describing the validation errors */
readonly errors: ValidationErrorItem[];

constructor(
message: string,
Expand Down
16 changes: 6 additions & 10 deletions lib/errors/validation/unique-constraint-error.ts
Expand Up @@ -5,7 +5,7 @@ interface UniqueConstraintErrorParent
extends Error,
Pick<CommonErrorProperties, 'sql'> {}

interface UniqueConstraintErrorOptions extends ErrorOptions {
export interface UniqueConstraintErrorOptions extends ErrorOptions {
parent?: UniqueConstraintErrorParent;
original?: UniqueConstraintErrorParent;
errors?: ValidationErrorItem[];
Expand All @@ -16,14 +16,11 @@ interface UniqueConstraintErrorOptions extends ErrorOptions {
/**
* Thrown when a unique constraint is violated in the database
*/
class UniqueConstraintError
extends ValidationError
implements CommonErrorProperties
{
parent: UniqueConstraintErrorParent;
original: UniqueConstraintErrorParent;
fields: Record<string, unknown>;
sql: string;
class UniqueConstraintError extends ValidationError implements CommonErrorProperties {
readonly parent: UniqueConstraintErrorParent;
readonly original: UniqueConstraintErrorParent;
readonly fields: Record<string, unknown>;
readonly sql: string;

constructor(options: UniqueConstraintErrorOptions) {
options = options ?? {};
Expand All @@ -34,7 +31,6 @@ class UniqueConstraintError
super(options.message, options.errors, { stack: options.stack });

this.name = 'SequelizeUniqueConstraintError';
this.errors = options.errors;
this.fields = options.fields ?? {};
this.parent = options.parent;
this.original = options.parent;
Expand Down
4 changes: 2 additions & 2 deletions types/index.d.ts
Expand Up @@ -8,8 +8,8 @@ import Utils = require("./lib/utils");

export * from "./lib/associations/index";
export * from "./lib/data-types";
export * from "./lib/errors";
export { BaseError as Error } from "./lib/errors";
export * from "../lib/errors";
export { BaseError as Error } from "../lib/errors";
export * from "./lib/model";
export * from "./lib/query-interface";
export * from "./lib/sequelize";
Expand Down

0 comments on commit 8d32df5

Please sign in to comment.