Commit 59ba69f
fix(graphql): structured error handling for page query (NOT_FOUND/PERMISSION_DENIED/BAD_REQUEST) (#35044)
## Summary
This PR introduces **structured error handling** for the GraphQL page
query across the dotCMS SDKs and example apps. The core idea: errors
should carry a stable, machine-readable code that consumers can branch
on — not free-form strings that break the moment a backend message is
reworded.
## The core: error messages
Before this PR, the SDK detected error types by string-matching on
GraphQL error messages (`"not found"`, `"permission"`, etc.). That is
brittle — any backend rewording silently degrades every error into a
generic 404 — and it doesn't survive i18n.
The fix is end-to-end:
1. **Backend always emits `extensions.classification`.**
`PermissionDeniedGraphQLException` now overrides `getErrorType()` so
GraphQL serializes the classification reliably. `PageDataFetcher` logs
the original `HTMLPageAssetNotFoundException` before rethrowing, so
stack traces are preserved.
2. **A stable contract on the wire.** Every page-query error response
now carries an `extensions.code` value: `NOT_FOUND`,
`PERMISSION_DENIED`, `BAD_REQUEST`, etc.
3. **The client reads the code, not the message.** `page-api.ts` was
rewritten around four explicit branches (logged unstructured errors →
`BAD_REQUEST` when `data` is null → structured codes when `data.page` is
null → generic 404 fallback). Partial content failures (page loads but a
sub-query fails) no longer swallow the errors — they surface via
`response.errors[]`.
4. **Error objects are useful.** `DotErrorPage` exposes `status` and
`code`, the `PERMISSION_DENIED` message includes actionable guidance
about dotCMS permissions, and a new `logLevel: 'verbose'` config logs
status, code, URL, and variables for debugging without polluting
production logs.
| Scenario | Before | After |
|---|---|---|
| Page does not exist | `null`, always 404 | `NOT_FOUND` / 404 |
| No permission | String-match miss → 404 | `PERMISSION_DENIED` / 403 |
| Malformed GraphQL query | JS `TypeError` | `BAD_REQUEST` / 400 |
| Partial content failure | Silently swallowed | `errors[]` on a
successful page response |
---
## Backend (Java)
- `PageDataFetcher` logs the original `HTMLPageAssetNotFoundException`
before rethrowing as `ResourceNotFoundException`.
- `PermissionDeniedGraphQLException` implements `getErrorType()`
returning `DataFetchingException` so `extensions.classification` is
always serialized.
## SDK — `@dotcms/types` (v1.2.0)
- `DotGraphQLApiResponse.data` typed as `{ page: DotCMSGraphQLPage |
null } | null` — models all four real response shapes.
- `DotCMSGraphQLError.extensions` and `.classification` made optional;
`path?: string[]` added.
- `DotCMSPageResponse` gains `errors?: DotCMSGraphQLError[]`; the
singular `error` field is kept as a `@deprecated` alias (removal: August
2026).
- `DotErrorPage` constructor `status` / `code` params have safe defaults
(`500`, `'UNKNOWN'`) — non-breaking.
## SDK — `@dotcms/client` (v1.2.0)
- Error detection switched from message string-matching to
`extensions.code`.
- Four-branch error handling in `page-api.ts` (see core summary above).
- `normalizedUrl` extracted once and reused across all error messages,
logs, and `DotErrorPage` instances.
- New `logLevel: 'default' | 'verbose'` config on `DotCMSClientConfig`;
verbose mode also gates the "no page query" performance warning.
- Improved `PERMISSION_DENIED` message with actionable guidance.
- Test coverage expanded for `page-api`, `content-api`, `utils`, and
verbose logging.
---
## Examples
### `examples/nextjs`
- `getDotCMSPage` returns `{ error }` on failure — consumers check
`pageContent?.error` instead of try/catch.
- New `ErrorPage` component handles 403 (Access Denied) and defaults
unknown statuses to 500.
- New shared `ErrorLayout` component used by both `ErrorPage` and
`not-found.js`; styling matched to the 404 design.
- Return-home link uses semantic `<Link>` directly.
- `dotCMSClient` enables `logLevel: 'verbose'` automatically in
development.
### `examples/astro`
- `Error.astro` updated to render structured status/code from
`DotErrorPage`.
- `getPage.ts` returns structured errors instead of throwing.
- `[...slug].astro` branches on the structured error to render the right
status.
- `dotCMSClient` enables verbose logging in development.
### `examples/angular`
- New standalone `ErrorComponent` rendering status, code, and message.
- `page.ts` consumes the structured error from the SDK and forwards it
to the template.
- `app.config.ts` enables verbose logging in development.
### `examples/angular-ssr`
- Same `ErrorComponent` + `page.ts` updates as the Angular example,
wired through `server.ts` so SSR returns the correct HTTP status.
## 1 parent 32daf63 commit 59ba69f
34 files changed
Lines changed: 1507 additions & 196 deletions
File tree
- core-web/libs/sdk
- angular/src/lib/providers/dotcms-client
- client
- src/lib/client
- content
- page
- types
- src/lib
- client
- page
- dotCMS/src/main/java/com/dotcms/graphql
- datafetcher/page
- exception
- examples
- angular-ssr
- src
- app
- components/error
- dotcms/pages/page
- angular
- src/app
- components/error
- dotcms/pages/page
- astro/src
- components/common
- integration/dotcms
- pages
- nextjs/src
- app
- [[...slug]]
- blog
- components
- utils
Lines changed: 1 addition & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
75 | 75 | | |
76 | 76 | | |
77 | 77 | | |
| 78 | + | |
78 | 79 | | |
79 | 80 | | |
80 | 81 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
699 | 699 | | |
700 | 700 | | |
701 | 701 | | |
702 | | - | |
703 | | - | |
704 | | - | |
705 | | - | |
706 | | - | |
707 | | - | |
708 | | - | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
709 | 710 | | |
710 | 711 | | |
711 | 712 | | |
712 | 713 | | |
713 | 714 | | |
714 | 715 | | |
715 | 716 | | |
716 | | - | |
| 717 | + | |
| 718 | + | |
717 | 719 | | |
718 | 720 | | |
719 | 721 | | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
720 | 725 | | |
721 | 726 | | |
722 | 727 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| |||
Lines changed: 193 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
0 commit comments