Skip to content

Commit fa4d000

Browse files
authored
fix(jsii-diff): be nicer about validation errors (#481)
Potentially assembly validation errors throw up a giant error list. Have jsii-diff be more concise about the errors, which will be better for cdk-build-tools.
1 parent 3a79142 commit fa4d000

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

packages/jsii-diff/bin/jsii-diff.ts

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,36 @@ async function main(): Promise<number> {
6363
return 0;
6464
}
6565

66+
// Allow both npm:<package> (legacy) and npm://<package> (looks better)
67+
const NPM_REGEX = /^npm:(\/\/)?/;
68+
6669
async function loadAssembly(name: string) {
67-
if (name.startsWith('npm:')) {
68-
let pkg = name.substring(4);
69-
if (!pkg) { pkg = await loadPackageNameFromAssembly(); }
70-
return await downloadNpmPackage(pkg, loadFromFilesystem);
71-
} else {
72-
return await loadFromFilesystem(name);
70+
try {
71+
if (name.match(NPM_REGEX)) {
72+
let pkg = name.replace(NPM_REGEX, '');
73+
if (!pkg) { pkg = await loadPackageNameFromAssembly(); }
74+
75+
// Put 'pkg' back into 'name' so any errors loading the assembly get a good source description
76+
name = `npm://${pkg}`;
77+
if (pkg.indexOf('@', 1) === -1) { name += '@latest'; }
78+
79+
return await downloadNpmPackage(pkg, loadFromFilesystem);
80+
} else {
81+
return await loadFromFilesystem(name);
82+
}
83+
} catch (e) {
84+
// Prepend information about which assembly we've failed to load
85+
//
86+
// Look at the type of error. If it has a lot of lines (like validation errors
87+
// tend to do) log everything to the debug log and only show a couple
88+
const maxLines = 3;
89+
const messageWithContext = `Error loading assembly '${name}': ${e.message}`;
90+
const errorLines = messageWithContext.split('\n');
91+
if (errorLines.length < maxLines) { throw new Error(messageWithContext); }
92+
for (const line of errorLines) {
93+
LOG.info(line);
94+
}
95+
throw new Error([...errorLines.slice(0, maxLines), '...'].join('\n'));
7396
}
7497
}
7598

0 commit comments

Comments
 (0)