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

feat: improve share results error handling #3070

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 3 additions & 0 deletions src/cli/commands/test/iac-local-execution/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,9 @@ export enum IaCErrorCodes {

// report errors
UnsupportedReportCommandError = 1120,

// share results errors
FailedToShareResults = 1130,
}

export interface TestReturnValue {
Expand Down
31 changes: 22 additions & 9 deletions src/lib/iac/cli-share-results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import config from '../config';
import { makeRequest } from '../request';
import { getAuthHeader } from '../api-token';
import {
IaCErrorCodes,
IacShareResultsFormat,
IaCTestFlags,
ShareResultsOutput,
Expand All @@ -14,10 +15,11 @@ import { Contributor } from '../types';
import * as analytics from '../analytics';
import { getContributors } from '../monitor/dev-count-analysis';
import * as Debug from 'debug';
import { AuthFailedError, ValidationError } from '../errors';
import { AuthFailedError, CustomError, ValidationError } from '../errors';

const debug = Debug('iac-cli-share-results');
import { ProjectAttributes, Tag } from '../types';
import { getErrorStringCode } from '../../cli/commands/test/iac-local-execution/error-utils';

export async function shareResults({
results,
Expand Down Expand Up @@ -47,6 +49,7 @@ export async function shareResults({
}
}
}

const { res, body } = await makeRequest({
method: 'POST',
url: `${config.API}/iac-cli-share-results`,
Expand All @@ -63,15 +66,25 @@ export async function shareResults({
},
});

if (res.statusCode === 401) {
throw AuthFailedError();
if (res.statusCode === 200) {
return { projectPublicIds: body, gitRemoteUrl: gitTarget?.remoteUrl };
} else {
if (res.statusCode === 401) {
throw AuthFailedError();
} else if (res.statusCode === 422 && body.error) {
throw new ValidationError(body.error);
} else {
throw new FailedToShareResults();
}
}
}

if (res.statusCode === 422) {
throw new ValidationError(
res.body.error ?? 'An error occurred, please contact Snyk support',
);
class FailedToShareResults extends CustomError {
constructor(message?: string) {
super(message || 'Failed to share results');
this.code = IaCErrorCodes.FailedToShareResults;
this.strCode = getErrorStringCode(this.code);
this.userMessage =
'An error occurred when trying to share your results, please contact Snyk support (support@snyk.io)';
}

return { projectPublicIds: body, gitRemoteUrl: gitTarget?.remoteUrl };
}
4 changes: 4 additions & 0 deletions test/acceptance/fake-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,10 @@ export const fakeServer = (basePath: string, snykToken: string): FakeServer => {
res.status(200).send({});
});

app.post(basePath + '/iac-cli-share-results', (req, res) => {
res.status(200).send({});
});

app.post(basePath + '/analytics/cli', (req, res) => {
res.status(200).send({});
});
Expand Down
1 change: 1 addition & 0 deletions test/jest/unit/iac/cli-share-results.fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ export const expectedEnvelopeFormatterResults = [
},
];

// if the test that is using this expected output fails for version, look at the package.json policy package version and update it accordingly
export const expectedEnvelopeFormatterResultsWithPolicy = expectedEnvelopeFormatterResults.map(
(result) => {
return {
Expand Down
5 changes: 4 additions & 1 deletion test/jest/unit/iac/cli-share-results.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ describe('CLI Share Results', () => {
beforeEach(() => {
requestSpy = jest
.spyOn(request, 'makeRequest')
.mockImplementation(async () => ({ res: {} as any, body: {} }));
.mockImplementation(async () => ({
res: { statusCode: 200 } as any,
body: {},
}));
envelopeFormattersSpy = jest.spyOn(
envelopeFormatters,
'convertIacResultToScanResult',
Expand Down