Skip to content

Commit 2896cb2

Browse files
committed
Require psr/http-client, implement exceptions
1 parent 0757afa commit 2896cb2

File tree

7 files changed

+138
-2
lines changed

7 files changed

+138
-2
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
"php-http/message": "^1.5",
4444
"php-http/discovery": "^1.2.1",
4545
"symfony/http-foundation": "^2.1|^3|^4",
46-
"moneyphp/money": "^3.1"
46+
"moneyphp/money": "^3.1",
47+
"psr/http-client": "^0.1.0"
4748
},
4849
"require-dev": {
4950
"omnipay/tests": "^3",

src/Common/Http/Client.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Http\Discovery\HttpClientDiscovery;
88
use Http\Discovery\MessageFactoryDiscovery;
99
use Http\Message\RequestFactory;
10+
use Omnipay\Common\Http\Exception\NetworkException;
11+
use Omnipay\Common\Http\Exception\RequestException;
1012
use Psr\Http\Message\RequestInterface;
1113
use Psr\Http\Message\ResponseInterface;
1214
use Psr\Http\Message\StreamInterface;
@@ -39,6 +41,10 @@ public function __construct($httpClient = null, RequestFactory $requestFactory =
3941
* @param array $headers
4042
* @param string|array|resource|StreamInterface|null $body
4143
* @param string $protocolVersion
44+
*
45+
* @throws \Psr\Http\Client\Exception\NetworkException
46+
* @throws \Psr\Http\Client\Exception\RequestException
47+
*
4248
* @return ResponseInterface
4349
*/
4450
public function request(
@@ -50,6 +56,23 @@ public function request(
5056
) : ResponseInterface {
5157
$request = $this->requestFactory->createRequest($method, $uri, $headers, $body, $protocolVersion);
5258

53-
return $this->httpClient->sendRequest($request);
59+
return $this->sendRequest($request);
60+
}
61+
62+
/**
63+
* @param RequestInterface $request
64+
* @return ResponseInterface
65+
* @throws \Psr\Http\Client\Exception\NetworkException
66+
* @throws \Psr\Http\Client\Exception\RequestException
67+
*/
68+
private function sendRequest(RequestInterface $request)
69+
{
70+
try {
71+
return $this->httpClient->sendRequest($request);
72+
} catch (\Http\Client\Exception\NetworkException $networkException) {
73+
throw new NetworkException($networkException->getMessage(), $request, $networkException);
74+
} catch (\Exception $exception) {
75+
throw new RequestException($exception->getMessage(), $request, $exception);
76+
}
5477
}
5578
}

src/Common/Http/ClientInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Omnipay\Common\Http;
44

5+
use Psr\Http\Client\Exception\NetworkException;
6+
use Psr\Http\Client\Exception\RequestException;
57
use Psr\Http\Message\ResponseInterface;
68
use Psr\Http\Message\StreamInterface;
79
use Psr\Http\Message\UriInterface;
@@ -17,6 +19,9 @@ interface ClientInterface
1719
* @param resource|string|StreamInterface|null $body
1820
* @param string $protocolVersion
1921
*
22+
* @throws RequestException when the HTTP client is passed a request that is invalid and cannot be sent.
23+
* @throws NetworkException if there is an error with the network or the remote server cannot be reached.
24+
*
2025
* @return ResponseInterface
2126
*/
2227
public function request(

src/Common/Http/Exception.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Omnipay\Common\Http;
4+
5+
use Psr\Http\Message\RequestInterface;
6+
use Throwable;
7+
8+
abstract class Exception extends \RuntimeException
9+
{
10+
/** @var RequestInterface */
11+
protected $request;
12+
13+
public function __construct(string $message, RequestInterface $request, Throwable $previous = null)
14+
{
15+
$this->request = $request;
16+
17+
parent::__construct($message, 0, $previous);
18+
}
19+
20+
/**
21+
* Returns the request.
22+
*
23+
* The request object MAY be a different object from the one passed to ClientInterface::sendRequest()
24+
*
25+
* @return RequestInterface
26+
*/
27+
public function getRequest(): RequestInterface
28+
{
29+
return $this->getRequest();
30+
}
31+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Omnipay\Common\Http\Exception;
4+
5+
use Omnipay\Common\Http\Exception;
6+
use Psr\Http\Client\Exception\NetworkException as PsrNetworkException;
7+
8+
class NetworkException extends Exception implements PsrNetworkException
9+
{
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Omnipay\Common\Http\Exception;
4+
5+
use Omnipay\Common\Http\Exception;
6+
use Psr\Http\Client\Exception\RequestException as PsrRequestException;
7+
8+
class RequestException extends Exception implements PsrRequestException
9+
{
10+
}

tests/Omnipay/Common/Http/ClientTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
namespace Omnipay\Common\Http;
44

55
use GuzzleHttp\Psr7\Response;
6+
use Http\Client\Exception\NetworkException;
67
use Mockery as m;
78
use GuzzleHttp\Psr7\Request;
89
use Http\Client\HttpClient;
910
use Http\Message\RequestFactory;
1011
use Omnipay\Tests\TestCase;
12+
use Psr\Http\Client\Exception\RequestException;
1113

1214
class ClientTest extends TestCase
1315
{
@@ -44,4 +46,58 @@ public function testSend()
4446
$this->assertSame($response, $client->request('GET', '/path'));
4547

4648
}
49+
50+
public function testSendException()
51+
{
52+
$mockClient = m::mock(HttpClient::class);
53+
$mockFactory = m::mock(RequestFactory::class);
54+
$client = new Client($mockClient, $mockFactory);
55+
56+
$request = new Request('GET', '/path');
57+
$response = new Response();
58+
59+
$mockFactory->shouldReceive('createRequest')->withArgs([
60+
'GET',
61+
'/path',
62+
[],
63+
null,
64+
'1.1',
65+
])->andReturn($request);
66+
67+
$mockClient->shouldReceive('sendRequest')
68+
->with($request)
69+
->andThrow(new \Exception('Something went wrong'));
70+
71+
$this->expectException(RequestException::class);
72+
$this->expectExceptionMessage('Something went wrong');
73+
74+
$client->request('GET', '/path');
75+
}
76+
77+
public function testSendNetworkException()
78+
{
79+
$mockClient = m::mock(HttpClient::class);
80+
$mockFactory = m::mock(RequestFactory::class);
81+
$client = new Client($mockClient, $mockFactory);
82+
83+
$request = new Request('GET', '/path');
84+
$response = new Response();
85+
86+
$mockFactory->shouldReceive('createRequest')->withArgs([
87+
'GET',
88+
'/path',
89+
[],
90+
null,
91+
'1.1',
92+
])->andReturn($request);
93+
94+
$mockClient->shouldReceive('sendRequest')
95+
->with($request)
96+
->andThrow(new NetworkException('Something went wrong', $request));
97+
98+
$this->expectException(\Psr\Http\Client\Exception\NetworkException::class);
99+
$this->expectExceptionMessage('Something went wrong');
100+
101+
$client->request('GET', '/path');
102+
}
47103
}

0 commit comments

Comments
 (0)