Skip to content

Commit

Permalink
Add error when ikey/connection string not present in TelemetryClient (m…
Browse files Browse the repository at this point in the history
  • Loading branch information
hectorhdzg authored Sep 7, 2022
1 parent ab79844 commit c507448
Show file tree
Hide file tree
Showing 6 changed files with 367 additions and 500 deletions.
5 changes: 0 additions & 5 deletions Library/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,6 @@ class Config implements IConfig {

const instrumentationKeyEnv: string | undefined = this._instrumentationKey;
this.instrumentationKey = csCode.instrumentationkey || iKeyCode /* === instrumentationKey */ || csEnv.instrumentationkey || instrumentationKeyEnv;

if (!this.instrumentationKey || this.instrumentationKey == "") {
throw new Error("Instrumentation key not found, please provide a connection string before starting the server");
}

let endpoint = `${this.endpointUrl || csCode.ingestionendpoint || csEnv.ingestionendpoint || this._endpointBase}`;
if (endpoint.endsWith("/")) {
// Remove extra '/' if present
Expand Down
3 changes: 3 additions & 0 deletions Library/TelemetryClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class TelemetryClient {
constructor(setupString?: string) {
var config = new Config(setupString);
this.config = config;
if (!this.config.instrumentationKey || this.config.instrumentationKey == "") {
throw new Error("Instrumentation key not found, please provide a connection string before starting Application Insights SDK.");
}
this.context = new Context();
this.commonProperties = {};
this.authorizationHandler = null;
Expand Down
8 changes: 7 additions & 1 deletion Tests/Bootstrap/Default.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ describe("#setupAndStart()", () => {

sinon.stub(Helpers, "sdkAlreadyExists", () => false);
// Test
assert.throws(function () { const Default = require("../../Bootstrap/Default") as typeof DefaultTypes; });
const Default = require("../../Bootstrap/Default") as typeof DefaultTypes;
Default.setLogger(new DiagnosticLogger(logger));

let result = Default.setupAndStart();
assert.equal(result, null);
assert.equal(logger.logCount, 0);
assert.equal(logger.errorCount, 1);
});
});
7 changes: 7 additions & 0 deletions Tests/Library/Client.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ describe("Library/TelemetryClient", () => {
};
assert.ok(client.getAuthorizationHandler(client.config));
});

it("should throw if no iKey is available", () => {
var env = {};
process.env = env;
assert.throws(() => new Client());
});

});

describe("#trackEvent()", () => {
Expand Down
7 changes: 1 addition & 6 deletions Tests/Library/Config.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,7 @@ describe("Library/Config", () => {
http.request.restore();
https.request.restore();
});
it("should throw if no iKey is available", () => {
var env = {};
process.env = env;
assert.throws(() => new Config());
});


it("should read iKey from environment", () => {
var env = <{ [id: string]: string }>{};
env[Config.ENV_iKey] = iKey;
Expand Down
Loading

0 comments on commit c507448

Please sign in to comment.