Skip to content

Commit

Permalink
Don't swallow exceptions caused by SDK user.
Browse files Browse the repository at this point in the history
  • Loading branch information
Flarna committed Dec 19, 2018
1 parent ba8ab7b commit 3ee7ecf
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ see also [Releases](https://github.com/Dynatrace/OneAgent-SDK-for-NodeJs/release

|Version|Description |
|:------|:-------------------------------------------|
|1.2.2 |don't swallow exceptions |
|1.2.1 |improve type definitions |
|1.2.0 |add support for custom request attributes |
|1.1.0 |add setResultData() for SQL Database tracer |
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dynatrace/oneagent-sdk",
"version": "1.2.1",
"version": "1.2.2",
"description": "Node.js SDK for Dynatrace OneAgent",
"engines": {
"node": ">= 4.0.0"
Expand Down Expand Up @@ -36,14 +36,14 @@
],
"devDependencies": {
"@types/mocha": "5.2.5",
"@types/node": "10.12.12",
"@types/sinon": "5.0.7",
"@types/node": "10.12.17",
"@types/sinon": "7.0.0",
"mocha": "5.2.0",
"rimraf": "2.6.2",
"sinon": "7.1.1",
"sinon": "7.2.2",
"source-map-support": "0.5.9",
"tslint": "5.11.0",
"typescript": "3.2.1"
"tslint": "5.12.0",
"typescript": "3.2.2"
},
"author": "Dynatrace",
"license": "Apache-2.0"
Expand Down
4 changes: 2 additions & 2 deletions samples/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sdksamples",
"version": "1.2.1",
"version": "1.2.2",
"description": "Samples showing API for OneAgent SDK",
"engines": {
"node": ">= 8.0.0"
Expand All @@ -11,7 +11,7 @@
"customrequestattribute": "node CustomRequestAttributes/CustomRequestAttributesSample.js"
},
"dependencies": {
"@dynatrace/oneagent-sdk": "1.2.1"
"@dynatrace/oneagent-sdk": "1.2.2"
},
"repository": {
"type": "git",
Expand Down
11 changes: 10 additions & 1 deletion src/Stub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,16 @@ function BufferFrom(str: string, encoding?: string): Buffer {

class DummyTracer implements Sdk.Tracer {
public start<R>(handler: (...args: any[]) => R, ...args: any[]): R {
if (typeof handler !== "function") {
throw new TypeError(`OneAgent SDK: Tracer.start() first parameter must be a function, but received ${typeof(handler)}`);
}
return handler.call(undefined, ...args);
}

public startWithContext<R>(handler: (...args: any[]) => R, thisObj?: any, ...args: any[]): R {
if (typeof handler !== "function") {
throw new TypeError(`OneAgent SDK: Tracer.start() first parameter must be a function, but received ${typeof(handler)}`);
}
return handler.call(thisObj, ...args);
}

Expand All @@ -51,7 +57,10 @@ class DummyOutgoingTracer extends DummyTracer implements Sdk.OutgoingTracer {
}

public endWithContext<R>(handler?: (...args: any[]) => R, thisObj?: any, ...args: any[]): R | undefined {
if (typeof handler === "function") {
if (handler !== undefined) {
if (typeof handler !== "function") {
throw new TypeError(`OneAgent SDK: Tracer.end() first parameter must be a function, but received ${typeof(handler)}`);
}
return handler.call(thisObj, ...args);
}
return undefined;
Expand Down
38 changes: 36 additions & 2 deletions test/DummyApiTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,31 @@ describe("Dummy", () => {
Sinon.assert.calledWithExactly(handlerStub, 1, "2", argObj);
Sinon.assert.calledOn(handlerStub, thisArg);
Assert.strictEqual(rc, expRc);

handlerStub.resetHistory();

rc = tracer.start(handlerStub, "3", argObj, 5);
Sinon.assert.callCount(handlerStub, 1);
Sinon.assert.calledWithExactly(handlerStub, "3", argObj, 5);
Sinon.assert.calledOn(handlerStub, undefined);
Assert.strictEqual(rc, expRc);
handlerStub.resetHistory();

Assert.throws(() => tracer.startWithContext({} as any, "this"), "TypeError: OneAgent SDK: Tracer.start() first parameter must be a function, but received object");
Assert.throws(() => (tracer as any).startWithContext(), "TypeError: OneAgent SDK: Tracer.start() first parameter must be a function, but received undefined");
Assert.throws(() => tracer.start(15 as any), "TypeError: OneAgent SDK: Tracer.start() first parameter must be a function, but received number");
Assert.throws(() => (tracer as any).start(), "TypeError: OneAgent SDK: Tracer.start() first parameter must be a function, but received undefined");

const expError = new RangeError("TestError");
handlerStub.throws(expError);
Assert.throws(() => tracer.startWithContext(handlerStub, thisArg), expError);
Sinon.assert.callCount(handlerStub, 1);
Sinon.assert.calledOn(handlerStub, thisArg);
handlerStub.resetHistory();

Assert.throws(() => tracer.start(handlerStub), expError);
Sinon.assert.callCount(handlerStub, 1);
Sinon.assert.calledOn(handlerStub, undefined);
handlerStub.resetHistory();
}

// ------------------------------------------------------------------------
Expand All @@ -70,13 +88,29 @@ describe("Dummy", () => {
Sinon.assert.calledOn(handlerStub, thisArg);
Assert.strictEqual(rc, expRc);
tracer.end();

handlerStub.resetHistory();

rc = tracer.end(handlerStub, "3", argObj, 5);
Sinon.assert.callCount(handlerStub, 1);
Sinon.assert.calledWithExactly(handlerStub, "3", argObj, 5);
Sinon.assert.calledOn(handlerStub, undefined);
Assert.strictEqual(rc, expRc);
handlerStub.resetHistory();

Assert.throws(() => tracer.endWithContext({} as any, "this"), "TypeError: OneAgent SDK: Tracer.end() first parameter must be a function, but received object");
Assert.throws(() => tracer.end(15 as any), "TypeError: OneAgent SDK: Tracer.end() first parameter must be a function, but received number");

const expError = new RangeError("TestError");
handlerStub.throws(expError);
Assert.throws(() => tracer.endWithContext(handlerStub, thisArg), expError);
Sinon.assert.callCount(handlerStub, 1);
Sinon.assert.calledOn(handlerStub, thisArg);
handlerStub.resetHistory();

Assert.throws(() => tracer.end(handlerStub), expError);
Sinon.assert.callCount(handlerStub, 1);
Sinon.assert.calledOn(handlerStub, undefined);
handlerStub.resetHistory();
}

// ========================================================================
Expand Down

0 comments on commit 3ee7ecf

Please sign in to comment.