Commit 734c3cd7 authored by Ad Schellevis's avatar Ad Schellevis

(mvc) api error handling (missing controller, missing action)

parent bbaccbb3
...@@ -79,7 +79,7 @@ $di->set('router', function () { ...@@ -79,7 +79,7 @@ $di->set('router', function () {
$router->setDefaultController('index'); $router->setDefaultController('index');
$router->setDefaultAction('index'); $router->setDefaultAction('index');
$router->setDefaultNamespace('OPNsense\Sample\Api'); $router->setDefaultNamespace('OPNsense\Base');
$router->add('/', array( $router->add('/', array(
"controller" => 'index', "controller" => 'index',
...@@ -142,3 +142,34 @@ $di->set('router', function () { ...@@ -142,3 +142,34 @@ $di->set('router', function () {
return $router; return $router;
}); });
// exception handling
$di->get('eventsManager')->attach("dispatch:beforeException", function($event, $dispatcher, $exception) {
switch ($exception->getCode()) {
case Phalcon\Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
// send to error action on default index controller
$dispatcher->forward(array(
'controller' => 'index',
'namespace' => '\OPNsense\Base',
'action' => 'handleError',
'params' => array(
'message' => 'controller ' . $dispatcher->getControllerClass() . ' not found',
'sender' => 'API'
)
));
return false;
case Phalcon\Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
// send to error action on default index controller
$dispatcher->forward(array(
'controller' => 'index',
'namespace' => '\OPNsense\Base',
'action' => 'handleError',
'params' => array(
'message' => 'action ' . $dispatcher->getActionName() . ' not found',
'sender' => 'API'
)
));
return false;
}
});
$di->get('dispatcher')->setEventsManager($di->get('eventsManager'));
...@@ -60,7 +60,6 @@ class ApiControllerBase extends ControllerRoot ...@@ -60,7 +60,6 @@ class ApiControllerBase extends ControllerRoot
$this->view->disable(); $this->view->disable();
} }
/** /**
* before routing event. * before routing event.
* Handles authentication and authentication of user requests * Handles authentication and authentication of user requests
...@@ -97,20 +96,12 @@ class ApiControllerBase extends ControllerRoot ...@@ -97,20 +96,12 @@ class ApiControllerBase extends ControllerRoot
// authentication + authorization successful. // authentication + authorization successful.
// pre validate request and communicate back to the user on errors // pre validate request and communicate back to the user on errors
$callMethodName = $dispatcher->getActionName().'Action'; $callMethodName = $dispatcher->getActionName().'Action';
$dispatchError = null; // check number of parameters using reflection
if (!method_exists($this, $callMethodName)) { $object_info = new \ReflectionObject($this);
// can not execute, method not found $req_c = $object_info->getMethod($callMethodName)->getNumberOfRequiredParameters();
$dispatchError = 'action ' . $dispatcher->getActionName() . ' not found'; if ($req_c > count($dispatcher->getParams())) {
} else { $dispatchError = 'action ' . $dispatcher->getActionName() .
// check number of parameters using reflection ' expects at least '. $req_c . ' parameter(s)';
$object_info = new \ReflectionObject($this);
$req_c = $object_info->getMethod($callMethodName)->getNumberOfRequiredParameters();
if ($req_c > count($dispatcher->getParams())) {
$dispatchError = 'action ' . $dispatcher->getActionName() .
' expects at least '. $req_c . ' parameter(s)';
}
}
if ($dispatchError != null) {
$this->response->setStatusCode(400, "Bad Request"); $this->response->setStatusCode(400, "Bad Request");
$this->response->setContentType('application/json', 'UTF-8'); $this->response->setContentType('application/json', 'UTF-8');
$this->response->setJsonContent( $this->response->setJsonContent(
......
...@@ -41,4 +41,25 @@ class IndexController extends ControllerBase ...@@ -41,4 +41,25 @@ class IndexController extends ControllerBase
public function indexAction() public function indexAction()
{ {
} }
/**
* log or send error message
* @param string $message error message
*/
public function handleErrorAction($message = null, $sender = null)
{
// API call, send error to user
if ($sender == 'API') {
$this->response->setStatusCode(400, "Bad Request");
$this->response->setContentType('application/json', 'UTF-8');
$this->response->setJsonContent(
array('message' => $message,
'status' => 400
));
} else {
$this->getLogger()->error($message);
$this->response->redirect("/", true);
}
return false;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment