Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
1 change: 1 addition & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ For notes on migrating to 2.x / 0.200.x see [the upgrade guide](doc/upgrade-to-2
is initialized.
* feat(otlp-exporter-base, otlp-grpc-exporter-base): add an option to let an SDK distribution prepend their own user-agent string in HTTP & GRPC exporters [#5928](https://github.com/open-telemetry/opentelemetry-js/pull/5928) @david-luna
* feat(web): add session handling implementation [5173](https://github.com/open-telemetry/opentelemetry-js/pull/5173) @martinkuba
* feat(instrumentation): allow error of safeExecuteInTheMiddleAsync to be async [#6032](https://github.com/open-telemetry/opentelemetry-js/pull/6032) @JPeer264
Comment thread
JPeer264 marked this conversation as resolved.
Outdated

### :bug: Bug Fixes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ export function safeExecuteInTheMiddle<T>(
*/
export async function safeExecuteInTheMiddleAsync<T>(
execute: () => T,
onFinish: (e: Error | undefined, result: T | undefined) => void,
onFinish: (
e: Error | undefined,
result: T | undefined
) => Promise<void> | void,
preventThrowingError?: boolean
): Promise<T> {
let error: Error | undefined;
Expand All @@ -60,7 +63,7 @@ export async function safeExecuteInTheMiddleAsync<T>(
} catch (e) {
error = e;
} finally {
onFinish(error, result);
await onFinish(error, result);
if (error && !preventThrowingError) {
// eslint-disable-next-line no-unsafe-finally
throw error;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,15 @@ describe('safeExecuteInTheMiddle', function () {
});

describe('safeExecuteInTheMiddleAsync', function () {
it('should not throw error', function () {
it('should not throw error', function (done) {
safeExecuteInTheMiddleAsync(
async () => {
await setTimeout(() => {}, 1);
await new Promise(res => setTimeout(res, 1));
return 'foo';
},
err => {
assert.deepStrictEqual(err, undefined);
done();
},
true
);
Expand All @@ -106,7 +107,7 @@ describe('safeExecuteInTheMiddleAsync', function () {
try {
await safeExecuteInTheMiddleAsync(
async () => {
await setTimeout(() => {}, 1);
await new Promise(res => setTimeout(res, 1));
throw error;
},
err => {
Expand All @@ -120,7 +121,7 @@ describe('safeExecuteInTheMiddleAsync', function () {
it('should return result', async function () {
const result = await safeExecuteInTheMiddleAsync(
async () => {
await setTimeout(() => {}, 1);
await new Promise(res => setTimeout(res, 1));
return 1;
},
(err, result) => {
Expand All @@ -130,4 +131,17 @@ describe('safeExecuteInTheMiddleAsync', function () {
);
assert.deepStrictEqual(result, 1);
});
it('should wait for the error', async function () {
const result = await Promise.race([
safeExecuteInTheMiddleAsync(
() => 1,
async () => {
await new Promise(res => setTimeout(res, 100));
}
),
new Promise(res => setTimeout(() => res('waited'), 10)),
]);

assert.deepStrictEqual(result, 'waited');
});
});