Skip to content

Commit 7dc31f6

Browse files
committed
Run the 500 error from the registry on exception if any, rethrown otherwise
1 parent 4bed08a commit 7dc31f6

File tree

1 file changed

+53
-47
lines changed

1 file changed

+53
-47
lines changed

src/WaterPipe/WaterPipe.php

Lines changed: 53 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,8 @@ public function delete(string $uri, $action)
268268
/**
269269
* Register a sub pipe managed by this pipe.
270270
*
271-
* @param string $uri The request URI.
272-
* @param callable $action The action to call when the request
273-
* correspond to the given URI.
271+
* @param string $baseUri The request URI.
272+
* @param WaterPipe $pipe The pipe to run within the given base URI.
274273
*/
275274
public function pipe(string $baseUri, WaterPipe $pipe)
276275
{
@@ -350,57 +349,64 @@ private function _findSubPipe()
350349

351350
private function _executeRequest()
352351
{
353-
// Execute middleware
354-
self::triggerBeforeExecuteEvent(Request::getInstance());
355-
356-
$registry = null;
357-
358-
switch (Request::getInstance()->getMethod()) {
359-
case RequestMethod::UNKNOWN:
360-
if (isset($this->_errorsRegistry[500]))
361-
return $this->_executeAction($this->_errorsRegistry[500]);
362-
else throw new InternalServerErrorException();
363-
364-
case RequestMethod::GET:
365-
$registry = $this->_getRequestRegistry;
366-
break;
367-
368-
case RequestMethod::POST:
369-
$registry = $this->_postRequestRegistry;
370-
break;
371-
372-
case RequestMethod::PUT:
373-
$registry = $this->_putRequestRegistry;
374-
break;
352+
try
353+
{
354+
// Execute middleware
355+
self::triggerBeforeExecuteEvent(Request::getInstance());
356+
357+
$registry = null;
358+
359+
switch (Request::getInstance()->getMethod()) {
360+
case RequestMethod::UNKNOWN:
361+
if (isset($this->_errorsRegistry[500]))
362+
return $this->_executeAction($this->_errorsRegistry[500]);
363+
else throw new InternalServerErrorException();
364+
365+
case RequestMethod::GET:
366+
$registry = $this->_getRequestRegistry;
367+
break;
368+
369+
case RequestMethod::POST:
370+
$registry = $this->_postRequestRegistry;
371+
break;
372+
373+
case RequestMethod::PUT:
374+
$registry = $this->_putRequestRegistry;
375+
break;
376+
377+
case RequestMethod::DELETE:
378+
$registry = $this->_deleteRequestRegistry;
379+
break;
380+
}
375381

376-
case RequestMethod::DELETE:
377-
$registry = $this->_deleteRequestRegistry;
378-
break;
379-
}
382+
if ($registry === null) {
383+
throw new \Exception("Cannot handle a request of this type");
384+
}
380385

381-
if ($registry === null) {
382-
throw new \Exception("Cannot handle a request of this type");
383-
}
386+
$runner = $this->_getActionForRoutes($registry);
384387

385-
$runner = $this->_getActionForRoutes($registry);
388+
if ($runner === null) {
389+
$runner = $this->_getActionForRoutes($this->_requestRegistry);
390+
}
386391

387-
if ($runner === null) {
388-
$runner = $this->_getActionForRoutes($this->_requestRegistry);
389-
}
392+
if ($runner === null) {
393+
if (isset($this->_errorsRegistry[404]))
394+
return $this->_executeAction($this->_errorsRegistry[404]);
395+
else throw new NotFoundErrorException();
396+
}
390397

391-
if ($runner === null) {
392-
if (isset($this->_errorsRegistry[404]))
393-
return $this->_executeAction($this->_errorsRegistry[404]);
394-
else throw new NotFoundErrorException();
395-
}
398+
if (!is_callable($runner) && !is_array($runner) && !is_subclass_of($runner, RouteAction::class)) {
399+
// TODO: Proper exception
400+
throw new \Exception("Malformed route action");
401+
}
396402

397-
if (!is_callable($runner) && !is_array($runner) && !is_subclass_of($runner, RouteAction::class)) {
398-
// TODO: Proper exception
399-
throw new \Exception("Malformed route action");
403+
// NOTE: No code will be executed after this call...
404+
$this->_executeAction($runner);
405+
} catch (\Exception $e) {
406+
if (isset($this->_errorsRegistry[500]))
407+
$this->_executeAction($this->_errorsRegistry[500]);
408+
else throw $e;
400409
}
401-
402-
// NOTE: No code will be executed after this call...
403-
$this->_executeAction($runner);
404410
}
405411

406412
private function _executeAction($runner)

0 commit comments

Comments
 (0)