Skip to content

Commit 4d7a598

Browse files
fix: improve error handling during package updates #633 (#643)
* style: run prettier on changed files * docs: add info about unavailability during updates #633 * fix: give friendlier error messages during updates #633 Before, access REST API resources resulted in a PHP error traceback which could cause unnecessary concern. This change will allow both endpoints and forms to give a friendlier, more informative notice. * fix: correctly set http response code for 503s during updates
1 parent f9102dd commit 4d7a598

File tree

4 files changed

+166
-162
lines changed

4 files changed

+166
-162
lines changed

composer.lock

Lines changed: 140 additions & 150 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pfSense-pkg-RESTAPI/files/usr/local/pkg/RESTAPI/Core/Endpoint.inc

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1199,9 +1199,20 @@ class Endpoint {
11991199
$nq_class_name = $this->get_class_shortname();
12001200

12011201
# Specify the PHP code to write to the Endpoints index.php file
1202+
$unavailable_error = new ServiceUnavailableError(
1203+
message: 'This resource is either not installed or is currently updating. Please try again later.',
1204+
response_id: 'ENDPOINT_UNAVAILABLE',
1205+
);
1206+
$unavailable_error_json = json_encode($unavailable_error->to_representation());
12021207
$code =
12031208
"<?php\n" .
1204-
"require_once('RESTAPI/Endpoints/$nq_class_name.inc');\n" .
1209+
"\$include = include('RESTAPI/Endpoints/$nq_class_name.inc');\n" .
1210+
"if (!\$include) {\n" .
1211+
" header('Content-Type: application/json');\n" .
1212+
" http_response_code(503);\n" .
1213+
" echo '$unavailable_error_json';\n" .
1214+
" exit(503);\n" .
1215+
"}\n" .
12051216
"echo (new $fq_class_name())->process_request();\n" .
12061217
"header('Referer: no-referrer');\n" .
12071218
"session_destroy();\n" .

pfSense-pkg-RESTAPI/files/usr/local/pkg/RESTAPI/Core/Form.inc

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -529,14 +529,16 @@ class Form {
529529

530530
# Specify the PHP code to write to the Endpoints index.php file
531531
$code =
532-
"<?php
533-
require_once('RESTAPI/Forms/" .
534-
$nq_class_name .
535-
".inc');
536-
require_once('guiconfig.inc');
537-
(new " .
538-
$fq_class_name .
539-
'())->print_form();';
532+
"<?php\n" .
533+
"require_once('guiconfig.inc');\n" .
534+
"\$include = include('RESTAPI/Forms/$nq_class_name.inc');\n" .
535+
"if (!\$include) {\n" .
536+
" http_response_code(503);\n" .
537+
" echo '<h1>Service Unavailable</h1>';\n" .
538+
" echo 'This resource is either not installed or is currently updating. Please try again later.';\n" .
539+
" exit();\n" .
540+
"}\n" .
541+
"(new $fq_class_name())->print_form();\n";
540542

541543
# Assign the absolute path to the file. Assume index.php filename if not specified.
542544
$filename = "/usr/local/www/$this->url";
@@ -548,7 +550,6 @@ class Form {
548550
if (is_file($filename)) {
549551
return true;
550552
}
551-
552553
return false;
553554
}
554555
}

pfSense-pkg-RESTAPI/files/usr/local/pkg/RESTAPI/Forms/SystemRESTAPIUpdatesForm.inc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ class SystemRESTAPIUpdatesForm extends Form {
3131

3232
public function on_save(string $success_banner_msg = ''): void {
3333
parent::on_save(
34-
success_banner_msg: 'The requested version is being installed in the background. Check this page again ' .
35-
'later to see the status.',
34+
success_banner_msg: 'The requested version is being installed in the background. During the update, the ' .
35+
"REST API may be intermittently unavailable. Attempts to access the REST API's endpoints and web " .
36+
'pages during the update may result in errors until it completes. Check this page again later to see ' .
37+
'the status.',
3638
);
3739
}
3840
}

0 commit comments

Comments
 (0)