Skip to content

Commit f1e9e67

Browse files
authored
feat: add new user facing python errors (#290)
chore: pull in latest catalog errors fix: correct unit tess fix: correct unit tess chore: correct unit test chore: correct unit test
1 parent 6d441fc commit f1e9e67

5 files changed

Lines changed: 50 additions & 70 deletions

File tree

lib/dependencies/inspect-implementation.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,7 @@ import * as subProcess from './sub-process';
66
import { DepGraph } from '@snyk/dep-graph';
77
import { buildDepGraph, PartialDepTree } from './build-dep-graph';
88
import { FILENAMES } from '../types';
9-
import {
10-
EmptyManifestError,
11-
FailedToWriteTempFiles,
12-
RequiredPackagesMissingError,
13-
UnparsableRequirementError,
14-
} from '../errors';
9+
import { OpenSourceEcosystems } from '@snyk/error-catalog-nodejs-public';
1510

1611
export function parseJsonWithContaminationFiltering(
1712
rawOutput: string
@@ -276,10 +271,11 @@ export async function inspectInstalledDeps(
276271
});
277272
dumpAllFilesInTempDir(tempDirObj.name);
278273
} catch (e) {
279-
throw new FailedToWriteTempFiles(
274+
throw new OpenSourceEcosystems.PythonFailedToWriteTempFilesError(
280275
`Failed to write temporary files:\n` +
281276
`${e}\n` +
282-
`Try running again with SNYK_TMP_PATH=<some directory>, where <some directory> is a valid directory that you have permissions to write to.`
277+
`Try running again with SNYK_TMP_PATH=<some directory>, where <some directory> is a valid directory that you have permissions to write to.`,
278+
{ e }
283279
);
284280
}
285281

@@ -314,7 +310,9 @@ export async function inspectInstalledDeps(
314310
const noDependenciesDetected = error.includes(emptyManifestMsg);
315311

316312
if (noDependenciesDetected) {
317-
throw new EmptyManifestError(emptyManifestMsg);
313+
throw new OpenSourceEcosystems.EmptyManifestError(emptyManifestMsg, {
314+
error,
315+
});
318316
}
319317

320318
if (error.indexOf('Required packages missing') !== -1) {
@@ -329,12 +327,19 @@ export async function inspectInstalledDeps(
329327
errMsg += '\nPlease run `pip install -r ' + targetFile + '`.';
330328
}
331329
errMsg += ' If the issue persists try again with --skip-unresolved.';
332-
333-
throw new RequiredPackagesMissingError(errMsg);
330+
throw new OpenSourceEcosystems.PythonRequiredPackagesMissingError(
331+
errMsg,
332+
{ error }
333+
);
334334
}
335335

336336
if (error.indexOf('Unparsable requirement line') !== -1) {
337-
throw new UnparsableRequirementError(error);
337+
throw new OpenSourceEcosystems.UnparseableManifestError(
338+
'Unparsable requirement',
339+
{
340+
error,
341+
}
342+
);
338343
}
339344
}
340345

lib/errors.ts

Lines changed: 0 additions & 34 deletions
This file was deleted.

lib/index.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,3 @@ export {
22
getDependencies as inspect,
33
PythonInspectOptions,
44
} from './dependencies';
5-
6-
export {
7-
EmptyManifestError,
8-
RequiredPackagesMissingError,
9-
PythonPluginErrorNames,
10-
} from './errors';

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"dependencies": {
2727
"@snyk/cli-interface": "^2.11.2",
2828
"@snyk/dep-graph": "^1.28.1",
29+
"@snyk/error-catalog-nodejs-public": "^5.77.0",
2930
"shescape": "2.1.6",
3031
"snyk-poetry-lockfile-parser": "^1.9.1",
3132
"tmp": "0.2.3"

test/system/inspect.spec.ts

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import {
2-
EmptyManifestError,
3-
inspect,
4-
RequiredPackagesMissingError,
5-
} from '../../lib';
1+
import { inspect } from '../../lib';
62
import * as testUtils from '../test-utils';
73
import { chdirWorkspaces, ensureVirtualenv } from '../test-utils';
84
import * as depGraphLib from '@snyk/dep-graph';
@@ -12,6 +8,7 @@ import * as subProcess from '../../lib/dependencies/sub-process';
128
import { SpawnSyncReturns } from 'child_process';
139
import * as fs from 'fs';
1410
import * as path from 'path';
11+
import { OpenSourceEcosystems } from '@snyk/error-catalog-nodejs-public';
1512

1613
// Usually the setup of virtual environments can run for a while
1714
jest.setTimeout(180000);
@@ -741,7 +738,7 @@ describe('inspect', () => {
741738

742739
await expect(
743740
async () => await inspect('.', FILENAMES.pip.manifest)
744-
).rejects.toThrow('Required packages missing: markupsafe');
741+
).rejects.toThrow('Missing required packages');
745742
});
746743

747744
it('should fail on nonexistent referenced local depedency', async () => {
@@ -751,7 +748,7 @@ describe('inspect', () => {
751748
tearDown = testUtils.activateVirtualenv(workspace);
752749

753750
await expect(inspect('.', FILENAMES.pip.manifest)).rejects.toThrow(
754-
'Unparsable requirement line (Requirement line ./lib/nonexistent is a local path, but could not be parsed)'
751+
'Unable to parse manifest file'
755752
);
756753
});
757754

@@ -1101,7 +1098,7 @@ describe('inspect', () => {
11011098
testUtils.chdirWorkspaces('pipfile-empty');
11021099
await expect(
11031100
async () => await inspect('.', FILENAMES.pipenv.manifest)
1104-
).rejects.toThrow('No dependencies detected in manifest');
1101+
).rejects.toThrow('Empty manifest file');
11051102
});
11061103
});
11071104

@@ -1227,31 +1224,48 @@ describe('inspect', () => {
12271224
});
12281225

12291226
describe('manifest file is empty', () => {
1230-
it('should throw EmptyManifestError', async () => {
1227+
it('should throw PythonEmptyManifestError', async () => {
12311228
mockedExecute.mockResolvedValueOnce('Python 3.9.5');
12321229
mockedExecute.mockRejectedValueOnce(
12331230
'No dependencies detected in manifest.'
12341231
);
12351232
const manifestFilePath = 'path/to/requirements.txt';
12361233

1237-
await expect(inspect('.', manifestFilePath)).rejects.toThrowError(
1238-
new EmptyManifestError('No dependencies detected in manifest.')
1239-
);
1234+
const err = await inspect('.', manifestFilePath).catch((e) => e);
1235+
1236+
expect(err).toBeInstanceOf(OpenSourceEcosystems.EmptyManifestError);
1237+
expect(err).toMatchObject({
1238+
message: 'Empty manifest file',
1239+
detail: 'No dependencies detected in manifest.',
1240+
metadata: {
1241+
errorCode: 'SNYK-OS-0012',
1242+
},
1243+
isErrorCatalogError: true,
1244+
});
12401245
});
12411246
});
12421247

12431248
describe('required packages were not installed', () => {
1244-
it('should throw RequiredPackagesMissingError', async () => {
1249+
it('should throw PythonRequiredPackagesMissingError', async () => {
12451250
mockedExecute.mockResolvedValueOnce('Python 3.9.5');
12461251
mockedExecute.mockRejectedValueOnce('Required packages missing');
12471252
const manifestFilePath = 'path/to/requirements.txt';
12481253

1249-
await expect(inspect('.', manifestFilePath)).rejects.toThrowError(
1250-
new RequiredPackagesMissingError(
1251-
'Required packages missing\n' +
1252-
'Please run `pip install -r path/to/requirements.txt`. If the issue persists try again with --skip-unresolved.'
1253-
)
1254+
const err = await inspect('.', manifestFilePath).catch((e) => e);
1255+
1256+
expect(err).toBeInstanceOf(
1257+
OpenSourceEcosystems.PythonRequiredPackagesMissingError
12541258
);
1259+
expect(err).toMatchObject({
1260+
message: 'Missing required packages',
1261+
detail:
1262+
'Required packages missing\n' +
1263+
'Please run `pip install -r path/to/requirements.txt`. If the issue persists try again with --skip-unresolved.',
1264+
metadata: {
1265+
errorCode: 'SNYK-OS-PYTHON-0013',
1266+
},
1267+
isErrorCatalogError: true,
1268+
});
12551269
});
12561270
});
12571271
});

0 commit comments

Comments
 (0)