Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
🎨 Better error handling when validating license key
Luke Vella049fb15931b0
diff --git a/apps/web/public/locales/en/app.json b/apps/web/public/locales/en/app.jsonindex 5d97cdc0b..2abb4d9f6 100644--- a/apps/web/public/locales/en/app.json+++ b/apps/web/public/locales/en/app.json@@ -382,5 +382,8 @@"changeRole": "Change role","licenseType": "License Type","removeLicense": "Remove License",- "removeLicenseDescription": "Are you sure you want to remove this license?"+ "removeLicenseDescription": "Are you sure you want to remove this license?",+ "licenseKeyErrorRateLimitExceeded": "Rate limit exceeded",+ "licenseKeyErrorInvalidLicenseKey": "Invalid license key",+ "licenseKeyGenericError": "An error occurred while validating the license key"}diff --git a/apps/web/src/features/licensing/actions/validate-license.ts b/apps/web/src/features/licensing/actions/validate-license.tsindex 70d4634aa..3c3ce5b0c 100644--- a/apps/web/src/features/licensing/actions/validate-license.ts+++ b/apps/web/src/features/licensing/actions/validate-license.ts@@ -8,20 +8,20 @@ export async function validateLicenseKey(key: string) {const { success } = await rateLimit("validate_license_key", 10, "1 m");if (!success) {- throw new Error("Rate limit exceeded");+ return {+ valid: false,+ error: "Rate limit exceeded" as const,+ };}- const { data, error } = await licensingClient.validateLicenseKey({+ const { data } = await licensingClient.validateLicenseKey({key,});- if (error) {- throw new Error(`License validation failed: ${error}`);- }-if (!data) {return {valid: false,+ error: "Invalid license key" as const,};}@@ -39,5 +39,6 @@ export async function validateLicenseKey(key: string) {return {valid: true,+ error: null,};}diff --git a/apps/web/src/features/licensing/components/license-key-form.tsx b/apps/web/src/features/licensing/components/license-key-form.tsxindex 444fee29c..b3ce95348 100644--- a/apps/web/src/features/licensing/components/license-key-form.tsx+++ b/apps/web/src/features/licensing/components/license-key-form.tsx@@ -39,17 +39,40 @@ export function LicenseKeyForm() {const onSubmit = async (data: LicenseKeyFormValues) => {try {- const { valid } = await validateLicenseKey(data.licenseKey);+ const { error } = await validateLicenseKey(data.licenseKey);- if (!valid) {- form.setError("licenseKey", {- message: "Invalid license key",- });+ if (error) {+ switch (error) {+ case "Rate limit exceeded":+ form.setError("licenseKey", {+ message: t("licenseKeyErrorRateLimitExceeded", {+ defaultValue: "Rate limit exceeded",+ }),+ });+ break;+ case "Invalid license key":+ form.setError("licenseKey", {+ message: t("licenseKeyErrorInvalidLicenseKey", {+ defaultValue: "Invalid license key",+ }),+ });+ break;+ default:+ form.setError("licenseKey", {+ message: t("licenseKeyGenericError", {+ defaultValue:+ "An error occurred while validating the license key",+ }),+ });+ break;+ }return;}} catch (error) {form.setError("licenseKey", {- message: "An error occurred while validating the license key",+ message: t("licenseKeyGenericError", {+ defaultValue: "An error occurred while validating the license key",+ }),});}router.refresh();diff --git a/apps/web/src/features/licensing/lib/licensing-client.ts b/apps/web/src/features/licensing/lib/licensing-client.tsindex ee51b31dd..76f15cb23 100644--- a/apps/web/src/features/licensing/lib/licensing-client.ts+++ b/apps/web/src/features/licensing/lib/licensing-client.ts@@ -47,10 +47,6 @@ export class LicensingClient {body: JSON.stringify(input),});- if (!res.ok) {- throw new Error("Failed to validate license key.");- }-return validateLicenseKeyResponseSchema.parse(await res.json());}}