mirror of
https://github.com/balena-io/etcher.git
synced 2025-04-24 07:17:18 +00:00
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:
parent
60754250d9
commit
df74a2763c
@ -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;
|
||||
});
|
||||
},
|
||||
|
||||
|
BIN
tests/image-stream/data/unrecognized/invalid.dmg
Normal file
BIN
tests/image-stream/data/unrecognized/invalid.dmg
Normal file
Binary file not shown.
@ -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');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
@ -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;
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user