Skip to content

Commit

Permalink
feat: set error cause if available (#1649)
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurschreiber authored Aug 14, 2024
1 parent 03777a7 commit f405eab
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1963,7 +1963,7 @@ class Connection extends EventEmitter {
}

process.nextTick(() => {
this.emit('connect', new ConnectionError(err.message, 'EINSTLOOKUP'));
this.emit('connect', new ConnectionError(err.message, 'EINSTLOOKUP', { cause: err }));
});
});
}
Expand Down Expand Up @@ -2331,11 +2331,11 @@ class Connection extends EventEmitter {
const routingMessage = this.routingData ? ` (redirected from ${this.config.server}${hostPostfix})` : '';
const message = `Failed to connect to ${server}${port}${routingMessage} - ${error.message}`;
this.debug.log(message);
this.emit('connect', new ConnectionError(message, 'ESOCKET'));
this.emit('connect', new ConnectionError(message, 'ESOCKET', { cause: error }));
} else {
const message = `Connection lost - ${error.message}`;
this.debug.log(message);
this.emit('error', new ConnectionError(message, 'ESOCKET'));
this.emit('error', new ConnectionError(message, 'ESOCKET', { cause: error }));
}
this.dispatchEvent('socketError', error);
}
Expand Down
8 changes: 4 additions & 4 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ export class ConnectionError extends Error {

declare isTransient: boolean | undefined;

constructor(message: string, code?: string) {
super(message);
constructor(message: string, code?: string, options?: ErrorOptions) {
super(message, options);

this.code = code;
}
Expand All @@ -20,8 +20,8 @@ export class RequestError extends Error {
declare procName: string | undefined;
declare lineNumber: number | undefined;

constructor(message: string, code?: string) {
super(message);
constructor(message: string, code?: string, options?: ErrorOptions) {
super(message, options);

this.code = code;
}
Expand Down
2 changes: 1 addition & 1 deletion src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ class Request extends EventEmitter {
try {
parameter.value = parameter.type.validate(parameter.value, collation);
} catch (error: any) {
throw new RequestError('Validation failed for parameter \'' + parameter.name + '\'. ' + error.message, 'EPARAM');
throw new RequestError('Validation failed for parameter \'' + parameter.name + '\'. ' + error.message, 'EPARAM', { cause: error });
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions test/integration/connection-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,10 @@ describe('Initiate Connect Test', function() {
try {
assert.instanceOf(err, ConnectionError);
assert.strictEqual(/** @type {ConnectionError} */(err).code, 'ESOCKET');

assert.instanceOf(/** @type {ConnectionError} */(err).cause, Error);
assert.strictEqual(/** @type {Error} */(/** @type {ConnectionError} */(err).cause).message, 'getaddrinfo ENOTFOUND something.invalid');

assert.strictEqual(connection.connectTimer, undefined);
done();
} catch (e) {
Expand Down Expand Up @@ -277,6 +281,10 @@ describe('Initiate Connect Test', function() {
connection.on('connect', (err) => {
assert.instanceOf(err, ConnectionError);
assert.strictEqual(/** @type {ConnectionError} */(err).code, 'EINSTLOOKUP');

assert.instanceOf(/** @type {ConnectionError} */(err).cause, Error);
assert.strictEqual(/** @type {Error} */(/** @type {ConnectionError} */(err).cause).message, 'getaddrinfo ENOTFOUND something.invalid');

assert.strictEqual(connection.connectTimer, undefined);

done();
Expand Down Expand Up @@ -310,6 +318,10 @@ describe('Initiate Connect Test', function() {
connection.connect(function(err) {
assert.instanceOf(err, ConnectionError);
assert.strictEqual(/** @type {ConnectionError} */(err).code, 'ESOCKET');

assert.instanceOf(/** @type {ConnectionError} */(err).cause, Error);
console.log(/** @type {ConnectionError} */(err).cause);
assert.include(/** @type {Error} */(/** @type {ConnectionError} */(err).cause).message, 'no ciphers available');
});

connection.on('end', function() {
Expand Down

0 comments on commit f405eab

Please sign in to comment.