Skip to content

Commit

Permalink
Merge pull request #5630 from snyk/fix/improved-error-messages-monito…
Browse files Browse the repository at this point in the history
…r-command

fix: error catalog messages for monitor commands
  • Loading branch information
PeterSchafer authored Dec 18, 2024
2 parents d4e3024 + 4e58601 commit eeb3ee0
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 43 deletions.
38 changes: 13 additions & 25 deletions src/lib/monitor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ import {
} from './utils';
import { countPathsToGraphRoot } from '../utils';
import { PackageExpanded } from 'snyk-resolve-deps/dist/types';
import { headerSnykTsCliTerminate } from '../request/constants';
import { EXIT_CODES } from '../../cli/exit-codes';

const debug = Debug('snyk');

Expand Down Expand Up @@ -213,7 +215,7 @@ async function monitorDepTree(
);
}

const { res, body } = await makeRequest({
return await monitorRequest({
body: {
meta: {
method: meta.method,
Expand Down Expand Up @@ -264,17 +266,6 @@ async function monitorDepTree(
url: config.API + '/monitor/' + packageManager,
json: true,
});

if (res.statusCode && res.statusCode >= 200 && res.statusCode <= 299) {
return body as MonitorResult;
} else {
const userMessage = body && body.userMessage;
if (!userMessage && res.statusCode === 504) {
throw new ConnectionTimeoutError();
} else {
throw new MonitorError(res.statusCode, userMessage);
}
}
}

export async function monitorDepGraph(
Expand Down Expand Up @@ -336,7 +327,7 @@ export async function monitorDepGraph(
);
}

const { res, body } = await makeRequest({
return await monitorRequest({
body: {
meta: {
method: meta.method,
Expand Down Expand Up @@ -379,17 +370,6 @@ export async function monitorDepGraph(
url: `${config.API}/monitor/${packageManager}/graph`,
json: true,
});

if (res.statusCode && res.statusCode >= 200 && res.statusCode <= 299) {
return body as MonitorResult;
} else {
const userMessage = body && body.userMessage;
if (!userMessage && res.statusCode === 504) {
throw new ConnectionTimeoutError();
} else {
throw new MonitorError(res.statusCode, userMessage);
}
}
}

async function monitorDepGraphFromDepTree(
Expand Down Expand Up @@ -465,7 +445,8 @@ async function monitorDepGraphFromDepTree(
errorMessageWithRetry('Your monitor request could not be completed.'),
);
}
const { res, body } = await makeRequest({

return await monitorRequest({
body: {
meta: {
method: meta.method,
Expand Down Expand Up @@ -511,6 +492,13 @@ async function monitorDepGraphFromDepTree(
url: `${config.API}/monitor/${packageManager}/graph`,
json: true,
});
}

async function monitorRequest(payload: any): Promise<MonitorResult> {
const { res, body } = await makeRequest(payload);
if (res.headers[headerSnykTsCliTerminate] == 'true') {
process.exit(EXIT_CODES.EX_TERMINATE);
}

if (res.statusCode && res.statusCode >= 200 && res.statusCode <= 299) {
return body as MonitorResult;
Expand Down
1 change: 1 addition & 0 deletions src/lib/request/constants.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const headerSnykAuthFailed = 'snyk-auth-failed';
export const headerSnykTsCliTerminate = 'snyk-terminate';
7 changes: 6 additions & 1 deletion src/lib/request/promise.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { EXIT_CODES } from '../../cli/exit-codes';
import { getAuthHeader } from '../api-token';
import { MissingApiTokenError } from '../errors';
import { headerSnykAuthFailed } from './constants';
import { headerSnykAuthFailed, headerSnykTsCliTerminate } from './constants';
import * as request from './index';

export async function makeRequest<T>(payload: any): Promise<T> {
return new Promise((resolve, reject) => {
request.makeRequest(payload, (error, res, body) => {
if (res.headers[headerSnykTsCliTerminate] == 'true') {
process.exit(EXIT_CODES.EX_TERMINATE);
}

if (error) {
return reject(error);
}
Expand Down
3 changes: 2 additions & 1 deletion src/lib/snyk-test/run-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ import {
import { PackageExpanded } from 'snyk-resolve-deps/dist/types';
import { normalizeTargetFile } from '../normalize-target-file';
import { EXIT_CODES } from '../../cli/exit-codes';
import { headerSnykTsCliTerminate } from '../request/constants';

const debug = debugModule('snyk:run-test');

Expand Down Expand Up @@ -533,7 +534,7 @@ function sendTestPayload(
return reject(error);
}

if (res?.headers?.['snyk-terminate']) {
if (res?.headers?.[headerSnykTsCliTerminate]) {
process.exit(EXIT_CODES.EX_TERMINATE);
}

Expand Down
40 changes: 24 additions & 16 deletions test/jest/acceptance/error-catalog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { runSnykCLI } from '../util/runSnykCLI';
import { fakeServer, getFirstIPv4Address } from '../../acceptance/fake-server';
import { getServerPort } from '../util/getServerPort';

const TEST_DISTROLESS_STATIC_IMAGE =
'gcr.io/distroless/static@sha256:7198a357ff3a8ef750b041324873960cf2153c11cc50abb9d8d5f8bb089f6b4e';

interface Workflow {
type: string;
cmd: string;
Expand All @@ -16,13 +19,21 @@ const integrationWorkflows: Workflow[] = [
type: 'golang/native',
cmd: 'code test',
},
{
type: 'typescript',
cmd: 'monitor',
},
{
type: 'typescript',
cmd: `container monitor ${TEST_DISTROLESS_STATIC_IMAGE}`,
},
];

describe.each(integrationWorkflows)(
'outputs Error Catalog errors',
({ cmd, type }) => {
const snykOrg = '11111111-2222-3333-4444-555555555555';
let env: any = {
let env: { [key: string]: string | undefined } = {
...process.env,
};

Expand Down Expand Up @@ -75,18 +86,17 @@ describe.each(integrationWorkflows)(
it(`snyk ${cmd}`, async () => {
server.setStatusCode(500);
const { code } = await runSnykCLI(`${cmd}`, { env });
expect(code).toBe(2);
const analyticsRequest = server
.getRequests()
.filter((value) =>
(value.url as string).includes(
`/api/hidden/orgs/${snykOrg}/analytics`,
),
value.url.includes(`/api/hidden/orgs/${snykOrg}/analytics`),
)
.pop();
expect(
analyticsRequest?.body.data.attributes.interaction.errors[0].code,
).toEqual('500');
const errors =
analyticsRequest?.body.data.attributes.interaction.errors;

expect(code).toBe(2);
expect(errors[0].code).toEqual('500');
});
});
});
Expand All @@ -96,19 +106,17 @@ describe.each(integrationWorkflows)(
it(`snyk ${cmd}`, async () => {
server.setStatusCode(400);
const { code } = await runSnykCLI(`${cmd}`, { env });
expect(code).toBe(2);

const analyticsRequest = server
.getRequests()
.filter((value) =>
(value.url as string).includes(
`/api/hidden/orgs/${snykOrg}/analytics`,
),
value.url.includes(`/api/hidden/orgs/${snykOrg}/analytics`),
)
.pop();
expect(
analyticsRequest?.body.data.attributes.interaction.errors[0].code,
).toEqual('400');
const errors =
analyticsRequest?.body.data.attributes.interaction.errors;

expect(code).toBe(2);
expect(errors[0].code).toEqual('400');
});
});
});
Expand Down

0 comments on commit eeb3ee0

Please sign in to comment.