From 11f96fefb7d13f4e85dc22fc6521f63088b5b6b7 Mon Sep 17 00:00:00 2001 From: Ramya Achutha Rao Date: Fri, 10 May 2019 23:00:34 -0700 Subject: [PATCH] [Service Bus] Better error message when session cannot be locked --- sdk/servicebus/service-bus/src/queueClient.ts | 7 ++++-- .../service-bus/src/session/messageSession.ts | 22 +++++++++++-------- .../service-bus/src/subscriptionClient.ts | 10 +++++---- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/sdk/servicebus/service-bus/src/queueClient.ts b/sdk/servicebus/service-bus/src/queueClient.ts index 6c5a6b5796bd..4b17e8328dc1 100644 --- a/sdk/servicebus/service-bus/src/queueClient.ts +++ b/sdk/servicebus/service-bus/src/queueClient.ts @@ -111,7 +111,8 @@ export class QueueClient implements Client { /** * Creates a Receiver for receiving messages from a Queue which does not have sessions enabled. * - Throws error if an open receiver already exists for this QueueClient. - * - Throws error if the Queue has sessions enabled. + * - Throws `InvalidOperationError` if the Queue has sessions enabled (in which case, use the + * overload of this method which takes `sessionOptions` argument) * * @param receiveMode An enum indicating the mode in which messages should be received. Possible * values are: @@ -129,7 +130,9 @@ export class QueueClient implements Client { * Creates a Receiver for receiving messages from a session enabled Queue. When no sessionId is * given, a random session among the available sessions is used. * - Throws error if an open receiver already exists for given sessionId. - * - Throws error if the Queue does not have sessions enabled. + * - Throws `SessionCannotBeLockedError` if the Queue does not have sessions enabled (in which + * case do not pass the `sessionOptions` argument) or if Service Bus is not able to get a lock on + * the session (in which case try again after some time) * * @param receiveMode An enum indicating the mode in which messages should be received. Possible * values are: diff --git a/sdk/servicebus/service-bus/src/session/messageSession.ts b/sdk/servicebus/service-bus/src/session/messageSession.ts index b56519f848bb..0efa34f1e2a3 100644 --- a/sdk/servicebus/service-bus/src/session/messageSession.ts +++ b/sdk/servicebus/service-bus/src/session/messageSession.ts @@ -1016,16 +1016,20 @@ export class MessageSession extends LinkEntity { this._receiver.source.filter && this._receiver.source.filter[Constants.sessionFilterName]; let errorMessage: string = ""; - // SB allows a sessionId with empty string value :) + // Service Bus creates receiver successfully with no sessionId if it fails to get a lock on + // the session instead of throwing the SessionCannotBeLockedError. So, we throw it instead. if (receivedSessionId == undefined) { - errorMessage = - `Received an incorrect sessionId '${receivedSessionId}' while creating ` + - `the receiver '${this.name}'.`; - } - if (this.sessionId != undefined && receivedSessionId !== this.sessionId) { - errorMessage = - `Received sessionId '${receivedSessionId}' does not match the provided ` + - `sessionId '${this.sessionId}' while creating the receiver '${this.name}'.`; + if (this.sessionId == undefined) { + // User asked for a random session to be picked, but there are no sessions free to take + // a lock on or the Queue/Subscription doesnt have sessions enabled. + errorMessage = `There are no sessions available for receiving messages.`; + } else { + // User passed a sessionId, but cannot get a lock on it either because somebody else + // has a lock on it or the Queue/Subscription doesnt have sessions enabled. + errorMessage = `The session with id '${ + this.sessionId + }' is not available for receiving messages.`; + } } if (errorMessage) { const error = translate({ diff --git a/sdk/servicebus/service-bus/src/subscriptionClient.ts b/sdk/servicebus/service-bus/src/subscriptionClient.ts index aa91b11d6fb8..f319336cbf29 100644 --- a/sdk/servicebus/service-bus/src/subscriptionClient.ts +++ b/sdk/servicebus/service-bus/src/subscriptionClient.ts @@ -109,9 +109,9 @@ export class SubscriptionClient implements Client { /** * Creates a Receiver for receiving messages from a Subscription which does not have sessions enabled. - * Throws error if an open receiver already exists for this SubscriptionClient. - * - * Throws error if the Subscription has sessions enabled. + * - Throws error if an open receiver already exists for this SubscriptionClient. + * - Throws `InvalidOperationError` if the Subscription has sessions enabled (in which case, use the + * overload of this method which takes `sessionOptions` argument) * * @param receiveMode An enum indicating the mode in which messages should be received. Possible * values are: @@ -129,7 +129,9 @@ export class SubscriptionClient implements Client { * Creates a Receiver for receiving messages from a session enabled Subscription. When no sessionId is * given, a random session among the available sessions is used. * - Throws error if an open receiver already exists for given sessionId. - * - Throws error if the Queue does not have sessions enabled. + * - Throws `SessionCannotBeLockedError` if the Subscription does not have sessions enabled (in which + * case do not pass the `sessionOptions` argument) or if Service Bus is not able to get a lock on + * the session (in which case try again after some time) * * @param receiveMode An enum indicating the mode in which messages should be received. Possible * values are: