Skip to content

Commit

Permalink
[Event Hubs] Improve error messages when constructing client (#4899)
Browse files Browse the repository at this point in the history
* [Event Hubs] Improve error messages when constructing client
  • Loading branch information
ShivangiReja authored Aug 27, 2019
1 parent 60839d2 commit 3d25b37
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 12 deletions.
32 changes: 30 additions & 2 deletions sdk/eventhub/event-hubs/src/eventHubClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import {
ConnectionConfig,
isTokenCredential,
RetryOptions,
Constants
Constants,
parseConnectionString,
EventHubConnectionStringModel
} from "@azure/core-amqp";

import { ConnectionContext } from "./connectionContext";
Expand Down Expand Up @@ -285,6 +287,29 @@ export class EventHubClient {
hostOrConnectionString = String(hostOrConnectionString);

if (!isTokenCredential(credentialOrOptions)) {
const parsedCS = parseConnectionString<EventHubConnectionStringModel>(hostOrConnectionString);
if (
!(
parsedCS.EntityPath ||
(typeof eventHubNameOrOptions === "string" && eventHubNameOrOptions)
)
) {
throw new TypeError(
`Either provide "eventHubName" or the "connectionString": "${hostOrConnectionString}", ` +
`must contain "EntityPath=<your-event-hub-name>".`
);
}
if (
parsedCS.EntityPath &&
typeof eventHubNameOrOptions === "string" &&
eventHubNameOrOptions &&
parsedCS.EntityPath !== eventHubNameOrOptions
) {
throw new TypeError(
`The entity path "${parsedCS.EntityPath}" in connectionString: "${hostOrConnectionString}" ` +
`doesn't match with eventHubName: "${eventHubNameOrOptions}".`
);
}
connectionString = hostOrConnectionString;
if (typeof eventHubNameOrOptions !== "string") {
// connectionstring and/or options were passed to constructor
Expand All @@ -303,6 +328,9 @@ export class EventHubClient {
const eventHubName = eventHubNameOrOptions;
let host = hostOrConnectionString;
credential = credentialOrOptions;
if (!eventHubName) {
throw new TypeError(`"eventHubName" is missing`);
}

if (!host.endsWith("/")) host += "/";
connectionString = `Endpoint=sb://${host};SharedAccessKeyName=defaultKeyName;SharedAccessKey=defaultKeyValue;EntityPath=${eventHubName}`;
Expand Down Expand Up @@ -396,7 +424,7 @@ export class EventHubClient {
* - `ownerLevel` : A number indicating that the consumer intends to be an exclusive consumer of events resulting in other
* consumers to fail if their `ownerLevel` is lower or doesn't exist.
* - `retryOptions`: The retry options used to govern retry attempts when an issue is encountered while receiving events.
*
*
* @throws {Error} Thrown if the underlying connection has been closed, create a new EventHubClient.
* @throws {TypeError} Thrown if a required parameter is missing.
*/
Expand Down
34 changes: 24 additions & 10 deletions sdk/eventhub/event-hubs/test/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,47 @@ import { EnvironmentCredential } from "@azure/identity";
const env = getEnvVars();

describe("Create EventHubClient #RunnableInBrowser", function(): void {
it("throws when it cannot find the Event Hub path", function(): void {
it("throws when it cannot find the Event Hub name", function(): void {
const connectionString = "Endpoint=sb://abc";
const test = function(): EventHubClient {
return new EventHubClient(connectionString);
};
test.should.throw(
Error,
`Either provide "path" or the "connectionString": "${connectionString}", ` +
`must contain EntityPath="<path-to-the-entity>".`
`Either provide "eventHubName" or the "connectionString": "${connectionString}", ` +
`must contain "EntityPath=<your-event-hub-name>".`
);
});

it("throws when EntityPath in Connection string doesn't match with event hub name parameter", function(): void {
const connectionString =
"Endpoint=sb://a;SharedAccessKeyName=b;SharedAccessKey=c=;EntityPath=my-event-hub-name";
const eventHubName = "event-hub-name";
const test = function(): EventHubClient {
return new EventHubClient(connectionString, eventHubName);
};
test.should.throw(
Error,
`The entity path "my-event-hub-name" in connectionString: "${connectionString}" ` +
`doesn't match with eventHubName: "${eventHubName}".`
);
});

it("creates an EventHubClient from a connection string", function(): void {
const client = new EventHubClient(
"Endpoint=sb://a;SharedAccessKeyName=b;SharedAccessKey=c;EntityPath=my-event-hub-path"
"Endpoint=sb://a;SharedAccessKeyName=b;SharedAccessKey=c;EntityPath=my-event-hub-name"
);
client.should.be.an.instanceof(EventHubClient);
should.equal(client.eventHubName, "my-event-hub-path");
should.equal(client.eventHubName, "my-event-hub-name");
});

it("creates an EventHubClient from a connection string and an Event Hub path", function(): void {
it("creates an EventHubClient from a connection string and an Event Hub name", function(): void {
const client = new EventHubClient(
"Endpoint=sb://a;SharedAccessKeyName=b;SharedAccessKey=c",
"my-event-hub-path"
"my-event-hub-name"
);
client.should.be.an.instanceof(EventHubClient);
should.equal(client.eventHubName, "my-event-hub-path");
should.equal(client.eventHubName, "my-event-hub-name");
});

it("creates an EventHubClient from a custom TokenCredential", function(): void {
Expand All @@ -61,9 +75,9 @@ describe("Create EventHubClient #RunnableInBrowser", function(): void {
};
}
};
const client = new EventHubClient("abc", "my-event-hub-path", dummyCredential);
const client = new EventHubClient("abc","my-event-hub-name", dummyCredential);
client.should.be.an.instanceof(EventHubClient);
should.equal(client.eventHubName, "my-event-hub-path");
should.equal(client.eventHubName, "my-event-hub-name");
});

it("creates an EventHubClient from an Azure.Identity credential", async function(): Promise<
Expand Down

0 comments on commit 3d25b37

Please sign in to comment.