Skip to content

Commit

Permalink
[gateway] Better hanlding for exception thrown on onPostExecute
Browse files Browse the repository at this point in the history
fixes #557
  • Loading branch information
makasim committed Jul 25, 2016
1 parent 5443dc9 commit 868d676
Show file tree
Hide file tree
Showing 2 changed files with 208 additions and 98 deletions.
39 changes: 31 additions & 8 deletions Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ class Gateway implements GatewayInterface
*/
public function __construct()
{
$this->stack = array();
$this->actions = array();
$this->apis = array();
$this->stack = [];
$this->actions = [];
$this->apis = [];

$this->extensions = new ExtensionCollection();
}
Expand Down Expand Up @@ -126,16 +126,39 @@ public function execute($request, $catchReply = false)
} catch (\Exception $e) {
$context->setException($e);

$this->extensions->onPostExecute($context);
$this->onPostExecuteWithException($context);
}

array_pop($this->stack);
return;
}

protected function onPostExecuteWithException(Context $context)
{
array_pop($this->stack);

$exception = $context->getException();

if ($context->getException()) {
throw $context->getException();
try {
$this->extensions->onPostExecute($context);
} catch (\Exception $e) {
// logic is similar to one in Symfony's ExceptionListener::onKernelException
$wrapper = $e;
while ($prev = $wrapper->getPrevious()) {
if ($exception === $wrapper = $prev) {
throw $e;
}
}

$prev = new \ReflectionProperty('Exception', 'previous');
$prev->setAccessible(true);
$prev->setValue($wrapper, $exception);

throw $e;
}

return;
if ($context->getException()) {
throw $context->getException();
}
}

/**
Expand Down
Loading

0 comments on commit 868d676

Please sign in to comment.