Skip to content

Commit

Permalink
Merge pull request #27 from packagist/t/ResourceNotFound-add-request-uri
Browse files Browse the repository at this point in the history
Add request uri to ResourceNotFoundException
  • Loading branch information
naderman authored Dec 2, 2019
2 parents f8a84e1 + 23903da commit bc012ab
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
31 changes: 31 additions & 0 deletions src/Exception/HttpTransportException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* (c) Packagist Conductors UG (haftungsbeschränkt) <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace PrivatePackagist\ApiClient\Exception;

use Throwable;

class HttpTransportException extends RuntimeException
{
private $requestUri;

public function __construct($message = "", $code = 0, $requestUri = "", Throwable $previous = null)
{
$this->requestUri = $requestUri;
parent::__construct($message, $code, $previous);
}

/**
* @return string
*/
public function getRequestUri()
{
return $this->requestUri;
}
}
2 changes: 1 addition & 1 deletion src/Exception/ResourceNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@

namespace PrivatePackagist\ApiClient\Exception;

class ResourceNotFoundException extends RuntimeException
class ResourceNotFoundException extends HttpTransportException
{
}
7 changes: 4 additions & 3 deletions src/HttpClient/Plugin/ExceptionThrower.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Http\Client\Common\Plugin;
use PrivatePackagist\ApiClient\Exception\ErrorException;
use PrivatePackagist\ApiClient\Exception\HttpTransportException;
use PrivatePackagist\ApiClient\Exception\ResourceNotFoundException;
use PrivatePackagist\ApiClient\Exception\RuntimeException;
use PrivatePackagist\ApiClient\HttpClient\Message\ResponseMediator;
Expand All @@ -29,7 +30,7 @@ public function __construct(ResponseMediator $responseMediator = null)

public function handleRequest(RequestInterface $request, callable $next, callable $first)
{
return $next($request)->then(function (ResponseInterface $response) {
return $next($request)->then(function (ResponseInterface $response) use ($request) {
if ($response->getStatusCode() < 400 || $response->getStatusCode() > 600) {
return $response;
}
Expand All @@ -41,11 +42,11 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
}

if ($response->getStatusCode() === 404) {
throw new ResourceNotFoundException($content['message'], $response->getStatusCode());
throw new ResourceNotFoundException($content['message'], $response->getStatusCode(), $request->getUri());
}
}

throw new RuntimeException(isset($content['message']) ? $content['message'] : $content, $response->getStatusCode());
throw new HttpTransportException(isset($content['message']) ? $content['message'] : $content, $response->getStatusCode(), $request->getUri());
});
}
}

0 comments on commit bc012ab

Please sign in to comment.