Skip to content

Commit dd71612

Browse files
authored
fix(publish:tarballs): Fix unhandled exception when publish fails (#23953)
1 parent fbafb3a commit dd71612

File tree

1 file changed

+26
-9
lines changed
  • build-tools/packages/build-cli/src/commands/publish

1 file changed

+26
-9
lines changed

build-tools/packages/build-cli/src/commands/publish/tarballs.ts

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ async function publishTarball(
207207
}
208208
} catch (error) {
209209
// Assume package or version is not published, so just continue and try to publish
210-
log.verbose(`Caught error: ${error}`);
210+
log.verbose(`Version appears unpublished; expected error: ${error}`);
211211
}
212212

213213
const args = ["publish", tarball.fileName, "--access", "public"];
@@ -216,15 +216,32 @@ async function publishTarball(
216216
}
217217
const tarballDirectory = path.dirname(tarball.filePath);
218218
log.verbose(`Executing publish command in ${tarballDirectory}: pnpm ${args.join(" ")}`);
219-
const publishOutput = await execa("npm", args, {
220-
cwd: tarballDirectory,
221-
});
222-
223-
if (publishOutput.exitCode !== 0) {
224-
log.warning(`Failed to publish ${tarball.name}`);
225-
log.verbose(publishOutput.stderr);
226-
return "Error";
219+
try {
220+
const publishOutput = await execa("npm", args, {
221+
cwd: tarballDirectory,
222+
});
223+
224+
if (publishOutput.exitCode !== 0) {
225+
return handlePublishError(log, tarball.name, publishOutput.stderr);
226+
}
227+
} catch (error) {
228+
const err = error as Error;
229+
return handlePublishError(log, tarball.name, err.message, err.stack);
227230
}
228231

229232
return "SuccessfullyPublished";
230233
}
234+
235+
function handlePublishError(
236+
log: Logger,
237+
name: string,
238+
message: string,
239+
stack?: string,
240+
): "Error" {
241+
log.warning(`Failed to publish '${name}'`);
242+
log.verbose(message);
243+
if (stack !== undefined) {
244+
log.verbose(stack);
245+
}
246+
return "Error";
247+
}

0 commit comments

Comments
 (0)