chore(broccoli): improve `overwrite` error in merge-trees

Modified the error message to include the relative duplicate path,
to help in diagnosing the cause of the error message.

Closes #2521
This commit is contained in:
Caitlin Potter 2015-06-16 11:24:29 -05:00
parent 2d499de2bd
commit 37fceda7e8
2 changed files with 14 additions and 5 deletions

View File

@ -55,7 +55,9 @@ describe('MergeTrees', () => {
let treeDiffer = MakeTreeDiffers(['tree1', 'tree2', 'tree3']);
let treeMerger = mergeTrees(['tree1', 'tree2', 'tree3'], 'dest', {});
expect(() => treeMerger.rebuild(treeDiffer.diffTrees()))
.toThrowError("`overwrite` option is required for handling duplicates.");
.toThrowError(
'Duplicate path found while merging trees. Path: "foo.js".\n' +
'Either remove the duplicate or enable the "overwrite" option for this merge.');
testDir = {
'tree1': {'foo.js': mockfs.file({content: 'tree1/foo.js content', mtime: new Date(1000)})},
@ -82,6 +84,8 @@ describe('MergeTrees', () => {
testDir.tree2['foo.js'] = mockfs.file({content: 'tree2/foo.js content', mtime: new Date(1000)});
mockfs(testDir);
expect(() => treeMerger.rebuild(treeDiffer.diffTrees()))
.toThrowError("`overwrite` option is required for handling duplicates.");
.toThrowError(
'Duplicate path found while merging trees. Path: "foo.js".\n' +
'Either remove the duplicate or enable the "overwrite" option for this merge.');
});
});

View File

@ -16,6 +16,11 @@ function outputFileSync(sourcePath, destPath) {
symlinkOrCopySync(sourcePath, destPath);
}
function pathOverwrittenError(path) {
const msg = 'Either remove the duplicate or enable the "overwrite" option for this merge.';
return new Error(`Duplicate path found while merging trees. Path: "${path}".\n${msg}`);
}
export class MergeTrees implements DiffingBroccoliPlugin {
private pathCache: {[key: string]: number[]} = Object.create(null);
public options: MergeTreesOptions;
@ -59,7 +64,7 @@ export class MergeTrees implements DiffingBroccoliPlugin {
// ASSERT(contains(pathsToEmit, changedPath));
cache.unshift(index);
} else {
throw new Error("`overwrite` option is required for handling duplicates.");
throw pathOverwrittenError(changedPath);
}
});
});
@ -79,7 +84,7 @@ export class MergeTrees implements DiffingBroccoliPlugin {
this.pathCache[removedPath] = undefined;
} else if (!emitted[removedPath]) {
if (cache.length === 1 && !overwrite) {
throw new Error("`overwrite` option is required for handling duplicates.");
throw pathOverwrittenError(removedPath);
}
emit(removedPath);
}
@ -102,7 +107,7 @@ export class MergeTrees implements DiffingBroccoliPlugin {
cache.push(index);
cache.sort((a, b) => a - b);
if (cache.length > 1 && !overwrite) {
throw new Error("`overwrite` option is required for handling duplicates.");
throw pathOverwrittenError(changedPath);
}
if (cache[cache.length - 1] === index && !emitted[changedPath]) {
emit(changedPath);