Skip to content

Simplify error handling in MetricScope wrapper #79

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

Merged
merged 1 commit into from
Feb 11, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 2 additions & 11 deletions src/logger/MetricScope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,18 @@ import { createMetricsLogger } from './MetricsLoggerFactory';
*/
const metricScope = <T, U extends readonly unknown[]>(
handler: (m: MetricsLogger) => (...args: U) => T | Promise<T>,
): ((...args: U) => Promise<T | undefined>) => {
const wrappedHandler = async (...args: U): Promise<T | undefined> => {
): ((...args: U) => Promise<T>) => {
const wrappedHandler = async (...args: U): Promise<T> => {
const metrics = createMetricsLogger();
let exception;
try {
return await handler(metrics)(...args);
} catch (e) {
exception = e;
} finally {
try {
await metrics.flush();
} catch (e) {
LOG('Failed to flush metrics', e);
}
}

if (exception) {
throw exception;
}

return;
};
return wrappedHandler;
};
Expand Down
73 changes: 72 additions & 1 deletion src/logger/__tests__/MetricScope.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import sleep from '../../../test/utils/Sleep';
import { metricScope } from '../MetricScope';
import { MetricsLogger } from '../MetricsLogger';

const mockEnvironment = jest.fn();
jest.mock('../../logger/MetricsLoggerFactory', () => {
return {
createMetricsLogger: () => new MetricsLogger(jest.fn()),
createMetricsLogger: () => new MetricsLogger(mockEnvironment),
};
});

Expand Down Expand Up @@ -115,3 +116,73 @@ test('sync scope returns child function return value', async () => {
// assert
expect(result).toBe(expected);
});

test('async scope rejects with child function reject value', async () => {
// arrange
const expected = true;

const handler = metricScope(() => async () => {
return await Promise.reject(expected);
});

// act
const result = handler();

// assert
await expect(result).rejects.toBe(expected);
});

test('sync scope rejects with child function error', async () => {
// arrange
const expected = true;

const handler = metricScope(() => () => {
throw expected;
});

// act
// the customer can pass in a synchronous function, but we will still return
// an async function back to the Lambda caller
const result = handler();

// assert
await expect(result).rejects.toBe(expected);
});

test('async scope flush is still called when child function rejects', async () => {
// arrange
mockEnvironment.mockReset();
const handler = metricScope(() => async () => {
return await Promise.reject('error');
});

// act
try {
await handler();
} catch (e) {
// ignored
}

// assert
expect(mockEnvironment).toHaveBeenCalled();
});

test('sync scope flush is still called when child function throws', async () => {
// arrange
mockEnvironment.mockReset();
const handler = metricScope(() => () => {
throw 'error';
});

// act
// the customer can pass in a synchronous function, but we will still return
// an async function back to the Lambda caller
try {
await handler();
} catch (e) {
// ignored
}

// assert
expect(mockEnvironment).toHaveBeenCalled();
});