Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
Return all overlapping IDs in error
Browse files Browse the repository at this point in the history
  • Loading branch information
haltman-at committed Nov 15, 2022
1 parent b756ba2 commit d7715e8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
9 changes: 5 additions & 4 deletions packages/codec/lib/compilations/utils.ts
Expand Up @@ -689,15 +689,16 @@ export function infoToCompilations(
}
}

export function findRepeatCompilationId(
export function findRepeatCompilationIds(
compilations: Compilation[]
): string | null {
): Set<string> {
let repeats: Set<string> = new Set();
for (let i = 0; i < compilations.length; i++) {
for (let j = i + 1; j < compilations.length; j++) {
if (compilations[i].id === compilations[j].id) {
return compilations[i].id;
repeats.add(compilations[i].id);
}
}
}
return null;
return repeats;
}
8 changes: 4 additions & 4 deletions packages/codec/lib/errors.ts
Expand Up @@ -82,10 +82,10 @@ export class NoProjectInfoError extends Error {
* with the same ID, or a compilation whose ID was already in use.
*/
export class RepeatCompilationIdError extends Error {
public id: string;
constructor(id: string) {
super(`Compilation id ${id} already in use.`);
this.id = id;
public ids: string[];
constructor(ids: string[]) {
super(`Compilation id(s) ${ids.join(", ")} repeated or already in use.`);
this.ids = ids;
this.name = "RepeatCompilationIdError";
}
}
25 changes: 12 additions & 13 deletions packages/decoder/lib/decoders.ts
Expand Up @@ -82,10 +82,10 @@ export class ProjectDecoder {
throw new NoProviderError();
}
//check for repeat compilation IDs
const repeatId =
Codec.Compilations.Utils.findRepeatCompilationId(compilations);
if (repeatId !== null) {
throw new Codec.RepeatCompilationIdError(repeatId);
const repeatIds =
Codec.Compilations.Utils.findRepeatCompilationIds(compilations);
if (repeatIds.size !== 0) {
throw new Codec.RepeatCompilationIdError([...repeatIds]);
}
this.providerAdapter = new ProviderAdapter(provider);
this.compilations = compilations;
Expand Down Expand Up @@ -158,18 +158,17 @@ export class ProjectDecoder {
const existingIds = new Set(
this.compilations.map(compilation => compilation.id)
);
const newIds = new Set(compilations.map(compilation => compilation.id));
//we use a find() rather than a some() so that we can put the ID in the error
const conflictingCompilation = compilations.find(compilation =>
existingIds.has(compilation.id)
);
if (conflictingCompilation !== undefined) {
throw new Codec.RepeatCompilationIdError(conflictingCompilation.id);
const overlappingIds = [...newIds].filter(id => existingIds.has(id));
if (overlappingIds.length !== 0) {
throw new Codec.RepeatCompilationIdError(overlappingIds);
}
//also: check for repeats among the ones we're adding
const repeatId =
Codec.Compilations.Utils.findRepeatCompilationId(compilations);
if (repeatId !== null) {
throw new Codec.RepeatCompilationIdError(repeatId);
const repeatIds =
Codec.Compilations.Utils.findRepeatCompilationIds(compilations);
if (repeatIds.size !== 0) {
throw new Codec.RepeatCompilationIdError([...repeatIds]);
}

//now: checks are over, start adding stuff
Expand Down
6 changes: 3 additions & 3 deletions packages/encoder/lib/encoders.ts
Expand Up @@ -97,11 +97,11 @@ export class ProjectEncoder {
throw new NoInternalInfoError();
}
//check for repeat compilation IDs
const repeatId = Codec.Compilations.Utils.findRepeatCompilationId(
const repeatIds = Codec.Compilations.Utils.findRepeatCompilationIds(
info.compilations
);
if (repeatId !== null) {
throw new Codec.RepeatCompilationIdError(repeatId);
if (repeatIds.size !== 0) {
throw new Codec.RepeatCompilationIdError([...repeatIds]);
}
//since that's good, save it and continue
this.compilations = info.compilations;
Expand Down

0 comments on commit d7715e8

Please sign in to comment.