catch php errors to return valid JSON when invoking API, refs #1047

Signed-off-by: Michael Kaufmann <d00p@froxlor.org>
This commit is contained in:
Michael Kaufmann 2022-07-22 08:37:19 +02:00
parent cb3be2556b
commit 72d14770de
No known key found for this signature in database
GPG Key ID: C121F97338D7A352
3 changed files with 26 additions and 0 deletions

View File

@ -30,6 +30,12 @@ require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/lib/functions.php';
require __DIR__ . '/lib/tables.inc.php';
// set error-handler
@set_error_handler([
'\\Froxlor\\Api\\Api',
'phpErrHandler'
]);
// Return response
try {
echo (new Api)->formatMiddleware(@file_get_contents('php://input'))->handle();

View File

@ -101,6 +101,20 @@ class Api
return $apiObj->$method();
}
/**
* API PHP error handler to always return a valid JSON response
*
* @param mixed $errno
* @param mixed $errstr
* @param mixed $errfile
* @param mixed $errline
* @return never
*/
public static function phpErrHandler($errno, $errstr, $errfile, $errline)
{
throw new Exception('Internal PHP error: #' . $errno . ' ' . $errstr /* . ' in ' . $errfile . ':' . $errline */, 500);
}
private function stripcslashesDeep($value)
{
return is_array($value) ? array_map([$this, 'stripcslashesDeep'], $value) : stripcslashes($value);

View File

@ -56,6 +56,12 @@ final class RunApiCommand extends CliCommand
require Froxlor::getInstallDir() . '/lib/functions.php';
// set error-handler
@set_error_handler([
'\\Froxlor\\Api\\Api',
'phpErrHandler'
]);
if ($result == self::SUCCESS) {
try {
$loginname = $input->getArgument('user');