|
| 1 | +SELECT 'list' AS component, |
| 2 | + 'Navigation' AS title; |
| 3 | + |
| 4 | +SELECT |
| 5 | + column1 AS title, column2 AS link, column3 AS description_md |
| 6 | +FROM (VALUES |
| 7 | + ('Link to arbitrary path', '/api/does/not/actually/exist', 'Covered by `api/404.sql`'), |
| 8 | + ('Link to arbitrary file', '/api/nothing.png', 'Covered by `api/404.sql`'), |
| 9 | + ('Link to non-existing .sql file', '/api/inexistent.sql', 'Covered by `api/404.sql`'), |
| 10 | + ('Link to 404 handler', '/api/404.sql', 'Actually `api/404.sql`'), |
| 11 | + ('Link to API landing page', '/api', 'Covered by `api/index.sql`'), |
| 12 | + ('Link to arbitrary broken path', '/backend/does/not/actually/exist', 'Not covered by anything, will yield a 404 error') |
| 13 | +); |
| 14 | + |
| 15 | +SELECT 'text' AS component, |
| 16 | + ' |
| 17 | +# Overview |
| 18 | +
|
| 19 | +This demo shows how a `404.sql` file can serve as a fallback error handler. Whenever a `404 Not |
| 20 | +Found` error would be emitted, instead a dedicated `404.sql` is called (if it exists) to serve the |
| 21 | +request. This is usefull in two scenarios: |
| 22 | +
|
| 23 | +1. Providing custom 404 error pages. |
| 24 | +2. To provide content under dynamic paths. |
| 25 | +
|
| 26 | +The former use-case is primarily of cosmetic nature, it allows for more informative, customized |
| 27 | +failure modes, enabling better UX. The latter use-case opens the door especially for REST API |
| 28 | +design, where dynamic paths are often used to convey arguments, i.e. `/api/resource/5` where `5` is |
| 29 | +the id of a resource. |
| 30 | +
|
| 31 | +
|
| 32 | +# Fallback Handler Selection |
| 33 | +
|
| 34 | +When a normal request to either a `.sql` or a static file fails with `404`, the `404` error is |
| 35 | +intercepted. The reuquest path''s target directory is scanned for a `404.sql`. If it exists, it is |
| 36 | +called. Otherwise, the parent directory is scanned for `404.sql`, which will be called if it exists. |
| 37 | +This search traverses up until it reaches the `web_root`. If even the webroot does not contain a |
| 38 | +`404.sql`, then the original `404` error is served as response to the HTTP client. |
| 39 | +
|
| 40 | +The fallback handler is not recursive; i.e. if anything causes another `404` during the call to a |
| 41 | +`404.sql`, then the request fails (emitting a `404` response to the HTTP client). |
| 42 | + ' AS contents_md; |
0 commit comments