Files
calendar/packages/trpc/server/lib/toTRPCError.ts
T
Benny JooGitHubbenny@cal.com <sldisek783@gmail.com>Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
3bf2a8fa6c refactor: replace TRPCError with ErrorWithCode in packages/features (#25482)
* refactor: replace TRPCError with ErrorWithCode in packages/features

This refactor moves error handling from throwing TRPCError directly in
packages/features to throwing ErrorWithCode instead. The conversion to
TRPCError now happens at the TRPC layer.

Changes:
- Add generic ErrorCode values (Unauthorized, Forbidden, NotFound,
  BadRequest, InternalServerError) to errorCodes.ts
- Update getServerErrorFromUnknown to map new ErrorCodes to proper
  HTTP status codes
- Create toTRPCError helper in packages/trpc/server/lib
- Create errorMappingMiddleware in packages/trpc/server/middlewares
- Migrate TRPCError throws in packages/features to ErrorWithCode:
  - teamService.ts
  - getEventTypeById.ts
  - eventTypeRepository.ts
  - OrganizationPermissionService.ts
  - OrganizationPaymentService.ts
  - sso.ts
  - handleCreatePhoneCall.ts
  - userCanCreateTeamGroupMapping.ts

This improves separation of concerns by making packages/features
transport-agnostic, allowing the same feature code to be reused from
tRPC, API routes, workers, etc.

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* fix: remove isTrpcCall parameter and fix lint warning

- Remove isTrpcCall parameter from get.handler.ts call since the
  feature layer no longer needs to know about tRPC
- Fix unsafe optional chaining lint warning in getEventTypesByViewer.ts
  by precomputing usersSource variable
- Complete migration of getEventTypesByViewer.ts to ErrorWithCode

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* revert

* add eslint rule

* add comment

* fix: add isTrpcCall back to getEventTypeById interface

The user reverted the removal of isTrpcCall parameter from the handler,
so we need to add it back to the interface to fix the type error.

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* test: update teamService tests to expect ErrorWithCode instead of TRPCError

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* refactor

* wip

* feat: integrate errorMappingMiddleware into base TRPC procedure

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* connect middlewares

* revert

* revert

* refactor

* rename

* fix: handle ErrorWithCode in teams server-page error handling

The error handling was checking for TRPCError, but teamService now throws
ErrorWithCode. This caused the 'This invitation is not for your account'
error message to not be displayed when a wrong user tries to use an
invitation link.

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* fix

* fix

* fix

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2025-12-02 08:11:17 -03:00

74 lines
1.8 KiB
TypeScript

import { ErrorWithCode } from "@calcom/lib/errors";
import { getHttpStatusCode } from "@calcom/lib/server/getServerErrorFromUnknown";
import { TRPCError } from "@trpc/server";
// Copied from `TRPC_ERROR_CODE_KEY` from `@trpc/server/unstable-core-do-not-import`
type TRPCErrorCode =
| "PARSE_ERROR"
| "BAD_REQUEST"
| "INTERNAL_SERVER_ERROR"
| "NOT_IMPLEMENTED"
| "UNAUTHORIZED"
| "FORBIDDEN"
| "NOT_FOUND"
| "METHOD_NOT_SUPPORTED"
| "TIMEOUT"
| "CONFLICT"
| "PRECONDITION_FAILED"
| "PAYLOAD_TOO_LARGE"
| "UNPROCESSABLE_CONTENT"
| "TOO_MANY_REQUESTS"
| "CLIENT_CLOSED_REQUEST";
function httpStatusToTrpcCode(status: number): TRPCErrorCode {
switch (status) {
case 400:
return "BAD_REQUEST";
case 401:
return "UNAUTHORIZED";
case 403:
return "FORBIDDEN";
case 404:
return "NOT_FOUND";
case 405:
return "METHOD_NOT_SUPPORTED";
case 408:
return "TIMEOUT";
case 409:
return "CONFLICT";
case 412:
return "PRECONDITION_FAILED";
case 413:
return "PAYLOAD_TOO_LARGE";
case 422:
return "UNPROCESSABLE_CONTENT";
case 429:
return "TOO_MANY_REQUESTS";
case 499:
return "CLIENT_CLOSED_REQUEST";
case 501:
return "NOT_IMPLEMENTED";
default:
return "INTERNAL_SERVER_ERROR";
}
}
/**
* Converts only ErrorWithCode instances to TRPC errors to limit impact.
*
* TODO: Consider converting any unknown error into TRPC error in the future.
*/
export function convertErrorWithCodeToTRPCError(cause: unknown) {
if (cause instanceof ErrorWithCode) {
const statusCode = getHttpStatusCode(cause);
return new TRPCError({
code: httpStatusToTrpcCode(statusCode),
message: cause.message ?? "",
cause: cause,
});
}
return cause;
}