Skip to content

Commit

Permalink
feat(types): remaining transition lib/errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Keimeno committed Nov 27, 2021
1 parent 5a10bbc commit 2a1c9f6
Show file tree
Hide file tree
Showing 27 changed files with 359 additions and 271 deletions.
34 changes: 0 additions & 34 deletions lib/errors/aggregate-error.js

This file was deleted.

33 changes: 33 additions & 0 deletions lib/errors/aggregate-error.ts
@@ -0,0 +1,33 @@
import BaseError from './base-error';

/**
* A wrapper for multiple Errors
*
* @param {Error[]} [errors] Array of errors
*
* @property errors {Error[]}
*/
class AggregateError extends BaseError {
errors: Array<AggregateError | Error>;

constructor(errors: Array<AggregateError | Error>) {
super('');
this.errors = errors;
this.name = 'AggregateError';
}

toString(): string {
const message = `AggregateError of:\n${this.errors
.map((error: Error | AggregateError) =>
error === this
? '[Circular AggregateError]'
: error instanceof AggregateError
? String(error).replace(/\n$/, '').replace(/^/gm, ' ')
: String(error).replace(/^/gm, ' ').substring(2)
)
.join('\n')}\n`;
return message;
}
}

export default AggregateError;
@@ -1,15 +1,13 @@
'use strict';

const BaseError = require('./base-error');
import BaseError from './base-error';

/**
* Thrown when an association is improperly constructed (see message for details)
*/
class AssociationError extends BaseError {
constructor(message) {
constructor(message: string) {
super(message);
this.name = 'SequelizeAssociationError';
}
}

module.exports = AssociationError;
export default AssociationError;
13 changes: 12 additions & 1 deletion lib/errors/base-error.ts
Expand Up @@ -2,6 +2,17 @@ interface ErrorOptions {
stack?: string;
}

interface CommonErrorProperties {
/** The database specific error which triggered this one */
readonly parent: Error;

/** The database specific error which triggered this one */
readonly original: Error;

/** The SQL that triggered the error */
readonly sql: string;
}

/**
* 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.
Expand All @@ -17,4 +28,4 @@ class BaseError extends Error {
}

export default BaseError;
export { ErrorOptions };
export { ErrorOptions, CommonErrorProperties };
@@ -1,6 +1,5 @@
'use strict';

const BaseError = require('./base-error');
import BaseError from './base-error';
import type { Model } from '../../types/lib/model';

/**
* Thrown when bulk operation fails, it represent per record level error.
Expand All @@ -10,12 +9,15 @@ const BaseError = require('./base-error');
* @param {object} record DAO instance that error belongs to
*/
class BulkRecordError extends BaseError {
constructor(error, record) {
errors: Error;
record: Model;

constructor(error: Error, record: Model) {
super(error.message);
this.name = 'SequelizeBulkRecordError';
this.errors = error;
this.record = record;
}
}

module.exports = BulkRecordError;
export default BulkRecordError;
2 changes: 1 addition & 1 deletion lib/errors/connection/connection-refused-error.ts
Expand Up @@ -10,4 +10,4 @@ class ConnectionRefusedError extends ConnectionError {
}
}

module.exports = ConnectionRefusedError;
export default ConnectionRefusedError;
40 changes: 22 additions & 18 deletions lib/errors/database-error.ts
@@ -1,40 +1,44 @@
import BaseError, { ErrorOptions } from './base-error';

interface IDatabaseError extends Error {
/**
* The SQL that triggered the error
*/
sql?: string;

/**
* The parameters for the sql that triggered the error
*/
parameters?: object;
import BaseError, { CommonErrorProperties, ErrorOptions } from './base-error';

interface DatabaseErrorParent
extends Error,
Pick<CommonErrorProperties, 'sql'> {
/** The parameters for the sql that triggered the error */
readonly parameters?: object;
}

interface DatabaseErrorSubclassOptions extends ErrorOptions {
parent?: DatabaseErrorParent;
message?: string;
}

/**
* A base class for all database related errors.
*/
class DatabaseError extends BaseError implements IDatabaseError {
class DatabaseError
extends BaseError
implements DatabaseErrorParent, CommonErrorProperties
{
parent: Error;
original: Error;
sql?: string;
parameters?: object;
sql: string;
parameters: object;

constructor(parent: IDatabaseError, options: ErrorOptions = {}) {
constructor(parent: DatabaseErrorParent, options: ErrorOptions = {}) {
super(parent.message);
this.name = 'SequelizeDatabaseError';

this.parent = parent;
this.original = parent;

this.sql = parent.sql;
this.parameters = parent.parameters;
this.parameters = parent.parameters ?? {};

if (options.stack) {
this.stack = options.stack;
}
}
}

module.exports = DatabaseError;
export default DatabaseError;
export { DatabaseErrorSubclassOptions, DatabaseErrorParent };
23 changes: 0 additions & 23 deletions lib/errors/database/exclusion-constraint-error.js

This file was deleted.

36 changes: 36 additions & 0 deletions lib/errors/database/exclusion-constraint-error.ts
@@ -0,0 +1,36 @@
import DatabaseError, { DatabaseErrorSubclassOptions } from '../database-error';

interface ExclusionConstraintErrorOptions {
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;

constructor(
options: DatabaseErrorSubclassOptions & ExclusionConstraintErrorOptions
) {
options = options || {};
options.parent = options.parent || { sql: '', name: '', message: '' };

super(options.parent, { stack: options.stack });
this.name = 'SequelizeExclusionConstraintError';

this.message = options.message || options.parent.message || '';
this.constraint = options.constraint;
this.fields = options.fields;
this.table = options.table;
}
}

export default ExclusionConstraintError;
25 changes: 0 additions & 25 deletions lib/errors/database/foreign-key-constraint-error.js

This file was deleted.

46 changes: 46 additions & 0 deletions lib/errors/database/foreign-key-constraint-error.ts
@@ -0,0 +1,46 @@
import DatabaseError, { DatabaseErrorSubclassOptions } from '../database-error';

enum RelationshipType {
parent = 'parent',
child = 'child',
}

interface ForeignKeyConstraintErrorOptions {
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 };
value: unknown;
index: string;
reltype: RelationshipType;

constructor(
options: ForeignKeyConstraintErrorOptions & DatabaseErrorSubclassOptions
) {
options = options || {};
options.parent = options.parent || { sql: '', name: '', message: '' };

super(options.parent, { stack: options.stack });
this.name = 'SequelizeForeignKeyConstraintError';

this.message =
options.message || options.parent.message || 'Database Error';
this.fields = options.fields;
this.table = options.table;
this.value = options.value;
this.index = options.index;
this.reltype = options.reltype;
}
}

export default ForeignKeyConstraintError;
export { RelationshipType };
15 changes: 0 additions & 15 deletions lib/errors/database/timeout-error.js

This file was deleted.

14 changes: 14 additions & 0 deletions lib/errors/database/timeout-error.ts
@@ -0,0 +1,14 @@
import { ErrorOptions } from '../base-error';
import DatabaseError, { DatabaseErrorParent } from '../database-error';

/**
* Thrown when a database query times out because of a deadlock
*/
class TimeoutError extends DatabaseError {
constructor(parent: DatabaseErrorParent, options: ErrorOptions = {}) {
super(parent, options);
this.name = 'SequelizeTimeoutError';
}
}

export default TimeoutError;
23 changes: 0 additions & 23 deletions lib/errors/database/unknown-constraint-error.js

This file was deleted.

0 comments on commit 2a1c9f6

Please sign in to comment.