From ef2b4e62e5dd2ad4197caf5efaa5809917cca35d Mon Sep 17 00:00:00 2001 From: ShivangiReja <45216704+ShivangiReja@users.noreply.github.com> Date: Mon, 15 Jul 2019 17:38:07 -0700 Subject: [PATCH] [Event Hubs] [Service Bus] Added event handlers for `error` and `protocolError` events on the connection object. (#4323) --- .../event-hubs/src/connectionContext.ts | 36 +++++++++++++++++++ sdk/servicebus/service-bus/changelog.md | 1 + .../service-bus/src/connectionContext.ts | 36 +++++++++++++++++++ 3 files changed, 73 insertions(+) diff --git a/sdk/eventhub/event-hubs/src/connectionContext.ts b/sdk/eventhub/event-hubs/src/connectionContext.ts index c49b3e778aaa..3ef398f96984 100644 --- a/sdk/eventhub/event-hubs/src/connectionContext.ts +++ b/sdk/eventhub/event-hubs/src/connectionContext.ts @@ -237,9 +237,45 @@ export namespace ConnectionContext { } }; + const protocolError: OnAmqpEvent = async (context: EventContext) => { + if (context.connection && context.connection.error) { + log.error( + "[%s] Error (context.connection.error) occurred on the amqp connection: %O", + connectionContext.connection.id, + context.connection && context.connection.error + ); + } + if (context.error) { + log.error( + "[%s] Error (context.error) occurred on the amqp connection: %O", + connectionContext.connection.id, + context.error + ); + } + }; + + const error: OnAmqpEvent = async (context: EventContext) => { + if (context.connection && context.connection.error) { + log.error( + "[%s] Error (context.connection.error) occurred on the amqp connection: %O", + connectionContext.connection.id, + context.connection && context.connection.error + ); + } + if (context.error) { + log.error( + "[%s] Error (context.error) occurred on the amqp connection: %O", + connectionContext.connection.id, + context.error + ); + } + }; + // Add listeners on the connection object. connectionContext.connection.on(ConnectionEvents.connectionOpen, onConnectionOpen); connectionContext.connection.on(ConnectionEvents.disconnected, disconnected); + connectionContext.connection.on(ConnectionEvents.protocolError, protocolError); + connectionContext.connection.on(ConnectionEvents.error, error); log.context("[%s] Created connection context successfully.", connectionContext.connectionId); return connectionContext; diff --git a/sdk/servicebus/service-bus/changelog.md b/sdk/servicebus/service-bus/changelog.md index f545ed0deebd..4005d1072245 100644 --- a/sdk/servicebus/service-bus/changelog.md +++ b/sdk/servicebus/service-bus/changelog.md @@ -1,6 +1,7 @@ # 2019-07-09 1.0.3 - Update `amqp-common` dependency version to 1.0.0-preview.6. This includes fix for the [bug 3971](https://github.com/Azure/azure-sdk-for-js/issues/3971) where the token audience in the credential created during [MSI based login](https://www.npmjs.com/package/@azure/ms-rest-nodeauth/v/2.0.2#msi-managed-service-identity-based-login-from-a-virtual-machine-created-in-azure) was being ignored. [PR 4146](https://github.com/Azure/azure-sdk-for-js/pull/4146) +- Added event handlers for `error` and `protocolError` events on the connection object to avoid the case of unhandled exceptions like [bug 4136](https://github.com/Azure/azure-sdk-for-js/issues/4136) # 2019-05-21 1.0.2 diff --git a/sdk/servicebus/service-bus/src/connectionContext.ts b/sdk/servicebus/service-bus/src/connectionContext.ts index 968a3e858382..5a572e641371 100644 --- a/sdk/servicebus/service-bus/src/connectionContext.ts +++ b/sdk/servicebus/service-bus/src/connectionContext.ts @@ -146,9 +146,45 @@ export namespace ConnectionContext { } }; + const protocolError: OnAmqpEvent = async (context: EventContext) => { + if (context.connection && context.connection.error) { + log.error( + "[%s] Error (context.connection.error) occurred on the amqp connection: %O", + connectionContext.connection.id, + context.connection && context.connection.error + ); + } + if (context.error) { + log.error( + "[%s] Error (context.error) occurred on the amqp connection: %O", + connectionContext.connection.id, + context.error + ); + } + }; + + const error: OnAmqpEvent = async (context: EventContext) => { + if (context.connection && context.connection.error) { + log.error( + "[%s] Error (context.connection.error) occurred on the amqp connection: %O", + connectionContext.connection.id, + context.connection && context.connection.error + ); + } + if (context.error) { + log.error( + "[%s] Error (context.error) occurred on the amqp connection: %O", + connectionContext.connection.id, + context.error + ); + } + }; + // Add listeners on the connection object. connectionContext.connection.on(ConnectionEvents.connectionOpen, onConnectionOpen); connectionContext.connection.on(ConnectionEvents.disconnected, disconnected); + connectionContext.connection.on(ConnectionEvents.protocolError, protocolError); + connectionContext.connection.on(ConnectionEvents.error, error); log.connectionCtxt( "[%s] Created connection context successfully.",