Skip to content

feat(starter): surface CNG permission 403s in the demo - #26

Merged
coladarci merged 1 commit into
mainfrom
starter/cng-permission-error-403
Sep 15, 2026
Merged

coladarci merged 1 commit into
mainfrom
starter/cng-permission-error-403

Conversation

@coladarci

@coladarci coladarci commented Sep 15, 2026 •

Copy link
Copy Markdown
Collaborator

Ports the error-handling changes from the local cng-test-starter-kit working tree into the scaffolding template, so every newly scaffolded app gets them.

What changed

app/examples/cng/main/api/activities/route.ts — the route wraps its cng.getAllActivities() call and translates a CngPermissionError into a 403 JSON response:

return NextResponse.json(
  { error: err.message, permissions: err.permissions },
  { status: err.statusCode },
);

This is an expected failure for an install whose manifest grants fewer permissions than the route calls for — not a bug — so it becomes a response the client can render instead of an unhandled throw that Next.js reports as a 500 with a stack trace. The permissions array lets the client name exactly what to grant. Any other rejection is re-thrown unchanged.

The rescue deliberately lives in the CNG route rather than in withAppAuthentication: that wrapper is the shared entry point for every platform the app is embedded into (Peek, Connect&GO, ACME), and only CNG throws this error. lib/with-app.ts is untouched by this PR.

app/examples/cng/main/view/page.tsx — the demo already rendered err.message; a comment now records why it must stay verbatim. The gateway's text ("CNG request forbidden: the app is missing the required permission: products:read") reaches the UI instead of being flattened to a generic failure.

Tests — three added: the 403 response shape with named permissions and the re-throw of any other failure (both in the activities route suite), plus the demo surfacing the gateway message verbatim.

Dependency bump

CngPermissionError is only exported from @peektravel/app-utilities 0.8.2 — the published 0.8.0 and 0.8.1 tarballs don't have it. The template's dependency therefore moves ^0.8.0 → ^0.8.2 with its lockfile regenerated, otherwise a freshly scaffolded app fails to typecheck on the import.

The root package.json / pnpm-lock.yaml also gain @peektravel/app-utilities@^0.8.2 as a CLI dependency. That change was already in the working tree and is carried along here; nothing under src/ imports it yet.

Verification

  • tsc -b and vitest run in this repo — clean, 23 passed, including the peek init e2e that scaffolds and installs deps against the bumped template
  • The template's versions of the touched files were copied over a scratch checkout of cng-test-starter-kit (which has the deps installed) and its full suite run there: tsc --noEmit clean, 181 tests passed

No CLI command, flag, or extension behavior changed, so the cli skill needs no sync. Worth a follow-up: src/skills/platform/cng/backoffice-api/SKILL.md documents CngApiError and could teach this 403 pattern to app-building agents.

🤖 Generated with Claude Code

`withAppAuthentication` now awaits the route handler inside a try/catch and
translates a `CngPermissionError` into a 403 JSON response carrying the
gateway's message and the missing `permissions`. Doing it in the wrapper keeps
every route from repeating the same catch, and stops an expected
misconfigured-install failure from surfacing as an unhandled 500 with a stack
trace. Any other rejection is re-thrown unchanged.

The CNG example view already rendered `err.message`, so the gateway's text
("...missing the required permission: products:read") now reaches the UI
verbatim instead of being flattened to a generic failure.

`CngPermissionError` is only exported from @peektravel/app-utilities 0.8.2, so
the starter template's dependency moves from ^0.8.0 to ^0.8.2.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

refactor(starter): rescue CNG permission 403s in the route, not the wrapper

`withAppAuthentication` is the shared entry point for every platform the app is
embedded into (Peek, Connect&GO, ACME), so teaching it about `CngPermissionError`
put one platform's failure mode in a file that serves all of them. Revert the
wrapper to its platform-agnostic form and move the rescue into the CNG example
route that actually makes the call.

The 403 response shape is unchanged — the gateway's message plus the missing
`permissions` — and so is the demo view that renders it. The two wrapper tests
move alongside the code, into the activities route's suite.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

remove from top level; added by mistake

dumb comment
@coladarci
coladarci force-pushed the starter/cng-permission-error-403 branch from 4c9f067 to a14c2c4 Compare September 15, 2026 19:10
@coladarci
coladarci merged commit 0ad3a46 into main Sep 15, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant