core/src/opnsense/www/api.php
2024-05-13 10:42:29 +02:00

38 lines
1.2 KiB
PHP

<?php
function error_output($http_code, $e, $user_message)
{
$response = [];
if (!file_exists('/var/run/development')) {
$response['errorMessage'] = $user_message;
} else {
$response['errorMessage'] = $e->getMessage();
$response['errorTrace'] = $e->getTraceAsString();
}
if (method_exists($e, 'getTitle')) {
$response['errorTitle'] = $e->getTitle();
}
header('HTTP', true, $http_code);
header("Content-Type: application/json;charset=utf-8");
echo json_encode($response, JSON_UNESCAPED_SLASHES);
}
try {
$config = include __DIR__ . "/../mvc/app/config/config.php";
include __DIR__ . "/../mvc/app/config/loader.php";
$router = new OPNsense\Mvc\Router('/api/', 'Api');
$response = $router->routeRequest($_SERVER['REQUEST_URI']);
if (!$response->isSent()) {
$response->send();
}
} catch (\OPNsense\Base\UserException $e) {
error_output(500, $e, $e->getMessage());
} catch (\OPNsense\Mvc\Exceptions\DispatchException) {
error_output(404, $e, gettext('Endpoint not found'));
} catch (\Error | \Exception $e) {
error_output(500, $e, gettext('Unexpected error, check log for details'));
error_log($e);
}