From 7863c124c077de9d575a095fb4cb61f720fd9fa4 Mon Sep 17 00:00:00 2001 From: Per von Zweigbergk Date: Sun, 14 Aug 2016 13:06:33 +0000 Subject: [PATCH 1/2] (mvc, api) improve error handling for api calls Example before: {"errorMessage":"Undefined offset: 0 [8]"} Example after: {"errorMessage":"Error at \/usr\/local\/etc\/inc\/util.inc:952 - Undefined offset: 0 (errno=8)"} --- .../OPNsense/Base/ApiControllerBase.php | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/opnsense/mvc/app/controllers/OPNsense/Base/ApiControllerBase.php b/src/opnsense/mvc/app/controllers/OPNsense/Base/ApiControllerBase.php index b9cfbb675..19126ab1a 100644 --- a/src/opnsense/mvc/app/controllers/OPNsense/Base/ApiControllerBase.php +++ b/src/opnsense/mvc/app/controllers/OPNsense/Base/ApiControllerBase.php @@ -59,13 +59,29 @@ class ApiControllerBase extends ControllerRoot /** * Raise errors, warnings, notices, etc. - * @param $errno - * @param $errstr + * @param $errno The first parameter, errno, contains the level of the + * error raised, as an integer. + * @param $errstr The second parameter, errstr, contains the error + * message, as a string. + * @param $errfile The third parameter is optional, errfile, which + * contains the filename that the error was raised in, as + * a string. + * @param $errline The fourth parameter is optional, errline, which + * contains the line number the error was raised at, as an + * integer. + * @param $errcontext The fifth parameter is optional, errcontext, which + * is an array that points to the active symbol table + * at the point the error occurred. In other words, + * errcontext will contain an array of every variable + * that existed in the scope the error was triggered + * in. User error handler must not modify error + * context. * @throws \Exception */ - public function APIErrorHandler($errno, $errstr) + public function APIErrorHandler($errno, $errstr, $errfile, $errline, $errcontext) { - throw new \Exception($errstr . ' ['.$errno.']'); + $msg = "Error at $errfile:$errline - $errstr (errno=$errno)"; + throw new \Exception($msg); } /** From 6839fe81acf2f420d337da2bcfba90828c08527a Mon Sep 17 00:00:00 2001 From: Per von Zweigbergk Date: Sun, 14 Aug 2016 13:21:30 +0000 Subject: [PATCH 2/2] (mvc, api) Removed useless escaping --- src/opnsense/www/api.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/opnsense/www/api.php b/src/opnsense/www/api.php index b0b7deca2..07641cfe8 100644 --- a/src/opnsense/www/api.php +++ b/src/opnsense/www/api.php @@ -29,6 +29,6 @@ try { $response = array(); $response['errorMessage'] = $e->getMessage(); header("Content-Type: application/json;charset=utf-8"); - echo htmlspecialchars(json_encode($response), ENT_NOQUOTES); + echo json_encode($response, JSON_UNESCAPED_SLASHES); error_log($e); }