6
6
use Closure ;
7
7
use Exception ;
8
8
use ReflectionException ;
9
+ use Rodri \SimpleRouter \Exceptions \ControllerMethodNotFoundException ;
10
+ use Rodri \SimpleRouter \Exceptions \SimpleRouterException ;
9
11
use Rodri \SimpleRouter \Handlers \GroupHttpHandler ;
10
12
use Rodri \SimpleRouter \Handlers \HttpHandler ;
11
13
use Rodri \SimpleRouter \Handlers \RouterHandler ;
14
+ use Rodri \SimpleRouter \Helpers \StatusCode ;
12
15
13
16
/**
14
17
* Class Router
@@ -67,7 +70,6 @@ public function headerConfigs(array $configs)
67
70
/**
68
71
* Add a router handler to chain of responsibility
69
72
* @param RouterHandler $routerHandler
70
- * @throws Exception
71
73
*/
72
74
public function addRouterHandler (RouterHandler $ routerHandler ): void
73
75
{
@@ -91,7 +93,6 @@ public function group(array $routerOptions, Closure $closure): void
91
93
* GET
92
94
* @param array $routerOptions Router options [0] => '/router' ['middleware' => Middleware
93
95
* @param String $controller Controller by pattern Controller#method
94
- * @throws Exception
95
96
*/
96
97
public function get (array $ routerOptions , string $ controller ): void
97
98
{
@@ -108,7 +109,6 @@ public function get(array $routerOptions, string $controller): void
108
109
* POST
109
110
* @param array $routerOptions Router options [0] => '/router' ['middleware' => Middleware
110
111
* @param String $controller Controller by pattern Controller#method
111
- * @throws Exception
112
112
*/
113
113
public function post (array $ routerOptions , string $ controller ): void
114
114
{
@@ -125,7 +125,6 @@ public function post(array $routerOptions, string $controller): void
125
125
* PUT
126
126
* @param array $routerOptions Router options [0] => '/router' ['middleware' => Middleware
127
127
* @param String $controller Controller by pattern Controller#method
128
- * @throws Exception
129
128
*/
130
129
public function put (array $ routerOptions , string $ controller ): void
131
130
{
@@ -142,7 +141,6 @@ public function put(array $routerOptions, string $controller): void
142
141
* PATCH
143
142
* @param array $routerOptions Router options [0] => '/router' ['middleware' => Middleware
144
143
* @param String $controller Controller by pattern Controller#method
145
- * @throws Exception
146
144
*/
147
145
public function patch (array $ routerOptions , string $ controller ): void
148
146
{
@@ -159,7 +157,6 @@ public function patch(array $routerOptions, string $controller): void
159
157
* DELETE
160
158
* @param array $routerOptions Router options [0] => '/router' ['middleware' => Middleware
161
159
* @param String $controller Controller by pattern Controller#method
162
- * @throws Exception
163
160
*/
164
161
public function delete (array $ routerOptions , string $ controller ): void
165
162
{
@@ -174,11 +171,26 @@ public function delete(array $routerOptions, string $controller): void
174
171
175
172
/**
176
173
* Dispatch to router work call the expected handle.
177
- * @throws ReflectionException
178
174
*/
179
175
public function dispatch ()
180
176
{
181
- echo $ this ->baseRouterHandler ->handle (new Request ());
177
+ try {
178
+ echo $ this ->baseRouterHandler ->handle (new Request ());
179
+ } catch (ControllerMethodNotFoundException |ReflectionException $ e ) {
180
+ if ($ this ->debugMode ) {
181
+ echo new Response ([
182
+ 'Mode ' => 'Debug ' ,
183
+ 'error ' => 'ControllerMethodNotFoundException ' ,
184
+ 'message ' => $ e ->getMessage (),
185
+ 'line ' => $ e ->getLine (),
186
+ 'file ' => $ e ->getFile (),
187
+ 'trace ' => $ e ->getTrace ()
188
+ ], StatusCode::INTERNAL_SERVER_ERROR );
189
+ } else {
190
+ echo new Response (null , StatusCode::INTERNAL_SERVER_ERROR );
191
+ }
192
+ }
193
+
182
194
flush ();
183
195
}
184
196
@@ -190,5 +202,4 @@ private function concatControllerAndNamespace(string $controller): string
190
202
{
191
203
return $ this ->controllerNamespace . '\\' . $ controller ;
192
204
}
193
-
194
205
}
0 commit comments