Skip to content

Commit

Permalink
feat: add code client detail error [NEBULA-1511] (#4922)
Browse files Browse the repository at this point in the history
* feat: add code client detail error

* feat: update code client
  • Loading branch information
nick-watson committed Nov 14, 2023
1 parent 343c962 commit 1cb1fe5
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 20 deletions.
30 changes: 15 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"@sentry/node": "^7.34.0",
"@snyk/cli-interface": "2.12.0",
"@snyk/cloud-config-parser": "^1.14.5",
"@snyk/code-client": "^4.22.3",
"@snyk/code-client": "^4.23.0",
"@snyk/dep-graph": "^2.7.4",
"@snyk/docker-registry-v2-client": "^2.10.0",
"@snyk/fix": "file:packages/snyk-fix",
Expand Down
11 changes: 10 additions & 1 deletion src/lib/plugins/sast/errors/code-client-error.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import { CustomError } from '../../../errors/custom-error';

const messagePrefix = 'There was a problem running Code analysis';

export class CodeClientError extends CustomError {
constructor(statusCode: number, statusText: string, additionalUserHelp = '') {
super(statusText);
this.code = statusCode;
this.userMessage = `${messagePrefix}. ${additionalUserHelp}\nContact support if the problem persists.`;
}
}

this.userMessage = `There was a problem running Code analysis. ${additionalUserHelp}\nContact support if the problem persists.`;
export class CodeClientErrorWithDetail extends CustomError {
constructor(message: string, statusCode: number, detail: string) {
super(message);
this.code = statusCode;
this.userMessage = `${messagePrefix}. ${detail}.`;
}
}
5 changes: 4 additions & 1 deletion src/lib/plugins/sast/errors/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export { MissingConfigurationError } from './missing-configuration-error';
export { FeatureNotSupportedBySnykCodeError } from './unsupported-feature-snyk-code-error';
export { CodeClientError } from './code-client-error';
export {
CodeClientError,
CodeClientErrorWithDetail,
} from './code-client-error';
23 changes: 21 additions & 2 deletions src/lib/plugins/sast/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from './format/output-format';
import { EcosystemPlugin } from '../../ecosystems/types';
import { FailedToRunTestError, NoSupportedSastFiles } from '../../errors';
import { CodeClientError } from './errors';
import { CodeClientErrorWithDetail, CodeClientError } from './errors';
import { filterIgnoredIssues } from './utils';
import { jsonStringifyLargeObject } from '../../json';
import * as analytics from '../../analytics';
Expand Down Expand Up @@ -111,7 +111,9 @@ export const codePlugin: EcosystemPlugin = {
return sarifResult ? { readableResult, sarifResult } : { readableResult };
} catch (error) {
let err: Error;
if (isCodeClientError(error)) {
if (isCodeClientErrorWithDetail(error)) {
err = resolveCodeClientErrorWithDetail(error);
} else if (isCodeClientError(error)) {
err = resolveCodeClientError(error);
} else if (error instanceof Error) {
err = error;
Expand Down Expand Up @@ -180,6 +182,23 @@ function resolveCodeClientError(error: {
);
}

function resolveCodeClientErrorWithDetail(error: any) {
return new CodeClientErrorWithDetail(
error.statusText,
error.statusCode,
error.detail,
);
}

function isCodeClientErrorWithDetail(error: any): boolean {
return (
error.hasOwnProperty('statusCode') &&
error.hasOwnProperty('statusText') &&
error.hasOwnProperty('detail') &&
error.hasOwnProperty('apiName')
);
}

function isUnauthorizedError(error: any): boolean {
return (
error.statusCode === 401 ||
Expand Down
31 changes: 31 additions & 0 deletions test/jest/unit/snyk-code/snyk-code-test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,37 @@ describe('Test snyk code', () => {
).rejects.toHaveProperty('message', "Failed to run 'code test'");
});

it('When code-client fails with details, show message with failure details', async () => {
const codeClientErrorWithDetail = {
apiName: 'getAnalysis',
statusCode: 422,
statusText: 'Analysis failed',
detail: 'Analysis failed, more info: https://snyk.io',
};

jest
.spyOn(analysis, 'getCodeTestResults')
.mockRejectedValue(codeClientErrorWithDetail);

isSastEnabledForOrgSpy.mockResolvedValueOnce({
sastEnabled: true,
localCodeEngine: {
enabled: false,
},
});
trackUsageSpy.mockResolvedValue({});

await expect(
ecosystems.testEcosystem('code', ['.'], {
path: '',
code: true,
}),
).rejects.toHaveProperty(
'userMessage',
'There was a problem running Code analysis. Analysis failed, more info: https://snyk.io.',
);
});

it('analyzeFolders should be called with the right arguments', async () => {
const baseURL = expect.any(String);
const sessionToken = `token ${fakeApiKey}`;
Expand Down

0 comments on commit 1cb1fe5

Please sign in to comment.