From 6ef36e7c72622a92f243e5fa6b60bd2264fa82b8 Mon Sep 17 00:00:00 2001 From: Christopher Radek Date: Wed, 3 Feb 2021 14:49:17 -0800 Subject: [PATCH 1/4] [core-amqp] improve error message on TypeError: sendRequest of undefined --- sdk/core/core-amqp/CHANGELOG.md | 2 ++ sdk/core/core-amqp/src/cbs.ts | 6 +++++- sdk/core/core-amqp/test/cbs.spec.ts | 24 ++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 sdk/core/core-amqp/test/cbs.spec.ts diff --git a/sdk/core/core-amqp/CHANGELOG.md b/sdk/core/core-amqp/CHANGELOG.md index 8e68260dab1d..4467a97ce42d 100644 --- a/sdk/core/core-amqp/CHANGELOG.md +++ b/sdk/core/core-amqp/CHANGELOG.md @@ -2,6 +2,8 @@ ## 2.1.0 (Unreleased) +- Fixes the bug reported in issue [13048](https://github.com/Azure/azure-sdk-for-js/issues/13048). + Now an informative error is thrown describing the circumstance that led to the error. - Adds the ability to configure the `amqpHostname` and `port` that a `ConnectionContextBase` will use when connecting to a service. The `host` field refers to the DNS host or IP address of the service, whereas the `amqpHostname` is the fully qualified host name of the service. Normally `host` and `amqpHostname` will be the same. diff --git a/sdk/core/core-amqp/src/cbs.ts b/sdk/core/core-amqp/src/cbs.ts index 52817c92a20b..5d11bae9f9df 100644 --- a/sdk/core/core-amqp/src/cbs.ts +++ b/sdk/core/core-amqp/src/cbs.ts @@ -190,6 +190,10 @@ export class CbsClient { tokenType: TokenType ): Promise { try { + if (!this._cbsSenderReceiverLink) { + throw new Error("Attempted to negotiate a claim but a CBS link does not exist."); + } + const request: RheaMessage = { body: token, message_id: generate_uuid(), @@ -201,7 +205,7 @@ export class CbsClient { type: tokenType } }; - const responseMessage = await this._cbsSenderReceiverLink!.sendRequest(request); + const responseMessage = await this._cbsSenderReceiverLink.sendRequest(request); logger.verbose("[%s] The CBS response is: %O", this.connection.id, responseMessage); return this._fromRheaMessageResponse(responseMessage); } catch (err) { diff --git a/sdk/core/core-amqp/test/cbs.spec.ts b/sdk/core/core-amqp/test/cbs.spec.ts new file mode 100644 index 000000000000..8fdb7a2af57b --- /dev/null +++ b/sdk/core/core-amqp/test/cbs.spec.ts @@ -0,0 +1,24 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + +import { assert } from "chai"; +import { Connection } from "rhea-promise"; +import { stub } from "sinon"; +import { CbsClient, TokenType } from "../src"; + +describe("CbsClient", function() { + const TEST_FAILURE = "Test failure"; + describe("negotiateClaim", function() { + it("throws an error if the cbs link doesn't exist.", async function() { + const connectionStub = stub(new Connection()) as any; + + const cbsClient = new CbsClient(connectionStub, "lock"); + try { + await cbsClient.negotiateClaim("audience", "token", TokenType.CbsTokenTypeSas); + throw new Error(TEST_FAILURE); + } catch (err) { + assert.equal(err.message, "Attempted to negotiate a claim but a CBS link does not exist."); + } + }); + }); +}); From 8844cb8ce8029526de9647997a4ffc11413090ac Mon Sep 17 00:00:00 2001 From: chradek <51000525+chradek@users.noreply.github.com> Date: Wed, 3 Feb 2021 15:41:41 -0800 Subject: [PATCH 2/4] Update sdk/core/core-amqp/src/cbs.ts Co-authored-by: Harsha Nalluru --- sdk/core/core-amqp/src/cbs.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/core/core-amqp/src/cbs.ts b/sdk/core/core-amqp/src/cbs.ts index 5d11bae9f9df..b767456e6e44 100644 --- a/sdk/core/core-amqp/src/cbs.ts +++ b/sdk/core/core-amqp/src/cbs.ts @@ -191,7 +191,7 @@ export class CbsClient { ): Promise { try { if (!this._cbsSenderReceiverLink) { - throw new Error("Attempted to negotiate a claim but a CBS link does not exist."); + throw new Error("Attempted to negotiate a claim but the CBS link does not exist."); } const request: RheaMessage = { From e6a5e5dc7727415ccc6752692a686e139cf951a7 Mon Sep 17 00:00:00 2001 From: Christopher Radek Date: Thu, 4 Feb 2021 16:07:21 -0800 Subject: [PATCH 3/4] [core-amqp] fix test after error message change --- sdk/core/core-amqp/test/cbs.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/core/core-amqp/test/cbs.spec.ts b/sdk/core/core-amqp/test/cbs.spec.ts index 8fdb7a2af57b..9a4704fdce9a 100644 --- a/sdk/core/core-amqp/test/cbs.spec.ts +++ b/sdk/core/core-amqp/test/cbs.spec.ts @@ -17,7 +17,7 @@ describe("CbsClient", function() { await cbsClient.negotiateClaim("audience", "token", TokenType.CbsTokenTypeSas); throw new Error(TEST_FAILURE); } catch (err) { - assert.equal(err.message, "Attempted to negotiate a claim but a CBS link does not exist."); + assert.equal(err.message, "Attempted to negotiate a claim but the CBS link does not exist."); } }); }); From 311d9b3a2eb0e35504f650d95a03c8f701a9cc22 Mon Sep 17 00:00:00 2001 From: Christopher Radek Date: Thu, 4 Feb 2021 16:08:38 -0800 Subject: [PATCH 4/4] [core-amqp] fix test after error message change --- sdk/core/core-amqp/test/cbs.spec.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/sdk/core/core-amqp/test/cbs.spec.ts b/sdk/core/core-amqp/test/cbs.spec.ts index 9a4704fdce9a..5adbed4db6f5 100644 --- a/sdk/core/core-amqp/test/cbs.spec.ts +++ b/sdk/core/core-amqp/test/cbs.spec.ts @@ -17,7 +17,10 @@ describe("CbsClient", function() { await cbsClient.negotiateClaim("audience", "token", TokenType.CbsTokenTypeSas); throw new Error(TEST_FAILURE); } catch (err) { - assert.equal(err.message, "Attempted to negotiate a claim but the CBS link does not exist."); + assert.equal( + err.message, + "Attempted to negotiate a claim but the CBS link does not exist." + ); } }); });