Skip to content

Commit f6c1263

Browse files
committed
Show error when changing plan fails
1 parent 178d7e8 commit f6c1263

File tree

2 files changed

+43
-47
lines changed

2 files changed

+43
-47
lines changed

apps/webapp/app/routes/resources.orgs.$organizationSlug.select-plan.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ export async function action({ request, params }: ActionFunctionArgs) {
153153
}
154154
}
155155

156-
return setPlan(organization, request, form.callerPath, payload, {
156+
return await setPlan(organization, request, form.callerPath, payload, {
157157
invalidateBillingCache: engine.invalidateBillingCache.bind(engine),
158158
});
159159
}

apps/webapp/app/services/platform.v3.server.ts

Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { MachinePresetName } from "@trigger.dev/core/v3";
1+
import { MachinePresetName, tryCatch } from "@trigger.dev/core/v3";
22
import type { Organization, Project, RuntimeEnvironmentType } from "@trigger.dev/database";
33
import {
44
BillingClient,
@@ -344,62 +344,58 @@ export async function setPlan(
344344
opts?: { invalidateBillingCache?: (orgId: string) => void }
345345
) {
346346
if (!client) {
347-
throw redirectWithErrorMessage(callerPath, request, "Error setting plan");
347+
return redirectWithErrorMessage(callerPath, request, "Error setting plan", {
348+
ephemeral: false,
349+
});
348350
}
349351

350-
try {
351-
const result = await client.setPlan(organization.id, plan);
352+
const [error, result] = await tryCatch(client.setPlan(organization.id, plan));
352353

353-
if (!result) {
354-
throw redirectWithErrorMessage(callerPath, request, "Error setting plan");
355-
}
354+
if (error) {
355+
return redirectWithErrorMessage(callerPath, request, error.message, { ephemeral: false });
356+
}
356357

357-
if (!result.success) {
358-
throw redirectWithErrorMessage(callerPath, request, result.error);
359-
}
358+
if (!result) {
359+
return redirectWithErrorMessage(callerPath, request, "Error setting plan", {
360+
ephemeral: false,
361+
});
362+
}
360363

361-
switch (result.action) {
362-
case "free_connect_required": {
363-
return redirect(result.connectUrl);
364-
}
365-
case "free_connected": {
366-
if (result.accepted) {
367-
// Invalidate billing cache since plan changed
368-
opts?.invalidateBillingCache?.(organization.id);
369-
return redirect(newProjectPath(organization, "You're on the Free plan."));
370-
} else {
371-
return redirectWithErrorMessage(
372-
callerPath,
373-
request,
374-
"Free tier unlock failed, your GitHub account is too new."
375-
);
376-
}
377-
}
378-
case "create_subscription_flow_start": {
379-
return redirect(result.checkoutUrl);
380-
}
381-
case "updated_subscription": {
382-
// Invalidate billing cache since subscription changed
364+
if (!result.success) {
365+
return redirectWithErrorMessage(callerPath, request, result.error, { ephemeral: false });
366+
}
367+
368+
switch (result.action) {
369+
case "free_connect_required": {
370+
return redirect(result.connectUrl);
371+
}
372+
case "free_connected": {
373+
if (result.accepted) {
374+
// Invalidate billing cache since plan changed
383375
opts?.invalidateBillingCache?.(organization.id);
384-
return redirectWithSuccessMessage(
376+
return redirect(newProjectPath(organization, "You're on the Free plan."));
377+
} else {
378+
return redirectWithErrorMessage(
385379
callerPath,
386380
request,
387-
"Subscription updated successfully."
381+
"Free tier unlock failed, your GitHub account is too new.",
382+
{ ephemeral: false }
388383
);
389384
}
390-
case "canceled_subscription": {
391-
// Invalidate billing cache since subscription was canceled
392-
opts?.invalidateBillingCache?.(organization.id);
393-
return redirectWithSuccessMessage(callerPath, request, "Subscription canceled.");
394-
}
395385
}
396-
} catch (e) {
397-
logger.error("Error setting plan", { organizationId: organization.id, error: e });
398-
throw redirectWithErrorMessage(
399-
callerPath,
400-
request,
401-
e instanceof Error ? e.message : "Error setting plan"
402-
);
386+
case "create_subscription_flow_start": {
387+
return redirect(result.checkoutUrl);
388+
}
389+
case "updated_subscription": {
390+
// Invalidate billing cache since subscription changed
391+
opts?.invalidateBillingCache?.(organization.id);
392+
return redirectWithSuccessMessage(callerPath, request, "Subscription updated successfully.");
393+
}
394+
case "canceled_subscription": {
395+
// Invalidate billing cache since subscription was canceled
396+
opts?.invalidateBillingCache?.(organization.id);
397+
return redirectWithSuccessMessage(callerPath, request, "Subscription canceled.");
398+
}
403399
}
404400
}
405401

0 commit comments

Comments
 (0)