|
1 | | -import { MachinePresetName } from "@trigger.dev/core/v3"; |
| 1 | +import { MachinePresetName, tryCatch } from "@trigger.dev/core/v3"; |
2 | 2 | import type { Organization, Project, RuntimeEnvironmentType } from "@trigger.dev/database"; |
3 | 3 | import { |
4 | 4 | BillingClient, |
@@ -344,62 +344,58 @@ export async function setPlan( |
344 | 344 | opts?: { invalidateBillingCache?: (orgId: string) => void } |
345 | 345 | ) { |
346 | 346 | if (!client) { |
347 | | - throw redirectWithErrorMessage(callerPath, request, "Error setting plan"); |
| 347 | + return redirectWithErrorMessage(callerPath, request, "Error setting plan", { |
| 348 | + ephemeral: false, |
| 349 | + }); |
348 | 350 | } |
349 | 351 |
|
350 | | - try { |
351 | | - const result = await client.setPlan(organization.id, plan); |
| 352 | + const [error, result] = await tryCatch(client.setPlan(organization.id, plan)); |
352 | 353 |
|
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 | + } |
356 | 357 |
|
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 | + } |
360 | 363 |
|
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 |
383 | 375 | opts?.invalidateBillingCache?.(organization.id); |
384 | | - return redirectWithSuccessMessage( |
| 376 | + return redirect(newProjectPath(organization, "You're on the Free plan.")); |
| 377 | + } else { |
| 378 | + return redirectWithErrorMessage( |
385 | 379 | callerPath, |
386 | 380 | request, |
387 | | - "Subscription updated successfully." |
| 381 | + "Free tier unlock failed, your GitHub account is too new.", |
| 382 | + { ephemeral: false } |
388 | 383 | ); |
389 | 384 | } |
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 | | - } |
395 | 385 | } |
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 | + } |
403 | 399 | } |
404 | 400 | } |
405 | 401 |
|
|
0 commit comments