Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: adding async function for safe execute in instrumentation #1803

Merged
merged 4 commits into from
Jan 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions packages/opentelemetry-instrumentation/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,32 @@ export function safeExecuteInTheMiddle<T>(
}
}

/**
* Async function to execute patched function and being able to catch errors
* @param execute - function to be executed
* @param onFinish - callback to run when execute finishes
*/
export async function safeExecuteInTheMiddleAsync<T>(
execute: () => T,
onFinish: (e: Error | undefined, result: T | undefined) => void,
preventThrowingError?: boolean
): Promise<T> {
let error: Error | undefined;
let result: T | undefined;
try {
result = await execute();
} catch (e) {
error = e;
} finally {
onFinish(error, result);
if (error && !preventThrowingError) {
// eslint-disable-next-line no-unsafe-finally
throw error;
}
// eslint-disable-next-line no-unsafe-finally
return result as T;
}
}
/**
* Checks if certain function has been already wrapped
* @param func
Expand Down
55 changes: 51 additions & 4 deletions packages/opentelemetry-instrumentation/test/common/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
*/

import * as assert from 'assert';
import { isWrapped, safeExecuteInTheMiddle } from '../../src';
import {
isWrapped,
safeExecuteInTheMiddle,
safeExecuteInTheMiddleAsync,
} from '../../src';

describe('isWrapped', () => {
describe('when function is wrapped', () => {
Expand Down Expand Up @@ -45,13 +49,12 @@ describe('isWrapped', () => {

describe('safeExecuteInTheMiddle', () => {
it('should not throw error', () => {
const error = new Error('test');
safeExecuteInTheMiddle(
() => {
throw error;
return 'foo';
},
err => {
assert.deepStrictEqual(error, err);
assert.deepStrictEqual(err, undefined);
},
true
);
Expand Down Expand Up @@ -84,3 +87,47 @@ describe('safeExecuteInTheMiddle', () => {
assert.deepStrictEqual(result, 1);
});
});

describe('safeExecuteInTheMiddleAsync', () => {
it('should not throw error', () => {
safeExecuteInTheMiddleAsync(
async () => {
await setTimeout(() => {}, 1);
return 'foo';
},
err => {
assert.deepStrictEqual(err, undefined);
},
true
);
});
it('should throw error', () => {
const error = new Error('test');
try {
safeExecuteInTheMiddleAsync(
async () => {
await setTimeout(() => {}, 1);
throw error;
},
err => {
assert.deepStrictEqual(error, err);
}
);
} catch (err) {
assert.deepStrictEqual(error, err);
}
});
it('should return result', async () => {
const result = await safeExecuteInTheMiddleAsync(
async () => {
await setTimeout(() => {}, 1);
return 1;
},
(err, result) => {
assert.deepStrictEqual(err, undefined);
assert.deepStrictEqual(result, 1);
}
);
assert.deepStrictEqual(result, 1);
});
});