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

feat(types): transition lib/errors #13710

Merged
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
46 changes: 0 additions & 46 deletions lib/dialects/mssql/async-queue.js

This file was deleted.

59 changes: 59 additions & 0 deletions lib/dialects/mssql/async-queue.ts
@@ -0,0 +1,59 @@
import { BaseError, ConnectionError } from '../../errors';

/**
* Thrown when a connection to a database is closed while an operation is in progress
*/
export class AsyncQueueError extends BaseError {
constructor(message: string) {
super(message);
this.name = 'SequelizeAsyncQueueError';
}
}

class AsyncQueue {
Keimeno marked this conversation as resolved.
Show resolved Hide resolved
previous: Promise<unknown>;
closed: boolean;
rejectCurrent: (reason?: any) => void;
Keimeno marked this conversation as resolved.
Show resolved Hide resolved

constructor() {
this.previous = Promise.resolve();
this.closed = false;
this.rejectCurrent = () => {
/** do nothing */
};
}

close() {
this.closed = true;
this.rejectCurrent(
new ConnectionError(
new AsyncQueueError(
'the connection was closed before this query could finish executing'
)
)
);
}

enqueue(asyncFunction: (...args: any[]) => Promise<unknown>) {
// This outer promise might seems superflous since down below we return asyncFunction().then(resolve, reject).
// However, this ensures that this.previous will never be a rejected promise so the queue will
// always keep going, while still communicating rejection from asyncFunction to the user.
return new Promise((resolve, reject) => {
this.previous = this.previous.then(() => {
this.rejectCurrent = reject;
if (this.closed) {
return reject(
new ConnectionError(
new AsyncQueueError(
'the connection was closed before this query could be executed'
sdepold marked this conversation as resolved.
Show resolved Hide resolved
)
)
);
}
return asyncFunction().then(resolve, reject);
});
});
}
}

export default AsyncQueue;
Keimeno marked this conversation as resolved.
Show resolved Hide resolved
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('');
Keimeno marked this conversation as resolved.
Show resolved Hide resolved
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;
19 changes: 16 additions & 3 deletions lib/errors/base-error.js → lib/errors/base-error.ts
@@ -1,4 +1,17 @@
'use strict';
export interface ErrorOptions {
stack?: string;
}

export 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.
Expand All @@ -8,10 +21,10 @@
* The Base Error all Sequelize Errors inherit from.
*/
class BaseError extends Error {
Keimeno marked this conversation as resolved.
Show resolved Hide resolved
constructor(message) {
constructor(message: string) {
Keimeno marked this conversation as resolved.
Show resolved Hide resolved
super(message);
this.name = 'SequelizeBaseError';
}
}

module.exports = BaseError;
export default BaseError;
@@ -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;
Keimeno marked this conversation as resolved.
Show resolved Hide resolved
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;
@@ -1,12 +1,13 @@
'use strict';

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

/**
* A base class for all connection related errors.
*/
class ConnectionError extends BaseError {
constructor(parent) {
parent: Error;
original: Error;

constructor(parent: Error) {
super(parent ? parent.message : '');
this.name = 'SequelizeConnectionError';
/**
Expand All @@ -19,4 +20,4 @@ class ConnectionError extends BaseError {
}
}

module.exports = ConnectionError;
export default ConnectionError;
@@ -1,15 +1,13 @@
'use strict';

const ConnectionError = require('./../connection-error');
import ConnectionError from './../connection-error';

/**
* Thrown when a connection to a database is refused due to insufficient privileges
*/
class AccessDeniedError extends ConnectionError {
constructor(parent) {
constructor(parent: Error) {
super(parent);
this.name = 'SequelizeAccessDeniedError';
}
}

module.exports = AccessDeniedError;
export default AccessDeniedError;
@@ -1,15 +1,13 @@
'use strict';

const ConnectionError = require('./../connection-error');
import ConnectionError from '../connection-error';

/**
* Thrown when connection is not acquired due to timeout
*/
class ConnectionAcquireTimeoutError extends ConnectionError {
constructor(parent) {
constructor(parent: Error) {
super(parent);
this.name = 'SequelizeConnectionAcquireTimeoutError';
}
}

module.exports = ConnectionAcquireTimeoutError;
export default ConnectionAcquireTimeoutError;
@@ -1,15 +1,13 @@
'use strict';

const ConnectionError = require('./../connection-error');
import ConnectionError from '../connection-error';

/**
* Thrown when a connection to a database is refused
*/
class ConnectionRefusedError extends ConnectionError {
constructor(parent) {
constructor(parent: Error) {
super(parent);
this.name = 'SequelizeConnectionRefusedError';
}
}

module.exports = ConnectionRefusedError;
export default ConnectionRefusedError;
@@ -1,15 +1,13 @@
'use strict';

const ConnectionError = require('./../connection-error');
import ConnectionError from '../connection-error';

/**
* Thrown when a connection to a database times out
*/
class ConnectionTimedOutError extends ConnectionError {
constructor(parent) {
constructor(parent: Error) {
super(parent);
this.name = 'SequelizeConnectionTimedOutError';
}
}

module.exports = ConnectionTimedOutError;
export default ConnectionTimedOutError;
@@ -1,15 +1,13 @@
'use strict';

const ConnectionError = require('./../connection-error');
import ConnectionError from '../connection-error';

/**
* Thrown when a connection to a database has a hostname that was not found
*/
class HostNotFoundError extends ConnectionError {
constructor(parent) {
constructor(parent: Error) {
super(parent);
this.name = 'SequelizeHostNotFoundError';
}
}

module.exports = HostNotFoundError;
export default HostNotFoundError;
@@ -1,15 +1,13 @@
'use strict';

const ConnectionError = require('./../connection-error');
import ConnectionError from '../connection-error';

/**
* Thrown when a connection to a database has a hostname that was not reachable
*/
class HostNotReachableError extends ConnectionError {
constructor(parent) {
constructor(parent: Error) {
super(parent);
this.name = 'SequelizeHostNotReachableError';
}
}

module.exports = HostNotReachableError;
export default HostNotReachableError;