Skip to content

Commit

Permalink
fix(jsii-diff): be nicer about validation errors (#481)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rix0rrr committed Apr 23, 2019
1 parent 3a79142 commit fa4d000
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions packages/jsii-diff/bin/jsii-diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,36 @@ async function main(): Promise<number> {
return 0;
}

// Allow both npm:<package> (legacy) and npm://<package> (looks better)
const NPM_REGEX = /^npm:(\/\/)?/;

async function loadAssembly(name: string) {
if (name.startsWith('npm:')) {
let pkg = name.substring(4);
if (!pkg) { pkg = await loadPackageNameFromAssembly(); }
return await downloadNpmPackage(pkg, loadFromFilesystem);
} else {
return await loadFromFilesystem(name);
try {
if (name.match(NPM_REGEX)) {
let pkg = name.replace(NPM_REGEX, '');
if (!pkg) { pkg = await loadPackageNameFromAssembly(); }

// Put 'pkg' back into 'name' so any errors loading the assembly get a good source description
name = `npm://${pkg}`;
if (pkg.indexOf('@', 1) === -1) { name += '@latest'; }

return await downloadNpmPackage(pkg, loadFromFilesystem);
} else {
return await loadFromFilesystem(name);
}
} catch (e) {
// Prepend information about which assembly we've failed to load
//
// Look at the type of error. If it has a lot of lines (like validation errors
// tend to do) log everything to the debug log and only show a couple
const maxLines = 3;
const messageWithContext = `Error loading assembly '${name}': ${e.message}`;
const errorLines = messageWithContext.split('\n');
if (errorLines.length < maxLines) { throw new Error(messageWithContext); }
for (const line of errorLines) {
LOG.info(line);
}
throw new Error([...errorLines.slice(0, maxLines), '...'].join('\n'));
}
}

Expand Down

0 comments on commit fa4d000

Please sign in to comment.