Skip to content

Commit 3e20ae4

Browse files
committed
feat: add 404.sql example
1 parent 3bc8185 commit 3e20ae4

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

examples/handle-404/api/404.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
SELECT 'debug' AS component,
2+
'api/404.sql' AS serving_file,
3+
sqlpage.path() AS request_path;
4+
5+
SELECT 'button' AS component;
6+
SELECT
7+
'Back home' AS title,
8+
'home' AS icon,
9+
'/' AS link;

examples/handle-404/api/index.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
SELECT
2+
'title' AS component,
3+
'Welcome to the API' AS contents;
4+
5+
SELECT 'button' AS component;
6+
SELECT
7+
'Back home' AS title,
8+
'home' AS icon,
9+
'/' AS link;

examples/handle-404/index.sql

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

Comments
 (0)