Skip to content

Commit

Permalink
Merge pull request #2346 from snyk/fix/ignore-analytics-failures
Browse files Browse the repository at this point in the history
fix: ignore analytics request failures
  • Loading branch information
Jahed Ahmed authored Nov 8, 2021
2 parents 7dcc609 + e94979f commit 974424d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
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();
});
});

0 comments on commit 974424d

Please sign in to comment.