Skip to content

Commit 9a37047

Browse files
committed
Fix for #523
1 parent d24087e commit 9a37047

File tree

4 files changed

+60
-38
lines changed

4 files changed

+60
-38
lines changed

api.php

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2823,16 +2823,18 @@ class SimpleRouter implements Router
28232823
private $responder;
28242824
private $cache;
28252825
private $ttl;
2826+
private $debug;
28262827
private $registration;
28272828
private $routes;
28282829
private $routeHandlers;
28292830
private $middlewares;
28302831

2831-
public function __construct(Responder $responder, Cache $cache, int $ttl)
2832+
public function __construct(Responder $responder, Cache $cache, int $ttl, bool $debug)
28322833
{
28332834
$this->responder = $responder;
28342835
$this->cache = $cache;
28352836
$this->ttl = $ttl;
2837+
$this->debug = $debug;
28362838
$this->registration = true;
28372839
$this->routes = $this->loadPathTree();
28382840
$this->routeHandlers = [];
@@ -2900,7 +2902,23 @@ public function handle(Request $request): Response
29002902
if (count($routeNumbers) == 0) {
29012903
return $this->responder->error(ErrorCode::ROUTE_NOT_FOUND, $request->getPath());
29022904
}
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;
29042922
}
29052923

29062924
}
@@ -5355,7 +5373,7 @@ public function __construct(Config $config)
53555373
$cache = CacheFactory::create($config);
53565374
$reflection = new ReflectionService($db, $cache, $config->getCacheTime());
53575375
$responder = new Responder();
5358-
$router = new SimpleRouter($responder, $cache, $config->getCacheTime());
5376+
$router = new SimpleRouter($responder, $cache, $config->getCacheTime(), $config->getDebug());
53595377
foreach ($config->getMiddlewares() as $middleware => $properties) {
53605378
switch ($middleware) {
53615379
case 'cors':
@@ -5429,23 +5447,9 @@ public function handle(Request $request): Response
54295447
try {
54305448
$response = $this->router->route($request);
54315449
} 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());
54465451
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);
54495453
}
54505454
}
54515455
return $response;
@@ -5871,6 +5875,13 @@ public function output()
58715875
echo $this->getBody();
58725876
}
58735877

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+
58745885
public function __toString(): String
58755886
{
58765887
$str = "$this->status\n";

src/Tqdev/PhpCrudApi/Api.php

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function __construct(Config $config)
4747
$cache = CacheFactory::create($config);
4848
$reflection = new ReflectionService($db, $cache, $config->getCacheTime());
4949
$responder = new Responder();
50-
$router = new SimpleRouter($responder, $cache, $config->getCacheTime());
50+
$router = new SimpleRouter($responder, $cache, $config->getCacheTime(), $config->getDebug());
5151
foreach ($config->getMiddlewares() as $middleware => $properties) {
5252
switch ($middleware) {
5353
case 'cors':
@@ -121,23 +121,9 @@ public function handle(Request $request): Response
121121
try {
122122
$response = $this->router->route($request);
123123
} catch (\Throwable $e) {
124-
if ($e instanceof \PDOException) {
125-
if (strpos(strtolower($e->getMessage()), 'duplicate') !== false) {
126-
$response = $this->responder->error(ErrorCode::DUPLICATE_KEY_EXCEPTION, '');
127-
} elseif (strpos(strtolower($e->getMessage()), 'default value') !== false) {
128-
$response = $this->responder->error(ErrorCode::DATA_INTEGRITY_VIOLATION, '');
129-
} elseif (strpos(strtolower($e->getMessage()), 'allow nulls') !== false) {
130-
$response = $this->responder->error(ErrorCode::DATA_INTEGRITY_VIOLATION, '');
131-
} elseif (strpos(strtolower($e->getMessage()), 'constraint') !== false) {
132-
$response = $this->responder->error(ErrorCode::DATA_INTEGRITY_VIOLATION, '');
133-
}
134-
}
135-
if (!$response) {
136-
$response = $this->responder->error(ErrorCode::ERROR_NOT_FOUND, $e->getMessage());
137-
}
124+
$response = $this->responder->error(ErrorCode::ERROR_NOT_FOUND, $e->getMessage());
138125
if ($this->debug) {
139-
$response->addHeader('X-Exception-Message', $e->getMessage());
140-
$response->addHeader('X-Exception-File', $e->getFile() . ':' . $e->getLine());
126+
$response->addExceptionHeaders($e);
141127
}
142128
}
143129
return $response;

src/Tqdev/PhpCrudApi/Middleware/Router/SimpleRouter.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,18 @@ class SimpleRouter implements Router
1414
private $responder;
1515
private $cache;
1616
private $ttl;
17+
private $debug;
1718
private $registration;
1819
private $routes;
1920
private $routeHandlers;
2021
private $middlewares;
2122

22-
public function __construct(Responder $responder, Cache $cache, int $ttl)
23+
public function __construct(Responder $responder, Cache $cache, int $ttl, bool $debug)
2324
{
2425
$this->responder = $responder;
2526
$this->cache = $cache;
2627
$this->ttl = $ttl;
28+
$this->debug = $debug;
2729
$this->registration = true;
2830
$this->routes = $this->loadPathTree();
2931
$this->routeHandlers = [];
@@ -91,7 +93,23 @@ public function handle(Request $request): Response
9193
if (count($routeNumbers) == 0) {
9294
return $this->responder->error(ErrorCode::ROUTE_NOT_FOUND, $request->getPath());
9395
}
94-
return call_user_func($this->routeHandlers[$routeNumbers[0]], $request);
96+
try {
97+
$response = call_user_func($this->routeHandlers[$routeNumbers[0]], $request);
98+
} catch (\PDOException $e) {
99+
if (strpos(strtolower($e->getMessage()), 'duplicate') !== false) {
100+
$response = $this->responder->error(ErrorCode::DUPLICATE_KEY_EXCEPTION, '');
101+
} elseif (strpos(strtolower($e->getMessage()), 'default value') !== false) {
102+
$response = $this->responder->error(ErrorCode::DATA_INTEGRITY_VIOLATION, '');
103+
} elseif (strpos(strtolower($e->getMessage()), 'allow nulls') !== false) {
104+
$response = $this->responder->error(ErrorCode::DATA_INTEGRITY_VIOLATION, '');
105+
} elseif (strpos(strtolower($e->getMessage()), 'constraint') !== false) {
106+
$response = $this->responder->error(ErrorCode::DATA_INTEGRITY_VIOLATION, '');
107+
}
108+
if ($this->debug) {
109+
$response->addExceptionHeaders($e);
110+
}
111+
}
112+
return $response;
95113
}
96114

97115
}

src/Tqdev/PhpCrudApi/Response.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ public function output()
7171
echo $this->getBody();
7272
}
7373

74+
public function addExceptionHeaders(\Throwable $e)
75+
{
76+
$this->addHeader('X-Exception-Name', get_class($e));
77+
$this->addHeader('X-Exception-Message', $e->getMessage());
78+
$this->addHeader('X-Exception-File', $e->getFile() . ':' . $e->getLine());
79+
}
80+
7481
public function __toString(): String
7582
{
7683
$str = "$this->status\n";

0 commit comments

Comments
 (0)