feat(image-stream): Make invalid images user errors (#1369)

This displays a user error if the reading the image causes an error,
instead of letting it fall through and get reported.
This is to avoid reporting errors that are not due to malfunction of the software,
but due to malformatted images.

Change-Type: minor
Changelog-Entry: Display nicer error dialog when reading an invalid image
This commit is contained in:
Jonas Hermsmeier 2017-05-08 18:29:28 +02:00 committed by GitHub
parent 60754250d9
commit df74a2763c
4 changed files with 26 additions and 1 deletions

View File

@ -27,6 +27,8 @@ const udif = Bluebird.promisifyAll(require('udif'));
const archive = require('./archive');
const zipArchiveHooks = require('./archive-hooks/zip');
const fileExtensions = require('../shared/file-extensions');
const path = require('path');
const errors = require('../shared/errors');
/**
* @summary Image handlers
@ -161,6 +163,16 @@ module.exports = {
},
transform: new PassThroughStream()
};
}).catch((error) => {
if (/invalid footer/i.test(error.message)) {
throw errors.createUserError({
title: 'Invalid image',
description: `There was an error reading "${path.basename(file)}". `
+ 'The image does not appear to be a valid Apple Disk Image (dmg), or may have the wrong filename extension.\n\n'
+ `Error: ${error.description || error.message}`
});
}
throw error;
});
},

Binary file not shown.

View File

@ -105,4 +105,14 @@ describe('ImageStream: DMG', function() {
});
context('invalid', function() {
describe('given an invalid dmg file', function() {
tester.expectError(
path.join(DATA_PATH, 'unrecognized', 'invalid.dmg'),
'Invalid image', 'Invalid footer');
});
});
});

View File

@ -43,11 +43,14 @@ const deleteIfExists = (file) => {
});
};
exports.expectError = function(file, errorMessage) {
exports.expectError = function(file, errorMessage, errorDetail) {
it('should be rejected with an error', function() {
return imageStream.getFromFilePath(file).catch((error) => {
m.chai.expect(error).to.be.an.instanceof(Error);
m.chai.expect(error.message).to.equal(errorMessage);
if (errorDetail) {
m.chai.expect(error.description).to.contain(errorDetail);
}
m.chai.expect(error.description).to.be.a.string;
m.chai.expect(error.description.length > 0).to.be.true;
});