Skip to content

Commit

Permalink
catch http error response in the debugger, otherwise the response is …
Browse files Browse the repository at this point in the history
…null
  • Loading branch information
rafrsr committed Jun 30, 2016
1 parent 0065498 commit 398a1c1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/ApiInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Rafrsr\GenericApi;

use GuzzleHttp\Exception\GuzzleException;
use Psr\Http\Message\ResponseInterface;
use Rafrsr\GenericApi\Exception\ApiException;

Expand Down Expand Up @@ -52,6 +53,7 @@ public function isModeLive();
*
* @return ResponseInterface|mixed
* @throws ApiException if any other kind of error is happened before or after the request is sent
* @throws GuzzleException if any error happen in the communication
*/
public function process(ApiServiceInterface $service);
}
6 changes: 4 additions & 2 deletions src/Debug/ApiDebugger.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ public function beginRequestProcess(RequestInterface $request)
* @param RequestProcess $process
* @param ResponseInterface $response
*/
public function finishRequestProcess(RequestProcess $process, ResponseInterface $response)
public function finishRequestProcess(RequestProcess $process, ResponseInterface $response = null)
{
$process->setResponse($response);
if ($response) {
$process->setResponse($response);
}
$this->requestStack[] = $process;
}

Expand Down
25 changes: 19 additions & 6 deletions src/GenericApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
namespace Rafrsr\GenericApi;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Psr\Http\Message\ResponseInterface;
use Rafrsr\GenericApi\Debug\ApiDebugger;
use Rafrsr\GenericApi\Debug\RequestProcess;
use Rafrsr\GenericApi\Debug\RequestProcessStack;
use Rafrsr\GenericApi\Event\OnResponseEvent;
use Rafrsr\GenericApi\Event\PreBuildRequestEvent;
use Rafrsr\GenericApi\Event\PreSendRequestEvent;
Expand Down Expand Up @@ -144,14 +143,28 @@ public function process(ApiServiceInterface $service)

$this->getEventDispatcher()->dispatch(self::EVENT_PRE_SEND_REQUEST, new PreSendRequestEvent($this, $service, $request));

$debugProcess = null;
if ($this->debugger) {
$process = $this->debugger->beginRequestProcess($request);
$debugProcess = $this->debugger->beginRequestProcess($request);
}

$httpResponse = $this->sendRequest($request);
$exception = null;
$httpResponse = null;
try {
$httpResponse = $this->sendRequest($request);
} catch (RequestException $e) {
$httpResponse = $e->getResponse();
//silent exception during debug
$exception = $e;
}

if ($debugProcess !== null) {
$this->debugger->finishRequestProcess($debugProcess, $httpResponse);
}

if ($this->debugger && isset($process)) {
$this->debugger->finishRequestProcess($process, $httpResponse);
//has pending exception
if ($exception && $exception instanceof \Exception) {
throw $exception;
}

$this->getEventDispatcher()->dispatch(self::EVENT_ON_RESPONSE, new OnResponseEvent($this, $service, $httpResponse));
Expand Down

0 comments on commit 398a1c1

Please sign in to comment.