Skip to content

Commit c6e0b08

Browse files
committed
UPDATE - Error handler
1 parent b80f34d commit c6e0b08

11 files changed

+126
-24
lines changed

public/index.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
$router->post(['/post'], 'HelloControllerExample#postTest');
2323

24-
// TODO: Group router and error treatment
24+
// TODO: Group router
2525

2626
# Execution of set router
2727
$router->dispatch();

src/Controllers/HelloControllerExample.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use Rodri\SimpleRouter\Request;
88
use Rodri\SimpleRouter\Response;
9-
use Rodri\SimpleRouter\utils\StatusCode;
9+
use Rodri\SimpleRouter\Helpers\StatusCode;
1010

1111
/**
1212
* Class HelloController Example
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
4+
namespace Rodri\SimpleRouter\Exceptions;
5+
6+
7+
class ControllerMethodNotFoundException extends \RuntimeException
8+
{
9+
10+
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
4+
namespace Rodri\SimpleRouter\Exceptions;
5+
6+
7+
use ReflectionException;
8+
9+
class SimpleRouterException extends ReflectionException
10+
{
11+
12+
}

src/Handlers/HttpHandler.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
use JetBrains\PhpStorm\Pure;
88
use Rodri\SimpleRouter\Request;
99
use Rodri\SimpleRouter\Response;
10-
use Rodri\SimpleRouter\utils\ControllerMethod;
11-
use Rodri\SimpleRouter\utils\StatusCode;
10+
use Rodri\SimpleRouter\Helpers\ControllerMethod;
11+
use Rodri\SimpleRouter\Helpers\StatusCode;
1212

1313
/**
1414
* Class HttpHandler - Handler to HTTP REQUESTS

src/Handlers/RouterHandler.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
use ReflectionException;
77
use Rodri\SimpleRouter\Request;
88
use Rodri\SimpleRouter\Response;
9-
use Rodri\SimpleRouter\utils\ControllerMethod;
10-
use Rodri\SimpleRouter\utils\StatusCode;
9+
use Rodri\SimpleRouter\Helpers\ControllerMethod;
10+
use Rodri\SimpleRouter\Helpers\StatusCode;
1111

1212
/**
1313
* Class RouterHandler

src/utils/ControllerMethod.php src/Helpers/ControllerMethod.php

+15-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
<?php
22

33

4-
namespace Rodri\SimpleRouter\utils;
4+
namespace Rodri\SimpleRouter\Helpers;
55

66

77
use ReflectionException;
88
use ReflectionMethod;
9+
use Rodri\SimpleRouter\Exceptions\ControllerMethodNotFoundException;
910
use Rodri\SimpleRouter\Request;
1011
use Rodri\SimpleRouter\Response;
12+
use Rodri\SimpleRouter\utils\Message;
1113

1214
/**
1315
* Class ControllerMethod
@@ -24,24 +26,31 @@ class ControllerMethod
2426
* Instantiate a new Class of Controller execute appropriate method
2527
* @param Request $request
2628
* @return Response|null
27-
* @throws ReflectionException
2829
*/
2930
public function call(Request $request): ?Response
3031
{
31-
return $this->reflectionMethod->invoke(new $this->controller, $request);
32+
try {
33+
return $this->reflectionMethod->invoke(new $this->controller, $request);
34+
} catch (ReflectionException $e) {
35+
throw new ControllerMethodNotFoundException(Message::getError(Message::ERROR_CONTROLLER_NOT_FOUND));
36+
}
3237
}
3338

3439
/**
35-
* Build a new instance of ContrllerMethod
36-
* @throws ReflectionException
40+
* Build a new instance of ControllerMethod
3741
*/
3842
public static function build(String $controller): ControllerMethod
3943
{
4044
$controllerMethod = new ControllerMethod();
4145

4246
$controller = explode('#', $controller);
4347

44-
$controllerMethod->reflectionMethod = new ReflectionMethod($controller[0], $controller[1]);
48+
try {
49+
$controllerMethod->reflectionMethod = new ReflectionMethod($controller[0], $controller[1]);
50+
} catch (ReflectionException $e) {
51+
throw new ControllerMethodNotFoundException(Message::getError(Message::ERROR_CONTROLLER_NOT_FOUND));
52+
}
53+
4554
$controllerMethod->controller = $controller[0];
4655

4756
return $controllerMethod;

src/utils/StatusCode.php src/Helpers/StatusCode.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33

4-
namespace Rodri\SimpleRouter\utils;
4+
namespace Rodri\SimpleRouter\Helpers;
55

66
/**
77
* Class StatusCode - HTTP/1.0 Status code
@@ -50,5 +50,6 @@
5050
class StatusCode
5151
{
5252
public const OK = 'HTTP/1.0 200 OK';
53-
const BAD_REQUEST = 'HTTP/1.0 400 Bad Request';
53+
public const BAD_REQUEST = 'HTTP/1.0 400 Bad Request';
54+
public const INTERNAL_SERVER_ERROR = 'HTTP/1.0 500 Internal Server Error';
5455
}

src/Response.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace Rodri\SimpleRouter;
55

66

7-
use Rodri\SimpleRouter\utils\StatusCode;
7+
use Rodri\SimpleRouter\Helpers\StatusCode;
88

99
/**
1010
* Class Response

src/Router.php

+20-9
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
use Closure;
77
use Exception;
88
use ReflectionException;
9+
use Rodri\SimpleRouter\Exceptions\ControllerMethodNotFoundException;
10+
use Rodri\SimpleRouter\Exceptions\SimpleRouterException;
911
use Rodri\SimpleRouter\Handlers\GroupHttpHandler;
1012
use Rodri\SimpleRouter\Handlers\HttpHandler;
1113
use Rodri\SimpleRouter\Handlers\RouterHandler;
14+
use Rodri\SimpleRouter\Helpers\StatusCode;
1215

1316
/**
1417
* Class Router
@@ -67,7 +70,6 @@ public function headerConfigs(array $configs)
6770
/**
6871
* Add a router handler to chain of responsibility
6972
* @param RouterHandler $routerHandler
70-
* @throws Exception
7173
*/
7274
public function addRouterHandler(RouterHandler $routerHandler): void
7375
{
@@ -91,7 +93,6 @@ public function group(array $routerOptions, Closure $closure): void
9193
* GET
9294
* @param array $routerOptions Router options [0] => '/router' ['middleware' => Middleware
9395
* @param String $controller Controller by pattern Controller#method
94-
* @throws Exception
9596
*/
9697
public function get(array $routerOptions, string $controller): void
9798
{
@@ -108,7 +109,6 @@ public function get(array $routerOptions, string $controller): void
108109
* POST
109110
* @param array $routerOptions Router options [0] => '/router' ['middleware' => Middleware
110111
* @param String $controller Controller by pattern Controller#method
111-
* @throws Exception
112112
*/
113113
public function post(array $routerOptions, string $controller): void
114114
{
@@ -125,7 +125,6 @@ public function post(array $routerOptions, string $controller): void
125125
* PUT
126126
* @param array $routerOptions Router options [0] => '/router' ['middleware' => Middleware
127127
* @param String $controller Controller by pattern Controller#method
128-
* @throws Exception
129128
*/
130129
public function put(array $routerOptions, string $controller): void
131130
{
@@ -142,7 +141,6 @@ public function put(array $routerOptions, string $controller): void
142141
* PATCH
143142
* @param array $routerOptions Router options [0] => '/router' ['middleware' => Middleware
144143
* @param String $controller Controller by pattern Controller#method
145-
* @throws Exception
146144
*/
147145
public function patch(array $routerOptions, string $controller): void
148146
{
@@ -159,7 +157,6 @@ public function patch(array $routerOptions, string $controller): void
159157
* DELETE
160158
* @param array $routerOptions Router options [0] => '/router' ['middleware' => Middleware
161159
* @param String $controller Controller by pattern Controller#method
162-
* @throws Exception
163160
*/
164161
public function delete(array $routerOptions, string $controller): void
165162
{
@@ -174,11 +171,26 @@ public function delete(array $routerOptions, string $controller): void
174171

175172
/**
176173
* Dispatch to router work call the expected handle.
177-
* @throws ReflectionException
178174
*/
179175
public function dispatch()
180176
{
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+
182194
flush();
183195
}
184196

@@ -190,5 +202,4 @@ private function concatControllerAndNamespace(string $controller): string
190202
{
191203
return $this->controllerNamespace . '\\' . $controller;
192204
}
193-
194205
}

src/utils/Message.php

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
4+
namespace Rodri\SimpleRouter\utils;
5+
6+
7+
use PhpParser\Node\Scalar\String_;
8+
9+
class Message
10+
{
11+
12+
private static ?Message $instance = null;
13+
14+
# Attributes
15+
private array $errorMessages;
16+
public const ERROR_CONTROLLER_NOT_FOUND = 0;
17+
public const ERROR_CONTROLLER_METHOD_INVOCATION = 1;
18+
19+
private function __construct()
20+
{
21+
$this->init();
22+
}
23+
24+
/**
25+
* Singleton instance of Message
26+
* @return Message
27+
*/
28+
public static function getInstance(): Message
29+
{
30+
if(!self::$instance)
31+
self::$instance = new Message();
32+
33+
return self::$instance;
34+
}
35+
36+
/**
37+
* Init resources
38+
*/
39+
private function init(): void
40+
{
41+
$this->errorMessages = [
42+
Message::ERROR_CONTROLLER_NOT_FOUND => 'Check if the controller namespace is defined and if has a correspond method.',
43+
Message::ERROR_CONTROLLER_METHOD_INVOCATION => 'Is not possible invoke the controller method, maybe some param is expected.'
44+
];
45+
}
46+
47+
/**
48+
* @param int $errorCode
49+
* @return string
50+
*/
51+
public static function getError(int $errorCode): string
52+
{
53+
$message = self::getInstance();
54+
if(!isset($message->errorMessages[$errorCode]))
55+
return 'Not defined';
56+
57+
return $message->errorMessages[$errorCode];
58+
}
59+
}

0 commit comments

Comments
 (0)