Skip to content
tRPC

Partners

Class: TRPCError

On this page

Defined in: packages/server/src/unstable-core-do-not-import/error/TRPCError.ts:95

An error with a tRPC error code, which sets the HTTP status and the code the client sees. Throw it from resolvers and middlewares.

Example

throw new TRPCError({ code: 'NOT_FOUND', message: 'No such post' });

See

https://trpc.io/docs/server/error-handling#throwing-errors

Extends

  • Error

Constructors

new TRPCError()

new TRPCError(opts): TRPCError

Defined in: packages/server/src/unstable-core-do-not-import/error/TRPCError.ts:103

Parameters

ParameterTypeDescription
opts{ cause: unknown; code: "PARSE_ERROR" | "BAD_REQUEST" | "INTERNAL_SERVER_ERROR" | "NOT_IMPLEMENTED" | "BAD_GATEWAY" | "SERVICE_UNAVAILABLE" | "GATEWAY_TIMEOUT" | "UNAUTHORIZED" | "PAYMENT_REQUIRED" | "FORBIDDEN" | "NOT_FOUND" | "METHOD_NOT_SUPPORTED" | "TIMEOUT" | "CONFLICT" | "PRECONDITION_FAILED" | "PAYLOAD_TOO_LARGE" | "UNSUPPORTED_MEDIA_TYPE" | "UNPROCESSABLE_CONTENT" | "PRECONDITION_REQUIRED" | "TOO_MANY_REQUESTS" | "CLIENT_CLOSED_REQUEST"; message: string; }-
opts.cause?unknownThe original error. Anything that isn't an Error is wrapped in one.
opts.code"PARSE_ERROR" | "BAD_REQUEST" | "INTERNAL_SERVER_ERROR" | "NOT_IMPLEMENTED" | "BAD_GATEWAY" | "SERVICE_UNAVAILABLE" | "GATEWAY_TIMEOUT" | "UNAUTHORIZED" | "PAYMENT_REQUIRED" | "FORBIDDEN" | "NOT_FOUND" | "METHOD_NOT_SUPPORTED" | "TIMEOUT" | "CONFLICT" | "PRECONDITION_FAILED" | "PAYLOAD_TOO_LARGE" | "UNSUPPORTED_MEDIA_TYPE" | "UNPROCESSABLE_CONTENT" | "PRECONDITION_REQUIRED" | "TOO_MANY_REQUESTS" | "CLIENT_CLOSED_REQUEST"See https://trpc.io/docs/server/error-handling#error-codes
opts.message?stringDefaults to the cause's message, or else the code.

Returns

TRPCError

Overrides

Error.constructor

Properties

cause?

readonly optional cause: Error

Defined in: packages/server/src/unstable-core-do-not-import/error/TRPCError.ts:99

The error this one wraps, if any.

Overrides

Error.cause


code

readonly code: "PARSE_ERROR" | "BAD_REQUEST" | "INTERNAL_SERVER_ERROR" | "NOT_IMPLEMENTED" | "BAD_GATEWAY" | "SERVICE_UNAVAILABLE" | "GATEWAY_TIMEOUT" | "UNAUTHORIZED" | "PAYMENT_REQUIRED" | "FORBIDDEN" | "NOT_FOUND" | "METHOD_NOT_SUPPORTED" | "TIMEOUT" | "CONFLICT" | "PRECONDITION_FAILED" | "PAYLOAD_TOO_LARGE" | "UNSUPPORTED_MEDIA_TYPE" | "UNPROCESSABLE_CONTENT" | "PRECONDITION_REQUIRED" | "TOO_MANY_REQUESTS" | "CLIENT_CLOSED_REQUEST"

Defined in: packages/server/src/unstable-core-do-not-import/error/TRPCError.ts:101

The tRPC error code, such as 'NOT_FOUND'.