-
Notifications
You must be signed in to change notification settings - Fork 143
feat: add 404.sql handler #544
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
SELECT 'debug' AS component, | ||
'api/404.sql' AS serving_file, | ||
sqlpage.path() AS request_path; | ||
|
||
SELECT 'button' AS component; | ||
SELECT | ||
'Back home' AS title, | ||
'home' AS icon, | ||
'/' AS link; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
SELECT | ||
'title' AS component, | ||
'Welcome to the API' AS contents; | ||
|
||
SELECT 'button' AS component; | ||
SELECT | ||
'Back home' AS title, | ||
'home' AS icon, | ||
'/' AS link; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
SELECT 'list' AS component, | ||
'Navigation' AS title; | ||
|
||
SELECT | ||
column1 AS title, column2 AS link, column3 AS description_md | ||
FROM (VALUES | ||
('Link to arbitrary path', '/api/does/not/actually/exist', 'Covered by `api/404.sql`'), | ||
('Link to arbitrary file', '/api/nothing.png', 'Covered by `api/404.sql`'), | ||
('Link to non-existing .sql file', '/api/inexistent.sql', 'Covered by `api/404.sql`'), | ||
('Link to 404 handler', '/api/404.sql', 'Actually `api/404.sql`'), | ||
('Link to API landing page', '/api', 'Covered by `api/index.sql`'), | ||
('Link to arbitrary broken path', '/backend/does/not/actually/exist', 'Not covered by anything, will yield a 404 error') | ||
); | ||
|
||
SELECT 'text' AS component, | ||
' | ||
# Overview | ||
|
||
This demo shows how a `404.sql` file can serve as a fallback error handler. Whenever a `404 Not | ||
Found` error would be emitted, instead a dedicated `404.sql` is called (if it exists) to serve the | ||
request. This is usefull in two scenarios: | ||
|
||
1. Providing custom 404 error pages. | ||
2. To provide content under dynamic paths. | ||
|
||
The former use-case is primarily of cosmetic nature, it allows for more informative, customized | ||
failure modes, enabling better UX. The latter use-case opens the door especially for REST API | ||
design, where dynamic paths are often used to convey arguments, i.e. `/api/resource/5` where `5` is | ||
the id of a resource. | ||
|
||
|
||
# Fallback Handler Selection | ||
|
||
When a normal request to either a `.sql` or a static file fails with `404`, the `404` error is | ||
intercepted. The reuquest path''s target directory is scanned for a `404.sql`. If it exists, it is | ||
called. Otherwise, the parent directory is scanned for `404.sql`, which will be called if it exists. | ||
This search traverses up until it reaches the `web_root`. If even the webroot does not contain a | ||
`404.sql`, then the original `404` error is served as response to the HTTP client. | ||
|
||
The fallback handler is not recursive; i.e. if anything causes another `404` during the call to a | ||
`404.sql`, then the request fails (emitting a `404` response to the HTTP client). | ||
' AS contents_md; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
SELECT 'alert' AS component, | ||
'We almost got an oopsie' AS title, | ||
'But the `404.sql` file saved the day!' AS description_md; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.