Skip to content

Commit 7d0e98e

Browse files
antfuclaude
andcommitted
feat: migrate warnings/errors to structured diagnostics via logs-sdk
Replace inconsistent console.warn/error/throw patterns with structured diagnostics using @antfu/experimental-logs-sdk. Each error gets a unique code (DTK0001-DTK0032 for core/rpc, RDDT0001-RDDT0002 for rolldown) with auto-generated docs URLs and ANSI-formatted output. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 5d11ef7 commit 7d0e98e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+3017
-150
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@ packages/kit/skills
2020
docs/.vitepress/cache
2121
.turbo
2222
.context
23+
24+
# Agent skills from npm packages (managed by skills-npm)
25+
**/skills/npm-*

docs/.vitepress/config.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,29 @@ export default extendConfig(withMermaid(defineConfig({
9191
{ text: 'Examples', link: '/kit/examples' },
9292
],
9393
},
94+
{
95+
text: 'Error Reference',
96+
link: '/errors/',
97+
collapsed: true,
98+
items: [
99+
{
100+
text: 'DevTools Kit (DTK)',
101+
collapsed: true,
102+
items: Array.from({ length: 32 }, (_, i) => {
103+
const code = `DTK${String(i + 1).padStart(4, '0')}`
104+
return { text: code, link: `/errors/${code}` }
105+
}),
106+
},
107+
{
108+
text: 'Rolldown DevTools (RDDT)',
109+
collapsed: true,
110+
items: [
111+
{ text: 'RDDT0001', link: '/errors/RDDT0001' },
112+
{ text: 'RDDT0002', link: '/errors/RDDT0002' },
113+
],
114+
},
115+
],
116+
},
94117
],
95118

96119
search: {

docs/errors/DTK0001.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
outline: deep
3+
---
4+
5+
# DTK0001: RPC Function Already Registered
6+
7+
> Package: `@vitejs/devtools-rpc`
8+
9+
## Message
10+
11+
> RPC function "`{name}`" is already registered
12+
13+
## Cause
14+
15+
This error is thrown by `RpcFunctionsCollectorBase.register()` when you attempt to register an RPC function with a name that already exists in the collector. Each function name must be unique within a collector instance unless you explicitly opt into overwriting.
16+
17+
## Example
18+
19+
```ts
20+
import { defineRpcFunction } from '@vitejs/devtools-kit'
21+
22+
const getVersion = defineRpcFunction({
23+
name: 'my-plugin:get-version',
24+
type: 'static',
25+
handler: () => '1.0.0',
26+
})
27+
28+
const getVersionAlt = defineRpcFunction({
29+
name: 'my-plugin:get-version', // same name as above
30+
type: 'static',
31+
handler: () => '2.0.0',
32+
})
33+
34+
// First registration succeeds
35+
collector.register(getVersion)
36+
37+
// Second registration throws DTK0001
38+
collector.register(getVersionAlt)
39+
```
40+
41+
## Fix
42+
43+
Either use a unique name for each function, or pass `force: true` to overwrite the existing registration:
44+
45+
```ts
46+
// Option 1: Use a unique name
47+
const getVersionAlt = defineRpcFunction({
48+
name: 'my-plugin:get-version-alt',
49+
type: 'static',
50+
handler: () => '2.0.0',
51+
})
52+
collector.register(getVersionAlt)
53+
54+
// Option 2: Force overwrite
55+
collector.register(getVersionAlt, true)
56+
```
57+
58+
## Source
59+
60+
`packages/rpc/src/collector.ts`

docs/errors/DTK0002.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
outline: deep
3+
---
4+
5+
# DTK0002: RPC Function Not Registered (Update)
6+
7+
> Package: `@vitejs/devtools-rpc`
8+
9+
## Message
10+
11+
> RPC function "`{name}`" is not registered. Use register() to add new functions.
12+
13+
## Cause
14+
15+
This error is thrown by `RpcFunctionsCollectorBase.update()` when you attempt to update a function definition that has not been registered yet. The `update()` method is intended for replacing an existing definition, not for adding new ones.
16+
17+
## Example
18+
19+
```ts
20+
import { defineRpcFunction } from '@vitejs/devtools-kit'
21+
22+
const getVersion = defineRpcFunction({
23+
name: 'my-plugin:get-version',
24+
type: 'static',
25+
handler: () => '2.0.0',
26+
})
27+
28+
// Throws DTK0002 because 'my-plugin:get-version' was never registered
29+
collector.update(getVersion)
30+
```
31+
32+
## Fix
33+
34+
Register the function first with `register()`, then use `update()` for subsequent changes. Alternatively, pass `force: true` to `update()` to allow upserting:
35+
36+
```ts
37+
// Option 1: Register first, update later
38+
collector.register(getVersion)
39+
// ... later ...
40+
collector.update(getVersionUpdated)
41+
42+
// Option 2: Force update (creates if missing)
43+
collector.update(getVersion, true)
44+
```
45+
46+
## Source
47+
48+
`packages/rpc/src/collector.ts`

docs/errors/DTK0003.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
outline: deep
3+
---
4+
5+
# DTK0003: RPC Function Schema Not Found
6+
7+
> Package: `@vitejs/devtools-rpc`
8+
9+
## Message
10+
11+
> RPC function "`{name}`" is not registered
12+
13+
## Cause
14+
15+
This error is thrown by `RpcFunctionsCollectorBase.getSchema()` when you request the argument and return schemas for a function name that does not exist in the collector. The function must be registered before its schema can be queried.
16+
17+
## Example
18+
19+
```ts
20+
// Throws DTK0003 because 'my-plugin:get-config' was never registered
21+
const schema = collector.getSchema('my-plugin:get-config')
22+
```
23+
24+
## Fix
25+
26+
Ensure the function is registered before querying its schema. You can check whether a function exists first using `has()`:
27+
28+
```ts
29+
import { defineRpcFunction } from '@vitejs/devtools-kit'
30+
31+
const getConfig = defineRpcFunction({
32+
name: 'my-plugin:get-config',
33+
type: 'query',
34+
handler: () => ({ debug: false }),
35+
})
36+
37+
// Register first
38+
collector.register(getConfig)
39+
40+
// Now getSchema works
41+
const schema = collector.getSchema('my-plugin:get-config')
42+
43+
// Or guard with has()
44+
if (collector.has('my-plugin:get-config')) {
45+
const schema = collector.getSchema('my-plugin:get-config')
46+
}
47+
```
48+
49+
## Source
50+
51+
`packages/rpc/src/collector.ts`

docs/errors/DTK0004.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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`

docs/errors/DTK0005.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
outline: deep
3+
---
4+
5+
# DTK0005: Function Not in Dump Store
6+
7+
> Package: `@vitejs/devtools-rpc`
8+
9+
## Message
10+
11+
> Function "`{name}`" not found in dump store
12+
13+
## Cause
14+
15+
This error is thrown by `createClientFromDump()` when you call a function on the dump client that was not included in the definitions passed to `dumpFunctions()`. The dump store only contains entries for functions that were part of the original dump collection, and the client rejects calls to unknown functions.
16+
17+
## Example
18+
19+
```ts
20+
import { defineRpcFunction } from '@vitejs/devtools-kit'
21+
import { createClientFromDump, dumpFunctions } from '@vitejs/devtools-rpc'
22+
23+
const getVersion = defineRpcFunction({
24+
name: 'my-plugin:get-version',
25+
type: 'static',
26+
handler: () => '1.0.0',
27+
})
28+
29+
// Only getVersion is dumped
30+
const store = await dumpFunctions([getVersion])
31+
const client = createClientFromDump(store)
32+
33+
// Throws DTK0005 because 'my-plugin:get-config' is not in the store
34+
await client['my-plugin:get-config']()
35+
```
36+
37+
## Fix
38+
39+
Include the function in the definitions array passed to `dumpFunctions()`:
40+
41+
```ts
42+
const getConfig = defineRpcFunction({
43+
name: 'my-plugin:get-config',
44+
type: 'query',
45+
handler: () => ({ debug: false }),
46+
dump: { inputs: [[]] },
47+
})
48+
49+
// Include all needed functions in the dump
50+
const store = await dumpFunctions([getVersion, getConfig])
51+
const client = createClientFromDump(store)
52+
53+
// Now both functions work
54+
await client['my-plugin:get-version']()
55+
await client['my-plugin:get-config']()
56+
```
57+
58+
## Source
59+
60+
`packages/rpc/src/dumps.ts`

0 commit comments

Comments
 (0)