[PR #1511] [MERGED] fix: refactor trpc errors #1683

Closed
opened 2026-02-26 20:30:35 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/documenso/documenso/pull/1511
Author: @dguyen
Created: 12/5/2024
Status: Merged
Merged: 12/6/2024
Merged by: @dguyen

Base: mainHead: fix/refactor-trpc-errors


📝 Commits (2)

  • 2f9b4bb fix: refactor trpc errors
  • a87af75 Merge branch 'main' into fix/refactor-trpc-errors

📊 Changes

27 files changed (+824 additions, -1536 deletions)

View changed files

📝 apps/web/src/app/(dashboard)/settings/profile/delete-account-dialog.tsx (+8 -19)
📝 apps/web/src/app/(signing)/sign/[token]/complete/claim-account.tsx (+18 -19)
📝 apps/web/src/components/forms/avatar-image.tsx (+13 -16)
📝 apps/web/src/components/forms/password.tsx (+20 -15)
📝 apps/web/src/components/forms/reset-password.tsx (+20 -15)
📝 apps/web/src/components/forms/signup.tsx (+12 -16)
📝 apps/web/src/components/forms/v2/signup.tsx (+26 -23)
📝 apps/web/src/pages/api/trpc/[trpc].ts (+1 -2)
📝 packages/lib/server-only/crypto/encrypt.ts (+1 -2)
📝 packages/lib/server-only/profile/set-avatar-image.ts (+7 -2)
📝 packages/lib/server-only/user/create-user.ts (+14 -1)
📝 packages/lib/server-only/user/reset-password.ts (+5 -4)
📝 packages/lib/server-only/user/update-password.ts (+5 -3)
📝 packages/trpc/server/admin-router/router.ts (+25 -90)
📝 packages/trpc/server/api-token-router/router.ts (+19 -57)
📝 packages/trpc/server/auth-router/router.ts (+54 -139)
packages/trpc/server/crypto/router.ts (+0 -8)
packages/trpc/server/crypto/schema.ts (+0 -15)
📝 packages/trpc/server/document-router/router.ts (+208 -394)
📝 packages/trpc/server/field-router/router.ts (+54 -95)

...and 7 more files

📄 Description

Description

Unwrapped functions in all trpc routers. The errors are parsed and handled in trpc callbacks.

Changes Made

  • Removed redundant crypto router
  • Unwrapped all router catch functions
  • Enable API error logging for all routers
  • Improved translations from server -> client

AppError

Errors will now be handled differently for trpc.

Inside a server function, you should throw an AppError for known errors, then parse it using AppError.parseError in the frontend.

Unknown errors such as an uncaught error from a 3rd party library, will be tagged as status code 500.

This should hopefully make error handling easier between front and backend. It will also be useful for proper error codes when we transition to OpenAPI via trpc.

Demonstration for errors in servers

// We map certain AppErrorCodes to status codes in `genericErrorCodeToTrpcErrorCodeMap`
// This should make it easier for generic errors like not found, unauth, invalid body, etc.
// This will produce a 404 error.
throw new AppError(AppErrorCode.NotFound, {
  message: 'Some optional message',
})

// This will produce a 400 error.
throw new AppError('ERROR_CODE', {
  statusCode: 400, // Optional status code
})

Backend -> Frontend

// Backend code.
throw new AppError('NO_PASSWORD', {
  statusCode: 400,
})

// Frontend
const onFormSubmit = async ({ currentPassword, password }: TPasswordFormSchema) => {
  try {
    await updatePassword({
      currentPassword,
      password,
    });

    // Rest of owl.
  } catch (err) {
    const error = AppError.parseError(err); // Rebuild the AppError from the error thrown by trpc.

    // Handle the error as you see fit.
    // error.code will be the same as the one thrown in the server.
    const errorMessage = match(error.code)
      .with('NO_PASSWORD', () => msg`User has no password.`)
      .with('INCORRECT_PASSWORD', () => msg`Current password is incorrect.`)
      .otherwise(
        () =>
          msg`We encountered an unknown error while attempting to update your password. Please try again later.`,
      );

    toast({
      title: _(msg`An error occurred`),
      description: _(errorMessage),
      variant: 'destructive',
    });
  }

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/documenso/documenso/pull/1511 **Author:** [@dguyen](https://github.com/dguyen) **Created:** 12/5/2024 **Status:** ✅ Merged **Merged:** 12/6/2024 **Merged by:** [@dguyen](https://github.com/dguyen) **Base:** `main` ← **Head:** `fix/refactor-trpc-errors` --- ### 📝 Commits (2) - [`2f9b4bb`](https://github.com/documenso/documenso/commit/2f9b4bbfd60109a6343220bc4006b156e46ac1bd) fix: refactor trpc errors - [`a87af75`](https://github.com/documenso/documenso/commit/a87af75954dbfae22a94b231db9dae3af222e39c) Merge branch 'main' into fix/refactor-trpc-errors ### 📊 Changes **27 files changed** (+824 additions, -1536 deletions) <details> <summary>View changed files</summary> 📝 `apps/web/src/app/(dashboard)/settings/profile/delete-account-dialog.tsx` (+8 -19) 📝 `apps/web/src/app/(signing)/sign/[token]/complete/claim-account.tsx` (+18 -19) 📝 `apps/web/src/components/forms/avatar-image.tsx` (+13 -16) 📝 `apps/web/src/components/forms/password.tsx` (+20 -15) 📝 `apps/web/src/components/forms/reset-password.tsx` (+20 -15) 📝 `apps/web/src/components/forms/signup.tsx` (+12 -16) 📝 `apps/web/src/components/forms/v2/signup.tsx` (+26 -23) 📝 `apps/web/src/pages/api/trpc/[trpc].ts` (+1 -2) 📝 `packages/lib/server-only/crypto/encrypt.ts` (+1 -2) 📝 `packages/lib/server-only/profile/set-avatar-image.ts` (+7 -2) 📝 `packages/lib/server-only/user/create-user.ts` (+14 -1) 📝 `packages/lib/server-only/user/reset-password.ts` (+5 -4) 📝 `packages/lib/server-only/user/update-password.ts` (+5 -3) 📝 `packages/trpc/server/admin-router/router.ts` (+25 -90) 📝 `packages/trpc/server/api-token-router/router.ts` (+19 -57) 📝 `packages/trpc/server/auth-router/router.ts` (+54 -139) ➖ `packages/trpc/server/crypto/router.ts` (+0 -8) ➖ `packages/trpc/server/crypto/schema.ts` (+0 -15) 📝 `packages/trpc/server/document-router/router.ts` (+208 -394) 📝 `packages/trpc/server/field-router/router.ts` (+54 -95) _...and 7 more files_ </details> ### 📄 Description ## Description Unwrapped functions in all trpc routers. The errors are parsed and handled in trpc callbacks. ## Changes Made - Removed redundant crypto router - Unwrapped all router catch functions - Enable API error logging for all routers - Improved translations from server -> client ### AppError Errors will now be handled differently for trpc. Inside a server function, you should throw an `AppError` for known errors, then parse it using `AppError.parseError` in the frontend. Unknown errors such as an uncaught error from a 3rd party library, will be tagged as status code 500. This should hopefully make error handling easier between front and backend. It will also be useful for proper error codes when we transition to OpenAPI via trpc. **Demonstration for errors in servers** ```ts // We map certain AppErrorCodes to status codes in `genericErrorCodeToTrpcErrorCodeMap` // This should make it easier for generic errors like not found, unauth, invalid body, etc. // This will produce a 404 error. throw new AppError(AppErrorCode.NotFound, { message: 'Some optional message', }) // This will produce a 400 error. throw new AppError('ERROR_CODE', { statusCode: 400, // Optional status code }) ``` ### Backend -> Frontend ```ts // Backend code. throw new AppError('NO_PASSWORD', { statusCode: 400, }) // Frontend const onFormSubmit = async ({ currentPassword, password }: TPasswordFormSchema) => { try { await updatePassword({ currentPassword, password, }); // Rest of owl. } catch (err) { const error = AppError.parseError(err); // Rebuild the AppError from the error thrown by trpc. // Handle the error as you see fit. // error.code will be the same as the one thrown in the server. const errorMessage = match(error.code) .with('NO_PASSWORD', () => msg`User has no password.`) .with('INCORRECT_PASSWORD', () => msg`Current password is incorrect.`) .otherwise( () => msg`We encountered an unknown error while attempting to update your password. Please try again later.`, ); toast({ title: _(msg`An error occurred`), description: _(errorMessage), variant: 'destructive', }); } ``` --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
kerem 2026-02-26 20:30:35 +03:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/documenso#1683
No description provided.