Skip to content

Commit 50d0d4f

Browse files
committed
fix: handle common errors from the analyser properly
1 parent 197a0e0 commit 50d0d4f

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

lib/index.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var fs = require('fs');
22
var path = require('path');
33
var subProcess = require('./sub-process');
44
var fetchSnykDockerAnalyzer = require('./fetch-snyk-docker-analyzer');
5+
var debug = require('debug')('snyk');
56

67
module.exports = {
78
inspect: inspect,
@@ -40,6 +41,26 @@ function getMetaData() {
4041
});
4142
}
4243

44+
function handleCommonErrors(error, targetImage) {
45+
if (error.indexOf('command not found') !== -1) {
46+
throw new Error('Snyk docker CLI was not found')
47+
}
48+
if (error.indexOf('Cannot connect to the Docker daemon') !== -1) {
49+
throw new Error('Cannot connect to the Docker daemon. Is the docker'
50+
+ ' daemon running?')
51+
}
52+
if ((error.indexOf('Error loading image from docker engine') !== -1) ||
53+
(error.indexOf('Error performing image analysis') !== -1)) {
54+
throw new Error('Docker image was not found: ' + targetImage)
55+
}
56+
if (error.indexOf('Error getting docker client:') !== -1) {
57+
throw new Error('Failed getting docker client')
58+
}
59+
if (error.indexOf('Error processing image:') !== -1) {
60+
throw new Error('Failed processing image:' + targetImage)
61+
}
62+
}
63+
4364
function getDependencies(analyzerBinaryPath, targetImage) {
4465
return subProcess.execute(
4566
analyzerBinaryPath,
@@ -51,9 +72,8 @@ function getDependencies(analyzerBinaryPath, targetImage) {
5172
})
5273
.catch(function (error) {
5374
if (typeof error === 'string') {
54-
if (error.indexOf('command not found') !== -1) {
55-
throw new Error('Snyk docker CLI wasn\'t found')
56-
}
75+
debug(`Error while running analyser: '${error}'`);
76+
handleCommonErrors(error, targetImage);
5777
errorMsg = error;
5878
errorMatch = /msg="(.*)"/g.exec(errorMsg)
5979
if (errorMatch) {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"nock": "^9.2.3",
2323
"semantic-release": "^6.3.6",
2424
"tap": "^11.1.3",
25+
"debug": "^3.1.0",
2526
"tap-only": "0.0.5"
2627
},
2728
"dependencies": {

test/system.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ test('fetches analyzer only if doesnt exist', function (t) {
5858

5959
test('inspect an image that doesnt exist', function (t) {
6060
return plugin.inspect('not-here:latest').catch((err) => {
61-
t.match(err.message, 'does not exist');
61+
t.match(err.message, 'Docker image was not found:');
6262
t.pass('failed as expected');
6363
})
6464
});

0 commit comments

Comments
 (0)