diff --git a/src/connection.ts b/src/connection.ts index c0ddb6b51..2267653f1 100644 --- a/src/connection.ts +++ b/src/connection.ts @@ -937,10 +937,6 @@ class Connection extends EventEmitter { * @private */ declare closed: boolean; - /** - * @private - */ - declare loginError: undefined | AggregateError | ConnectionError; /** * @private */ @@ -2002,7 +1998,6 @@ class Connection extends EventEmitter { } this.closed = true; - this.loginError = undefined; } } @@ -3305,7 +3300,6 @@ class Connection extends EventEmitter { this.curTransientRetryCount++; this.clearConnectTimer(); - this.loginError = undefined; this.socket!.removeListener('error', this._onSocketError); this.socket!.removeListener('close', this._onSocketClose); @@ -3346,12 +3340,12 @@ class Connection extends EventEmitter { } else { this.transitionTo(this.STATE.LOGGED_IN_SENDING_INITIAL_SQL); } - } else if (this.loginError) { - if (isTransientError(this.loginError)) { + } else if (handler.loginError) { + if (isTransientError(handler.loginError)) { this.debug.log('Initiating retry on transient error'); this.transitionTo(this.STATE.TRANSIENT_FAILURE_RETRY); } else { - this.emit('connect', this.loginError); + this.emit('connect', handler.loginError); this.transitionTo(this.STATE.FINAL); } } else { @@ -3399,12 +3393,12 @@ class Connection extends EventEmitter { }); this.ntlmpacket = undefined; - } else if (this.loginError) { - if (isTransientError(this.loginError)) { + } else if (handler.loginError) { + if (isTransientError(handler.loginError)) { this.debug.log('Initiating retry on transient error'); return this.transitionTo(this.STATE.TRANSIENT_FAILURE_RETRY); } else { - this.emit('connect', this.loginError); + this.emit('connect', handler.loginError); return this.transitionTo(this.STATE.FINAL); } } else { @@ -3487,30 +3481,30 @@ class Connection extends EventEmitter { try { tokenResponse = await credentials.getToken(tokenScope); } catch (err) { - this.loginError = new AggregateError( + const aggrErr = new AggregateError( [new ConnectionError('Security token could not be authenticated or authorized.', 'EFEDAUTH'), err]); - this.emit('connect', this.loginError); + this.emit('connect', aggrErr); this.transitionTo(this.STATE.FINAL); return; } // Type guard the token value so that it is never null. if (tokenResponse === null) { - this.loginError = new AggregateError( + const aggrErr = new AggregateError( [new ConnectionError('Security token could not be authenticated or authorized.', 'EFEDAUTH')]); - this.emit('connect', this.loginError); + this.emit('connect', aggrErr); this.transitionTo(this.STATE.FINAL); return; } this.sendFedAuthTokenMessage(tokenResponse.token); - } else if (this.loginError) { - if (isTransientError(this.loginError)) { + } else if (handler.loginError) { + if (isTransientError(handler.loginError)) { this.debug.log('Initiating retry on transient error'); this.transitionTo(this.STATE.TRANSIENT_FAILURE_RETRY); } else { - this.emit('connect', this.loginError); + this.emit('connect', handler.loginError); this.transitionTo(this.STATE.FINAL); } } else { diff --git a/src/token/handler.ts b/src/token/handler.ts index 0c259a723..0a45af80d 100644 --- a/src/token/handler.ts +++ b/src/token/handler.ts @@ -250,10 +250,13 @@ export class Login7TokenHandler extends TokenHandler { declare loginAckReceived: boolean; + declare loginError: ConnectionError | undefined; + constructor(connection: Connection) { super(); this.loginAckReceived = false; this.connection = connection; + this.loginError = undefined; } onInfoMessage(token: InfoMessageToken) { @@ -270,7 +273,7 @@ export class Login7TokenHandler extends TokenHandler { error.isTransient = true; } - this.connection.loginError = error; + this.loginError = error; } onSSPI(token: SSPIToken) { @@ -305,27 +308,27 @@ export class Login7TokenHandler extends TokenHandler { if (authentication.type === 'azure-active-directory-password' || authentication.type === 'azure-active-directory-access-token' || authentication.type === 'azure-active-directory-msi-vm' || authentication.type === 'azure-active-directory-msi-app-service' || authentication.type === 'azure-active-directory-service-principal-secret' || authentication.type === 'azure-active-directory-default') { if (token.fedAuth === undefined) { - this.connection.loginError = new ConnectionError('Did not receive Active Directory authentication acknowledgement'); + this.loginError = new ConnectionError('Did not receive Active Directory authentication acknowledgement'); } else if (token.fedAuth.length !== 0) { - this.connection.loginError = new ConnectionError(`Active Directory authentication acknowledgment for ${authentication.type} authentication method includes extra data`); + this.loginError = new ConnectionError(`Active Directory authentication acknowledgment for ${authentication.type} authentication method includes extra data`); } } else if (token.fedAuth === undefined && token.utf8Support === undefined) { - this.connection.loginError = new ConnectionError('Received acknowledgement for unknown feature'); + this.loginError = new ConnectionError('Received acknowledgement for unknown feature'); } else if (token.fedAuth) { - this.connection.loginError = new ConnectionError('Did not request Active Directory authentication, but received the acknowledgment'); + this.loginError = new ConnectionError('Did not request Active Directory authentication, but received the acknowledgment'); } } onLoginAck(token: LoginAckToken) { if (!token.tdsVersion) { // unsupported TDS version - this.connection.loginError = new ConnectionError('Server responded with unknown TDS version.', 'ETDS'); + this.loginError = new ConnectionError('Server responded with unknown TDS version.', 'ETDS'); return; } if (!token.interface) { // unsupported interface - this.connection.loginError = new ConnectionError('Server responded with unsupported interface.', 'EINTERFACENOTSUPP'); + this.loginError = new ConnectionError('Server responded with unsupported interface.', 'EINTERFACENOTSUPP'); return; }