From d38d703fd39944a3cf9497c3e45775c9dc523005 Mon Sep 17 00:00:00 2001 From: Catalin Pit Date: Wed, 22 Apr 2026 08:42:07 +0300 Subject: [PATCH] fix: error message (update title) (#2691) --- .../dialogs/template-use-dialog.tsx | 39 ++++++++++++------- .../trpc/server/template-router/router.ts | 4 ++ 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/apps/remix/app/components/dialogs/template-use-dialog.tsx b/apps/remix/app/components/dialogs/template-use-dialog.tsx index 5f5a23955..625c6c272 100644 --- a/apps/remix/app/components/dialogs/template-use-dialog.tsx +++ b/apps/remix/app/components/dialogs/template-use-dialog.tsx @@ -8,6 +8,7 @@ import { DocumentDistributionMethod, DocumentSigningOrder } from '@prisma/client import { FileTextIcon, InfoIcon, Plus, UploadCloudIcon, X } from 'lucide-react'; import { useFieldArray, useForm } from 'react-hook-form'; import { useNavigate } from 'react-router'; +import { match } from 'ts-pattern'; import * as z from 'zod'; import { APP_DOCUMENT_UPLOAD_SIZE_LIMIT } from '@documenso/lib/constants/app'; @@ -19,7 +20,7 @@ import { DO_NOT_INVALIDATE_QUERY_ON_MUTATION, SKIP_QUERY_BATCH_META, } from '@documenso/lib/constants/trpc'; -import { AppError } from '@documenso/lib/errors/app-error'; +import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error'; import { type TRecipientLite, ZRecipientEmailSchema } from '@documenso/lib/types/recipient'; import { putPdfFile } from '@documenso/lib/universal/upload/put-file'; import { trpc } from '@documenso/trpc/react'; @@ -47,7 +48,6 @@ import { import { Input } from '@documenso/ui/primitives/input'; import { SpinnerBox } from '@documenso/ui/primitives/spinner'; import { Tooltip, TooltipContent, TooltipTrigger } from '@documenso/ui/primitives/tooltip'; -import type { Toast } from '@documenso/ui/primitives/use-toast'; import { useToast } from '@documenso/ui/primitives/use-toast'; const ZAddRecipientsForNewDocumentSchema = z.object({ @@ -201,19 +201,32 @@ export function TemplateUseDialog({ } catch (err) { const error = AppError.parseError(err); - const toastPayload: Toast = { + const errorMessage = match(error.code) + .with( + 'DOCUMENT_SEND_FAILED', + () => msg`The document was created but could not be sent to recipients.`, + ) + .with( + AppErrorCode.INVALID_BODY, + AppErrorCode.INVALID_REQUEST, + () => + msg`The document could not be created because of missing or invalid information. Please review the template's recipients and fields.`, + ) + .with( + AppErrorCode.NOT_FOUND, + () => msg`The template or one of its recipients could not be found.`, + ) + .with( + AppErrorCode.LIMIT_EXCEEDED, + () => msg`You have reached your document limit for this plan.`, + ) + .otherwise(() => msg`An error occurred while creating document from template.`); + + toast({ title: _(msg`Error`), - description: _(msg`An error occurred while creating document from template.`), + description: _(errorMessage), variant: 'destructive', - }; - - if (error.code === 'DOCUMENT_SEND_FAILED') { - toastPayload.description = _( - msg`The document was created but could not be sent to recipients.`, - ); - } - - toast(toastPayload); + }); } }; diff --git a/packages/trpc/server/template-router/router.ts b/packages/trpc/server/template-router/router.ts index af318ea19..e1b1f0b7b 100644 --- a/packages/trpc/server/template-router/router.ts +++ b/packages/trpc/server/template-router/router.ts @@ -602,6 +602,10 @@ export const templateRouter = router({ }).catch((err) => { console.error(err); + if (err instanceof AppError) { + throw err; + } + throw new AppError('DOCUMENT_SEND_FAILED'); }); }