@@ -2823,16 +2823,18 @@ class SimpleRouter implements Router
2823
2823
private $ responder ;
2824
2824
private $ cache ;
2825
2825
private $ ttl ;
2826
+ private $ debug ;
2826
2827
private $ registration ;
2827
2828
private $ routes ;
2828
2829
private $ routeHandlers ;
2829
2830
private $ middlewares ;
2830
2831
2831
- public function __construct (Responder $ responder , Cache $ cache , int $ ttl )
2832
+ public function __construct (Responder $ responder , Cache $ cache , int $ ttl, bool $ debug )
2832
2833
{
2833
2834
$ this ->responder = $ responder ;
2834
2835
$ this ->cache = $ cache ;
2835
2836
$ this ->ttl = $ ttl ;
2837
+ $ this ->debug = $ debug ;
2836
2838
$ this ->registration = true ;
2837
2839
$ this ->routes = $ this ->loadPathTree ();
2838
2840
$ this ->routeHandlers = [];
@@ -2900,7 +2902,23 @@ public function handle(Request $request): Response
2900
2902
if (count ($ routeNumbers ) == 0 ) {
2901
2903
return $ this ->responder ->error (ErrorCode::ROUTE_NOT_FOUND , $ request ->getPath ());
2902
2904
}
2903
- return call_user_func ($ this ->routeHandlers [$ routeNumbers [0 ]], $ request );
2905
+ try {
2906
+ $ response = call_user_func ($ this ->routeHandlers [$ routeNumbers [0 ]], $ request );
2907
+ } catch (\PDOException $ e ) {
2908
+ if (strpos (strtolower ($ e ->getMessage ()), 'duplicate ' ) !== false ) {
2909
+ $ response = $ this ->responder ->error (ErrorCode::DUPLICATE_KEY_EXCEPTION , '' );
2910
+ } elseif (strpos (strtolower ($ e ->getMessage ()), 'default value ' ) !== false ) {
2911
+ $ response = $ this ->responder ->error (ErrorCode::DATA_INTEGRITY_VIOLATION , '' );
2912
+ } elseif (strpos (strtolower ($ e ->getMessage ()), 'allow nulls ' ) !== false ) {
2913
+ $ response = $ this ->responder ->error (ErrorCode::DATA_INTEGRITY_VIOLATION , '' );
2914
+ } elseif (strpos (strtolower ($ e ->getMessage ()), 'constraint ' ) !== false ) {
2915
+ $ response = $ this ->responder ->error (ErrorCode::DATA_INTEGRITY_VIOLATION , '' );
2916
+ }
2917
+ if ($ this ->debug ) {
2918
+ $ response ->addExceptionHeaders ($ e );
2919
+ }
2920
+ }
2921
+ return $ response ;
2904
2922
}
2905
2923
2906
2924
}
@@ -5355,7 +5373,7 @@ public function __construct(Config $config)
5355
5373
$ cache = CacheFactory::create ($ config );
5356
5374
$ reflection = new ReflectionService ($ db , $ cache , $ config ->getCacheTime ());
5357
5375
$ responder = new Responder ();
5358
- $ router = new SimpleRouter ($ responder , $ cache , $ config ->getCacheTime ());
5376
+ $ router = new SimpleRouter ($ responder , $ cache , $ config ->getCacheTime (), $ config -> getDebug () );
5359
5377
foreach ($ config ->getMiddlewares () as $ middleware => $ properties ) {
5360
5378
switch ($ middleware ) {
5361
5379
case 'cors ' :
@@ -5429,23 +5447,9 @@ public function handle(Request $request): Response
5429
5447
try {
5430
5448
$ response = $ this ->router ->route ($ request );
5431
5449
} catch (\Throwable $ e ) {
5432
- if ($ e instanceof \PDOException) {
5433
- if (strpos (strtolower ($ e ->getMessage ()), 'duplicate ' ) !== false ) {
5434
- $ response = $ this ->responder ->error (ErrorCode::DUPLICATE_KEY_EXCEPTION , '' );
5435
- } elseif (strpos (strtolower ($ e ->getMessage ()), 'default value ' ) !== false ) {
5436
- $ response = $ this ->responder ->error (ErrorCode::DATA_INTEGRITY_VIOLATION , '' );
5437
- } elseif (strpos (strtolower ($ e ->getMessage ()), 'allow nulls ' ) !== false ) {
5438
- $ response = $ this ->responder ->error (ErrorCode::DATA_INTEGRITY_VIOLATION , '' );
5439
- } elseif (strpos (strtolower ($ e ->getMessage ()), 'constraint ' ) !== false ) {
5440
- $ response = $ this ->responder ->error (ErrorCode::DATA_INTEGRITY_VIOLATION , '' );
5441
- }
5442
- }
5443
- if (!$ response ) {
5444
- $ response = $ this ->responder ->error (ErrorCode::ERROR_NOT_FOUND , $ e ->getMessage ());
5445
- }
5450
+ $ response = $ this ->responder ->error (ErrorCode::ERROR_NOT_FOUND , $ e ->getMessage ());
5446
5451
if ($ this ->debug ) {
5447
- $ response ->addHeader ('X-Exception-Message ' , $ e ->getMessage ());
5448
- $ response ->addHeader ('X-Exception-File ' , $ e ->getFile () . ': ' . $ e ->getLine ());
5452
+ $ response ->addExceptionHeaders ($ e );
5449
5453
}
5450
5454
}
5451
5455
return $ response ;
@@ -5871,6 +5875,13 @@ public function output()
5871
5875
echo $ this ->getBody ();
5872
5876
}
5873
5877
5878
+ public function addExceptionHeaders (\Throwable $ e )
5879
+ {
5880
+ $ this ->addHeader ('X-Exception-Name ' , get_class ($ e ));
5881
+ $ this ->addHeader ('X-Exception-Message ' , $ e ->getMessage ());
5882
+ $ this ->addHeader ('X-Exception-File ' , $ e ->getFile () . ': ' . $ e ->getLine ());
5883
+ }
5884
+
5874
5885
public function __toString (): String
5875
5886
{
5876
5887
$ str = "$ this ->status \n" ;
0 commit comments