Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Added error listeners to RequestHandlerRunner #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion src/RequestHandlerRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ class RequestHandlerRunner
*/
private $serverRequestFactory;

/**
* @var callable[]
*/
protected $listeners = [];

public function __construct(
RequestHandlerInterface $handler,
Emitter\EmitterInterface $emitter,
Expand Down Expand Up @@ -97,9 +102,43 @@ public function run() : void
$this->emitter->emit($response);
}

private function emitMarshalServerRequestException(Throwable $exception) : void
/**
* Attach an error listener.
*
* Each listener receives the following two arguments:
*
* - Throwable $error
* - ResponseInterface $response
*
* These instances are all immutable, and the return values of
* listeners are ignored; use listeners for reporting purposes
* only.
*/
public function attachListener(callable $listener) : void
{
if (in_array($listener, $this->listeners, true)) {
return;
}

$this->listeners[] = $listener;
}

/**
* Trigger all error listeners.
*/
private function triggerListeners(
\Throwable $error,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\ in Throwable is not needed as already in use statement.

ResponseInterface $response
) : void {
foreach ($this->listeners as $listener) {
$listener($error, $response);
}
}

public function emitMarshalServerRequestException(Throwable $exception) : void
{
$response = ($this->serverRequestErrorResponseGenerator)($exception);
$this->triggerListeners($exception, $response);
$this->emitter->emit($response);
}
}