Skip to content

Commit 9a6681d

Browse files
committed
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)
1 parent 864dba0 commit 9a6681d

1 file changed

Lines changed: 24 additions & 4 deletions

File tree

packages/cli-v3/src/commands/deploy.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,7 +1708,14 @@ async function handleFromBundleDeploy({
17081708
);
17091709
}
17101710

1711-
const manifestResult = BuildManifest.safeParse(JSON.parse(manifestRaw));
1711+
let manifestJson: unknown;
1712+
try {
1713+
manifestJson = JSON.parse(manifestRaw);
1714+
} catch {
1715+
throw new Error(`Invalid build.json in the bundle directory: not valid JSON`);
1716+
}
1717+
1718+
const manifestResult = BuildManifest.safeParse(manifestJson);
17121719

17131720
if (!manifestResult.success) {
17141721
throw new Error(`Invalid build.json in the bundle directory: ${manifestResult.error.message}`);
@@ -1724,11 +1731,13 @@ async function handleFromBundleDeploy({
17241731
);
17251732

17261733
if (!buildArgsError) {
1727-
const [parseError, parsed] = await tryCatch(Promise.resolve(JSON.parse(buildArgsRaw)));
1728-
if (parseError) {
1734+
let parsed: { env?: Record<string, string> };
1735+
try {
1736+
parsed = JSON.parse(buildArgsRaw);
1737+
} catch {
17291738
throw new Error(`Invalid ${BUNDLE_BUILD_ARGS_FILE} in the bundle directory`);
17301739
}
1731-
buildEnvVars = parsed.env ?? {};
1740+
buildEnvVars = (typeof parsed === "object" && parsed !== null ? parsed.env : undefined) ?? {};
17321741
} else if (bundleManifest.build.env && Object.keys(bundleManifest.build.env).length > 0) {
17331742
// The scrubbed manifest can't carry values, but if a manifest somehow has them, use them.
17341743
buildEnvVars = bundleManifest.build.env;
@@ -1744,6 +1753,17 @@ async function handleFromBundleDeploy({
17441753
);
17451754
}
17461755

1756+
// In attach mode the branch env already exists (it was created by whatever
1757+
// initialized the deployment); a fresh-init preview deploy needs the upsert.
1758+
if (options.env === "preview" && branch && !existingDeploymentId) {
1759+
await upsertBranch({
1760+
accessToken: auth.accessToken,
1761+
apiUrl: auth.apiUrl,
1762+
projectRef,
1763+
branch,
1764+
});
1765+
}
1766+
17471767
const projectClient = await getProjectClient({
17481768
accessToken: auth.accessToken,
17491769
apiUrl: auth.apiUrl,

0 commit comments

Comments
 (0)