Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
O
OpnSense
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kulya
OpnSense
Commits
734c3cd7
Commit
734c3cd7
authored
Nov 10, 2015
by
Ad Schellevis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(mvc) api error handling (missing controller, missing action)
parent
bbaccbb3
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
59 additions
and
16 deletions
+59
-16
services_api.php
src/opnsense/mvc/app/config/services_api.php
+32
-1
ApiControllerBase.php
...e/mvc/app/controllers/OPNsense/Base/ApiControllerBase.php
+6
-15
IndexController.php
...nse/mvc/app/controllers/OPNsense/Base/IndexController.php
+21
-0
No files found.
src/opnsense/mvc/app/config/services_api.php
View file @
734c3cd7
...
...
@@ -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'
));
src/opnsense/mvc/app/controllers/OPNsense/Base/ApiControllerBase.php
View file @
734c3cd7
...
...
@@ -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
(
...
...
src/opnsense/mvc/app/controllers/OPNsense/Base/IndexController.php
View file @
734c3cd7
...
...
@@ -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
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment