From 4e5860124bf6025c123ecb1e0c8d847bfb797394 Mon Sep 17 00:00:00 2001 From: CatalinSnyk Date: Mon, 16 Dec 2024 19:15:33 +0200 Subject: [PATCH] fix: error catalog messages for monitor commands --- src/lib/monitor/index.ts | 38 +++++++------------- src/lib/request/constants.ts | 1 + src/lib/request/promise.ts | 7 +++- src/lib/snyk-test/run-test.ts | 3 +- test/jest/acceptance/error-catalog.spec.ts | 40 +++++++++++++--------- 5 files changed, 46 insertions(+), 43 deletions(-) diff --git a/src/lib/monitor/index.ts b/src/lib/monitor/index.ts index 61eb390beb..d818ef8eee 100644 --- a/src/lib/monitor/index.ts +++ b/src/lib/monitor/index.ts @@ -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'); @@ -213,7 +215,7 @@ async function monitorDepTree( ); } - const { res, body } = await makeRequest({ + return await monitorRequest({ body: { meta: { method: meta.method, @@ -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( @@ -336,7 +327,7 @@ export async function monitorDepGraph( ); } - const { res, body } = await makeRequest({ + return await monitorRequest({ body: { meta: { method: meta.method, @@ -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( @@ -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, @@ -511,6 +492,13 @@ async function monitorDepGraphFromDepTree( url: `${config.API}/monitor/${packageManager}/graph`, json: true, }); +} + +async function monitorRequest(payload: any): Promise { + 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; diff --git a/src/lib/request/constants.ts b/src/lib/request/constants.ts index 04a4521006..c595afb6dd 100644 --- a/src/lib/request/constants.ts +++ b/src/lib/request/constants.ts @@ -1 +1,2 @@ export const headerSnykAuthFailed = 'snyk-auth-failed'; +export const headerSnykTsCliTerminate = 'snyk-terminate'; diff --git a/src/lib/request/promise.ts b/src/lib/request/promise.ts index f15b9b5bdf..cb72131146 100644 --- a/src/lib/request/promise.ts +++ b/src/lib/request/promise.ts @@ -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(payload: any): Promise { 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); } diff --git a/src/lib/snyk-test/run-test.ts b/src/lib/snyk-test/run-test.ts index b23bd77ed2..ea96c7a1b8 100644 --- a/src/lib/snyk-test/run-test.ts +++ b/src/lib/snyk-test/run-test.ts @@ -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'); @@ -533,7 +534,7 @@ function sendTestPayload( return reject(error); } - if (res?.headers?.['snyk-terminate']) { + if (res?.headers?.[headerSnykTsCliTerminate]) { process.exit(EXIT_CODES.EX_TERMINATE); } diff --git a/test/jest/acceptance/error-catalog.spec.ts b/test/jest/acceptance/error-catalog.spec.ts index a0d3ff8933..829399634b 100644 --- a/test/jest/acceptance/error-catalog.spec.ts +++ b/test/jest/acceptance/error-catalog.spec.ts @@ -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; @@ -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, }; @@ -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'); }); }); }); @@ -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'); }); }); });