Skip to content

Commit

Permalink
chore: adding async function for safe execute in instrumentation (#1803)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Dyla <[email protected]>
  • Loading branch information
obecny and dyladan authored Jan 13, 2021
1 parent 7c93b2a commit 139d9db
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 4 deletions.
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);
});
});

0 comments on commit 139d9db

Please sign in to comment.