(mvc, api) add error handling for api calls, https://github.com/opnsense/core/issues/1136

This commit is contained in:
Ad Schellevis 2016-08-14 14:23:43 +02:00
parent 720acc9e1d
commit 9ed6406bdc
2 changed files with 17 additions and 1 deletions

View File

@ -57,6 +57,17 @@ class ApiControllerBase extends ControllerRoot
return null;
}
/**
* Raise errors, warnings, notices, etc.
* @param $errno
* @param $errstr
* @throws \Exception
*/
public function APIErrorHandler($errno, $errstr)
{
throw new \Exception($errstr . ' ['.$errno.']');
}
/**
* Initialize API controller
*/
@ -64,6 +75,7 @@ class ApiControllerBase extends ControllerRoot
{
// disable view processing
$this->view->disable();
set_error_handler(array($this, 'APIErrorHandler'));
}
/**

View File

@ -26,5 +26,9 @@ try {
echo $application->handle()->getContent();
} catch (\Exception $e) {
echo $e->getMessage();
$response = array();
$response['errorMessage'] = $e->getMessage();
header("Content-Type: application/json;charset=utf-8");
echo htmlspecialchars(json_encode($response), ENT_NOQUOTES);
error_log($e);
}