Skip to content

Commit 36abcc1

Browse files
committed
feat(plugins): added functional actions for exporting error pages
1 parent 9ee6904 commit 36abcc1

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

docs/next/en-US/plugins/functional.md

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ If you'd like to request that a new action, functional or control, be added, ple
2626
- `after_failed_static_alias_dir_copy` -- runs arbitrary code if the export process fails to copy a static alias that was a directory (e.g. to report the failed export to a server crash management system)
2727
- `after_failed_static_alias_file_copy` -- runs arbitrary code if the export process fails to copy a static alias that was a file (e.g. to report the failed export to a server crash management system)
2828
- `after_successful_export` -- runs arbitrary code after the export process has completed, if it was successful (e.g. copying custom files into `.perseus/dist/`)
29+
- `export_error_page_actions` --- actions that'll be run when exporting an error page
30+
- `before_export_error_page` --- runs arbitrary code before this process has started (providing the error code to be exported for and the output file)
31+
- `after_successful_export_error_page` -- runs arbitrary code after this process has completed, if it was successful
32+
- `after_failed_write` -- runs arbitrary code after this process has completed, if it couldn't write to the target output file
2933
- `server_actions` -- actions that'll be run as part of the Perseus server when the user runs `perseus serve` (or when a [serverful production deployment](:deploying/serverful) runs)
3034
- `before_serve` -- runs arbitrary code before the server starts (e.g. to spawn an API server)
3135
- `client_actions` -- actions that'll run in the browser when the user's app is accessed

examples/basic/.perseus/builder/src/bin/export_error_page.rs

+17-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ async fn real_main() -> i32 {
2020
env::set_current_dir("../").unwrap();
2121

2222
let plugins = get_plugins::<SsrNode>();
23+
2324
let error_pages = get_error_pages(&plugins);
2425
let root_id = get_app_root(&plugins);
2526
let immutable_store = get_immutable_store(&plugins);
@@ -62,6 +63,14 @@ async fn real_main() -> i32 {
6263
return 1;
6364
}
6465
};
66+
plugins
67+
.functional_actions
68+
.export_error_page_actions
69+
.before_export_error_page
70+
.run(
71+
(err_code_to_build_for, output.to_string()),
72+
plugins.get_plugin_data(),
73+
);
6574
// Build that error page as the server does
6675
let err_page_str = build_error_page(
6776
"",
@@ -76,18 +85,23 @@ async fn real_main() -> i32 {
7685
// Write that to the mandatory second argument (the output location)
7786
// We'll move out of `.perseus/` first though
7887
env::set_current_dir("../").unwrap();
79-
match fs::write(output, err_page_str) {
88+
match fs::write(&output, err_page_str) {
8089
Ok(_) => (),
8190
Err(err) => {
8291
eprintln!("{}", fmt_err(&err));
92+
plugins
93+
.functional_actions
94+
.export_error_page_actions
95+
.after_failed_write
96+
.run((err, output.to_string()), plugins.get_plugin_data());
8397
return 1;
8498
}
8599
};
86100

87101
plugins
88102
.functional_actions
89-
.export_actions
90-
.after_successful_export
103+
.export_error_page_actions
104+
.after_successful_export_error_page
91105
.run((), plugins.get_plugin_data());
92106
println!("Static exporting successfully completed!");
93107
0

packages/perseus/src/plugins/functional.rs

+13
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ pub struct FunctionalPluginActions<G: Html> {
6464
pub build_actions: FunctionalPluginBuildActions,
6565
/// Actions pertaining to the export process.
6666
pub export_actions: FunctionalPluginExportActions,
67+
/// Actions pertaining to the process of exporting an error page.
68+
pub export_error_page_actions: FunctionalPluginExportErrorPageActions,
6769
/// Actions pertaining to the server.
6870
pub server_actions: FunctionalPluginServerActions,
6971
/// Actions pertaining to the client-side code.
@@ -76,6 +78,7 @@ impl<G: Html> Default for FunctionalPluginActions<G> {
7678
settings_actions: FunctionalPluginSettingsActions::<G>::default(),
7779
build_actions: FunctionalPluginBuildActions::default(),
7880
export_actions: FunctionalPluginExportActions::default(),
81+
export_error_page_actions: FunctionalPluginExportErrorPageActions::default(),
7982
server_actions: FunctionalPluginServerActions::default(),
8083
client_actions: FunctionalPluginClientActions::default(),
8184
}
@@ -139,6 +142,16 @@ pub struct FunctionalPluginExportActions {
139142
/// Runs after the export process if it completes successfully.
140143
pub after_successful_export: FunctionalPluginAction<(), ()>,
141144
}
145+
/// Functional actions that pertain to the process of exporting an error page.
146+
#[derive(Default)]
147+
pub struct FunctionalPluginExportErrorPageActions {
148+
/// Runs before the process of exporting an error page, providing the HTTP status code to be exported and the output filename (relative to the root of the project, not to `.perseus/`).
149+
pub before_export_error_page: FunctionalPluginAction<(u16, String), ()>,
150+
/// Runs after a error page was exported successfully.
151+
pub after_successful_export_error_page: FunctionalPluginAction<(), ()>,
152+
/// Runs if writing to the output file failed. Error and filename are given.
153+
pub after_failed_write: FunctionalPluginAction<(std::io::Error, String), ()>,
154+
}
142155
/// Functional actions that pertain to the server.
143156
#[derive(Default)]
144157
pub struct FunctionalPluginServerActions {

0 commit comments

Comments
 (0)