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

fix: ignore analytics request failures #2346

Merged
1 commit merged into from
Nov 8, 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
6 changes: 4 additions & 2 deletions src/lib/analytics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function allowAnalytics(): boolean {
* given by the data parameter, or called from {@link addDataAndSend}.
* @param customData the analytics data to send to the backend.
*/
export async function postAnalytics(
async function postAnalytics(
customData,
): Promise<void | { res: needle.NeedleResponse; body: any }> {
// if the user opt'ed out of analytics, then let's bail out early
Expand Down Expand Up @@ -80,7 +80,7 @@ export async function postAnalytics(
const queryString =
Object.keys(queryStringParams).length > 0 ? queryStringParams : undefined;

return makeRequest({
const res = await makeRequest({
body: {
data: analyticsData,
},
Expand All @@ -90,6 +90,8 @@ export async function postAnalytics(
method: 'post',
headers: headers,
});

return res;
} catch (err) {
debug('analytics', err); // this swallows the analytics error
}
Expand Down
20 changes: 19 additions & 1 deletion test/jest/unit/lib/analytics/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,36 @@ import { argsFrom } from './utils';
import * as apiTokenModule from '../../../../../src/lib/api-token';

describe('analytics module', () => {
afterEach(() => {
jest.restoreAllMocks();
});

it('sends anaytics with no token set', async () => {
analytics.add('k1', 'v1');
const requestSpy = jest.spyOn(request, 'makeRequest');
requestSpy.mockResolvedValue();

const someTokenExistsSpy = jest.spyOn(apiTokenModule, 'someTokenExists');
someTokenExistsSpy.mockReturnValue(false);
requestSpy.mockImplementation(jest.fn());

await analytics.addDataAndSend({
args: argsFrom({}),
});

expect(requestSpy).toBeCalledTimes(1);
expect(requestSpy.mock.calls[0][0]).not.toHaveProperty(
'headers.authorization',
);
});

it('ignores analytics request failures', async () => {
const requestSpy = jest.spyOn(request, 'makeRequest');
requestSpy.mockRejectedValue(new Error('this should be ignored'));

const result = analytics.addDataAndSend({
args: argsFrom({}),
});

await expect(result).resolves.toBeUndefined();
});
});