# Class: TRPCError

> 

Defined in: [packages/server/src/unstable-core-do-not-import/error/TRPCError.ts:95](https://github.com/trpc/trpc/blob/7c907acfd8b31929214792e3b8c2ad7a8ac72563/packages/server/src/unstable-core-do-not-import/error/TRPCError.ts#L95)

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

```ts
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`](https://trpc.io/docs/typedoc/server/index/classes/TRPCError.md)

Defined in: [packages/server/src/unstable-core-do-not-import/error/TRPCError.ts:103](https://github.com/trpc/trpc/blob/7c907acfd8b31929214792e3b8c2ad7a8ac72563/packages/server/src/unstable-core-do-not-import/error/TRPCError.ts#L103)

#### Parameters

| Parameter | Type | Description |
| ------ | ------ | ------ |
| `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`? | `unknown` | The 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`? | `string` | Defaults to the cause's message, or else the code. |

#### Returns

[`TRPCError`](https://trpc.io/docs/typedoc/server/index/classes/TRPCError.md)

#### Overrides

`Error.constructor`

## Properties

### cause?

> `readonly` `optional` **cause**: `Error`

Defined in: [packages/server/src/unstable-core-do-not-import/error/TRPCError.ts:99](https://github.com/trpc/trpc/blob/7c907acfd8b31929214792e3b8c2ad7a8ac72563/packages/server/src/unstable-core-do-not-import/error/TRPCError.ts#L99)

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](https://github.com/trpc/trpc/blob/7c907acfd8b31929214792e3b8c2ad7a8ac72563/packages/server/src/unstable-core-do-not-import/error/TRPCError.ts#L101)

The tRPC error code, such as `'NOT_FOUND'`.
