|
| 1 | +--- |
| 2 | +outline: deep |
| 3 | +--- |
| 4 | + |
| 5 | +# DTK0004: Missing RPC Handler |
| 6 | + |
| 7 | +> Package: `@vitejs/devtools-rpc` |
| 8 | +
|
| 9 | +## Message |
| 10 | + |
| 11 | +> Either handler or setup function must be provided for RPC function "`{name}`" |
| 12 | +
|
| 13 | +## Cause |
| 14 | + |
| 15 | +This error is thrown by `getRpcHandler()` when an RPC function definition provides neither a `handler` property nor a `setup` function that returns a `handler`. It can occur in two situations: |
| 16 | + |
| 17 | +1. When the collector's proxy tries to resolve a handler for a registered function. |
| 18 | +2. During `dumpFunctions()` when resolving handlers for pre-computation. |
| 19 | + |
| 20 | +Every RPC function must have a way to produce a handler -- either directly via `handler` or lazily via `setup`. |
| 21 | + |
| 22 | +## Example |
| 23 | + |
| 24 | +```ts |
| 25 | +import { defineRpcFunction } from '@vitejs/devtools-kit' |
| 26 | + |
| 27 | +// Missing both handler and setup |
| 28 | +const broken = defineRpcFunction({ |
| 29 | + name: 'my-plugin:broken', |
| 30 | + type: 'query', |
| 31 | +}) |
| 32 | + |
| 33 | +collector.register(broken) |
| 34 | + |
| 35 | +// Throws DTK0004 when the handler is resolved |
| 36 | +await collector.getHandler('my-plugin:broken') |
| 37 | +``` |
| 38 | + |
| 39 | +A `setup` function that forgets to return a handler also triggers this error: |
| 40 | + |
| 41 | +```ts |
| 42 | +const alsoMissing = defineRpcFunction({ |
| 43 | + name: 'my-plugin:also-missing', |
| 44 | + type: 'query', |
| 45 | + setup: (ctx) => { |
| 46 | + // Forgot to return { handler: ... } |
| 47 | + return {} |
| 48 | + }, |
| 49 | +}) |
| 50 | +``` |
| 51 | + |
| 52 | +## Fix |
| 53 | + |
| 54 | +Provide a `handler` directly, or return one from `setup`: |
| 55 | + |
| 56 | +```ts |
| 57 | +// Option 1: Direct handler |
| 58 | +const getVersion = defineRpcFunction({ |
| 59 | + name: 'my-plugin:get-version', |
| 60 | + type: 'static', |
| 61 | + handler: () => '1.0.0', |
| 62 | +}) |
| 63 | + |
| 64 | +// Option 2: Handler via setup (useful when you need context) |
| 65 | +const getConfig = defineRpcFunction({ |
| 66 | + name: 'my-plugin:get-config', |
| 67 | + type: 'query', |
| 68 | + setup: ctx => ({ |
| 69 | + handler: () => ctx.config, |
| 70 | + }), |
| 71 | +}) |
| 72 | +``` |
| 73 | + |
| 74 | +## Source |
| 75 | + |
| 76 | +`packages/rpc/src/handler.ts` |
0 commit comments