mirror of
https://github.com/lucaspalomodevelop/core.git
synced 2026-03-13 16:14:40 +00:00
(mvc) api error handling (missing controller, missing action)
This commit is contained in:
parent
bbaccbb332
commit
734c3cd729
@ -79,7 +79,7 @@ $di->set('router', function () {
|
||||
|
||||
$router->setDefaultController('index');
|
||||
$router->setDefaultAction('index');
|
||||
$router->setDefaultNamespace('OPNsense\Sample\Api');
|
||||
$router->setDefaultNamespace('OPNsense\Base');
|
||||
|
||||
$router->add('/', array(
|
||||
"controller" => 'index',
|
||||
@ -142,3 +142,34 @@ $di->set('router', function () {
|
||||
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
|
||||
$this->view->disable();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* before routing event.
|
||||
* Handles authentication and authentication of user requests
|
||||
@ -97,20 +96,12 @@ class ApiControllerBase extends ControllerRoot
|
||||
// authentication + authorization successful.
|
||||
// pre validate request and communicate back to the user on errors
|
||||
$callMethodName = $dispatcher->getActionName().'Action';
|
||||
$dispatchError = null;
|
||||
if (!method_exists($this, $callMethodName)) {
|
||||
// can not execute, method not found
|
||||
$dispatchError = 'action ' . $dispatcher->getActionName() . ' not found';
|
||||
} else {
|
||||
// check number of parameters using reflection
|
||||
$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) {
|
||||
// check number of parameters using reflection
|
||||
$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)';
|
||||
$this->response->setStatusCode(400, "Bad Request");
|
||||
$this->response->setContentType('application/json', 'UTF-8');
|
||||
$this->response->setJsonContent(
|
||||
|
||||
@ -41,4 +41,25 @@ class IndexController extends ControllerBase
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user