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

fix(types): deduplicate error typings #14002

Merged
merged 2 commits into from Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 */
ephys marked this conversation as resolved.
Show resolved Hide resolved
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,4 +1,4 @@
import type { Model } from '../../types/lib/model';
import type { Model } from '../..';
import BaseError from './base-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
16 changes: 7 additions & 9 deletions lib/errors/database/exclusion-constraint-error.ts
Expand Up @@ -2,20 +2,18 @@ import type { DatabaseErrorSubclassOptions } from '../database-error';
import DatabaseError 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 @@ -7,22 +7,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
16 changes: 7 additions & 9 deletions lib/errors/database/unknown-constraint-error.ts
Expand Up @@ -2,20 +2,18 @@ import type { DatabaseErrorSubclassOptions } from '../database-error';
import DatabaseError 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
23 changes: 12 additions & 11 deletions lib/errors/validation-error.ts
@@ -1,4 +1,4 @@
import type { Model } from '../../types/lib/model';
import type { Model } from '../..';
import type { ErrorOptions } from './base-error';
import BaseError from './base-error';

Expand Down Expand Up @@ -52,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 @@ -203,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
15 changes: 6 additions & 9 deletions lib/errors/validation/unique-constraint-error.ts
Expand Up @@ -6,7 +6,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 @@ -17,13 +17,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 +32,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 @@ -11,8 +11,8 @@ export * from "../lib/transaction";
export type { Connection } from "./lib/connection-manager";
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