fix(cli): review feedback for --from-bundle error handling

- wrap the bundle JSON parses in real try/catch so a corrupt build.json
  or trigger-build-args.json surfaces the intended error message instead
  of a raw SyntaxError (the eager JSON.parse threw before tryCatch could
  see it)
- upsert the preview branch on fresh-init from-bundle deploys, matching
  the main deploy path (attach mode already has the branch env)
This commit is contained in:
Saadi Myftija
2026-07-22 11:42:31 +02:00
parent c564578090
commit 332549f6db
+25 -4
View File
@@ -1895,7 +1895,14 @@ async function handleFromBundleDeploy({
);
}
const manifestResult = BuildManifest.safeParse(JSON.parse(manifestRaw));
let manifestJson: unknown;
try {
manifestJson = JSON.parse(manifestRaw);
} catch {
throw new Error(`Invalid build.json in the bundle directory: not valid JSON`);
}
const manifestResult = BuildManifest.safeParse(manifestJson);
if (!manifestResult.success) {
throw new Error(`Invalid build.json in the bundle directory: ${manifestResult.error.message}`);
@@ -1911,11 +1918,13 @@ async function handleFromBundleDeploy({
);
if (!buildArgsError) {
const [parseError, parsed] = await tryCatch(Promise.resolve(JSON.parse(buildArgsRaw)));
if (parseError) {
let parsed: { env?: Record<string, string> };
try {
parsed = JSON.parse(buildArgsRaw);
} catch {
throw new Error(`Invalid ${BUNDLE_BUILD_ARGS_FILE} in the bundle directory`);
}
buildEnvVars = parsed.env ?? {};
buildEnvVars = (typeof parsed === "object" && parsed !== null ? parsed.env : undefined) ?? {};
} else if (bundleManifest.build.env && Object.keys(bundleManifest.build.env).length > 0) {
// The scrubbed manifest can't carry values, but if a manifest somehow has them, use them.
buildEnvVars = bundleManifest.build.env;
@@ -1931,6 +1940,18 @@ async function handleFromBundleDeploy({
);
}
// In attach mode the branch env already exists (it was created by whatever
// initialized the deployment); a fresh-init preview deploy needs the upsert.
if (options.env === "preview" && branch && !existingDeploymentId) {
await upsertBranch({
accessToken: auth.accessToken,
apiUrl: auth.apiUrl,
projectRef,
branch,
gitMeta: undefined,
});
}
const projectClient = await getProjectClient({
accessToken: auth.accessToken,
apiUrl: auth.apiUrl,