Skip to content

Commit 3812688

Browse files
committed
docs(skills): fit the hook refusal section under the data-hooks token ceiling
Pays for the new section by deleting the error-handling examples the handler and onError sections already carry, and folds the testing pointer into the references list. Claude-Session: https://claude.ai/code/session_01WGufzfyVQuRsL6SRGuKCem Co-authored-by: Claude <noreply@anthropic.com>
1 parent 7a55ba4 commit 3812688

1 file changed

Lines changed: 19 additions & 93 deletions

File tree

‎skills/objectstack-data/references/data-hooks.md‎

Lines changed: 19 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
- [Hook Definition Schema](#hook-definition-schema) · [Key Properties Explained](#key-properties-explained)
55
- [Sandboxed Hook Bodies (`body`)](#sandboxed-hook-bodies-body--what-the-sandbox-ctx-can-call) · [Capabilities — the complete list](#capabilities--the-complete-list) · [Sandbox restrictions](#sandbox-restrictions)
66
- [Hook Context API](#hook-context-api) · [Common Patterns](#common-patterns) · [Registration Methods](#registration-methods)
7-
- [Best Practices](#best-practices) · [Error Handling](#error-handling) · [Testing Hooks](#testing-hooks) · [References](#references)
7+
- [Best Practices](#best-practices) · [Error Handling](#error-handling) · [References](#references)
88

99
Reference companion to `objectstack-data/SKILL.md`, and the catalog's anchor for
1010
data hooks. Covers the 8 lifecycle events, the inline `handler` and sandboxed
@@ -113,9 +113,6 @@ const myHook = defineHook({
113113
// Optional: Error handling strategy
114114
onError: 'abort', // 'abort' | 'log'
115115

116-
// Optional: Declared refusal catalogue (sandboxed `body` refuses by code)
117-
// refusals: { CREDIT_LIMIT: { message: 'Over the credit limit.', status: 422 } },
118-
119116
// Optional: Execution timeout (ms)
120117
timeoutMs: 5000,
121118

@@ -938,101 +935,29 @@ hook re-trigger itself; use `object: '*'` unless you mean every object; throw in
938935

939936
## Error Handling
940937

941-
### Throwing Errors (Abort Operation)
942-
943-
```typescript
944-
handler: async (ctx) => {
945-
if (!ctx.input.email) {
946-
// Aborts operation, rolls back transaction
947-
throw new Error('Email is required');
948-
}
949-
}
950-
```
951-
952-
### Logging Errors (Continue)
953-
954-
```typescript
955-
{
956-
onError: 'log', // Log error, don't abort
957-
handler: async (ctx) => {
958-
try {
959-
await sendEmail(ctx.input.email);
960-
} catch (error) {
961-
// Error is logged, operation continues
962-
console.error('Failed to send email', error);
963-
}
964-
}
965-
}
966-
```
967-
968-
### Custom Error Messages
969-
970-
```typescript
971-
handler: async (ctx) => {
972-
if (ctx.input.annual_revenue < 0) {
973-
throw new Error('Annual revenue cannot be negative');
974-
}
975-
976-
if (ctx.input.annual_revenue > 1000000000) {
977-
throw new Error('Annual revenue exceeds maximum allowed value (1B)');
978-
}
979-
}
980-
```
938+
A throw aborts the operation and rolls back its transaction; `onError: 'log'`
939+
(above) makes an `after*` hook log and continue instead.
981940

982-
### Declared refusals (`refusals`) — coded, translatable refusals
941+
### Declared refusals (`refusals`)
983942

984-
A hook can declare its refusal catalogue: one entry per code, each with the
985-
sentence the user reads and an optional 4xx `status` (default `400`). The body
986-
then refuses **by code only** — it throws an error carrying a declared `code`,
987-
and the runtime answers with the entry's message and status:
943+
With a `refusals` catalogue, a sandboxed `body` refuses **by code only** and the
944+
runtime answers with that entry:
988945

989946
```typescript
990-
defineHook({
991-
name: 'credit_guard',
992-
object: 'order',
993-
events: ['beforeInsert'],
994-
refusals: {
995-
CREDIT_LIMIT: { message: 'This order exceeds the customer credit limit.' },
996-
ACCOUNT_ON_HOLD: { message: 'The account is on hold.', status: 409 },
997-
},
998-
body: {
999-
language: 'js',
1000-
source: `
1001-
if (ctx.input.amount > 100000) {
1002-
throw Object.assign(new Error('over limit'), { code: 'CREDIT_LIMIT' });
1003-
}
1004-
`,
1005-
},
1006-
});
947+
refusals: {
948+
CREDIT_LIMIT: { message: 'Over the credit limit.' },
949+
ON_HOLD: { message: 'Account on hold.', status: 409 }, // default 400
950+
},
951+
body: { language: 'js', source: `
952+
if (ctx.input.amount > 100000) throw Object.assign(new Error(), { code: 'CREDIT_LIMIT' });
953+
` },
1007954
```
1008955

1009-
- **Codes** are SCREAMING_SNAKE (`CREDIT_LIMIT`). The error envelope carries
1010-
it as `declaredCode`, beside the status's registered `code`
1011-
(`VALIDATION_ERROR` for 400), so a client can branch on it.
1012-
- **The message is translated** through
1013-
`hooks.<hook>.refusals.<CODE>.message` in the caller's locale; `os i18n
1014-
extract` scaffolds one key per declared entry and `os i18n check` counts them.
1015-
The message is plain text — no `{{ }}` holes are filled. The body's own
1016-
`Error` text and `status` are not what the caller reads.
1017-
- **An undeclared code is refused loudly.** A code the body throws literally
1018-
(`{ code: 'X' }`, or `e.code = 'X'; throw e`) that the catalogue does not
1019-
declare fails the save at `body.source`. Any other undeclared code — or a
1020-
throw with no code — is a `500` fault at run time, naming the hook and the
1021-
code. So once a hook declares `refusals`, every refusal it makes is one of them.
1022-
- An error a `ctx.api` call raised that the body lets leave unchanged, and a
1023-
crash (`TypeError`), pass through as before; they are not the body's refusal.
1024-
- The catalogue is read for a sandboxed `body`. An inline `handler` keeps
1025-
throwing its own errors.
1026-
1027-
---
1028-
1029-
## Testing Hooks
1030-
1031-
Hook test harnesses — vitest units and `LiteKernel` integration setups — are the
1032-
**objectstack-platform** skill's surface (its frontmatter claims "test harnesses via
1033-
LiteKernel"): see [objectstack-platform/SKILL.md](../../objectstack-platform/SKILL.md).
1034-
1035-
---
956+
- Codes are SCREAMING_SNAKE; the error envelope carries one as `declaredCode`.
957+
- The plain-text message is translated via `hooks.<hook>.refusals.<CODE>.message`
958+
in the caller's locale; the body's own error text and `status` are not used.
959+
- An undeclared code, or none, is refused: at save if thrown literally, else as
960+
a `500` fault. Unchanged `ctx.api` errors and crashes pass through.
1036961

1037962
---
1038963

@@ -1042,5 +967,6 @@ LiteKernel"): see [objectstack-platform/SKILL.md](../../objectstack-platform/SKI
1042967
- [Project hooks pattern](../SKILL.md#lifecycle-hooks) — Hook integration in the data skill
1043968
- [objectstack-platform/references/plugin-hooks.md](../../objectstack-platform/references/plugin-hooks.md) — plugin/kernel hooks (a different extension point)
1044969
- [objectstack-automation](../../objectstack-automation/SKILL.md) — Flows and Workflows
970+
- [objectstack-platform/SKILL.md](../../objectstack-platform/SKILL.md) — hook test harnesses (vitest units, `LiteKernel`)
1045971

1046972
---

0 commit comments

Comments
 (0)