Skip to content

Commit c181ed6

Browse files
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.
1 parent 21188aa commit c181ed6

2 files changed

Lines changed: 19 additions & 9 deletions

File tree

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1199,9 +1199,18 @@ 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+
" echo '$unavailable_error_json';\n" .
1212+
" exit(503);\n" .
1213+
"}\n" .
12051214
"echo (new $fq_class_name())->process_request();\n" .
12061215
"header('Referer: no-referrer');\n" .
12071216
"session_destroy();\n" .

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -529,14 +529,15 @@ 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+
" echo '<h1>Service Unavailable</h1>';\n" .
537+
" echo 'This resource is either not installed or is currently updating. Please try again later.';\n" .
538+
" exit(503);\n" .
539+
"}\n" .
540+
"(new $fq_class_name())->print_form();\n";
540541

541542
# Assign the absolute path to the file. Assume index.php filename if not specified.
542543
$filename = "/usr/local/www/$this->url";

0 commit comments

Comments
 (0)