Skip to content

Commit faa418c

Browse files
committed
Add class RequestException
1 parent af43e5b commit faa418c

3 files changed

Lines changed: 121 additions & 5 deletions

File tree

src/Client.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Framework\HTTP\URL;
1515
use Generator;
1616
use InvalidArgumentException;
17-
use RuntimeException;
1817

1918
/**
2019
* Class Client.
@@ -50,7 +49,7 @@ public function createRequest(URL | string $url) : Request
5049
* @param Request $request
5150
*
5251
* @throws InvalidArgumentException for invalid Request Protocol
53-
* @throws RuntimeException for curl error
52+
* @throws RequestException for curl error
5453
*
5554
* @return Response
5655
*/
@@ -66,7 +65,11 @@ public function run(Request $request) : Response
6665
$info = (array) \curl_getinfo($handle);
6766
}
6867
if ($body === false) {
69-
throw new RuntimeException(\curl_error($handle), \curl_errno($handle));
68+
throw new RequestException(
69+
\curl_error($handle),
70+
\curl_errno($handle),
71+
info: $info
72+
);
7073
}
7174
\curl_close($handle);
7275
if ($body === true) {

src/RequestException.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of Aplus Framework HTTP Client Library.
4+
*
5+
* (c) Natan Felles <natanfelles@gmail.com>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace Framework\HTTP\Client;
11+
12+
use JetBrains\PhpStorm\ArrayShape;
13+
use RuntimeException;
14+
use Throwable;
15+
16+
/**
17+
* Class RequestException.
18+
*
19+
* @package http-client
20+
*/
21+
class RequestException extends RuntimeException
22+
{
23+
/**
24+
* @var array<mixed>
25+
*/
26+
protected array $info;
27+
28+
/**
29+
* RequestException constructor.
30+
*
31+
* @param string $message
32+
* @param int $code
33+
* @param Throwable|null $previous
34+
* @param array<mixed> $info
35+
*/
36+
public function __construct(
37+
string $message = '',
38+
int $code = 0,
39+
?Throwable $previous = null,
40+
array $info = []
41+
) {
42+
parent::__construct($message, $code, $previous);
43+
$this->info = $info;
44+
}
45+
46+
/**
47+
* @return array<mixed>
48+
*/
49+
#[ArrayShape([
50+
'appconnect_time_us' => 'int',
51+
'certinfo' => 'array',
52+
'connect_time' => 'float',
53+
'connect_time_us' => 'int',
54+
'content_type' => 'string',
55+
'download_content_length' => 'float',
56+
'effective_method' => 'string',
57+
'filetime' => 'int',
58+
'header_size' => 'int',
59+
'http_code' => 'int',
60+
'http_version' => 'int',
61+
'local_ip' => 'string',
62+
'local_port' => 'int',
63+
'namelookup_time' => 'float',
64+
'namelookup_time_us' => 'int',
65+
'pretransfer_time' => 'float',
66+
'pretransfer_time_us' => 'int',
67+
'primary_ip' => 'string',
68+
'primary_port' => 'int',
69+
'protocol' => 'int',
70+
'redirect_count' => 'int',
71+
'redirect_time' => 'float',
72+
'redirect_time_us' => 'int',
73+
'redirect_url' => 'string',
74+
'request_size' => 'int',
75+
'scheme' => 'string',
76+
'size_download' => 'float',
77+
'size_upload' => 'float',
78+
'speed_download' => 'float',
79+
'speed_upload' => 'float',
80+
'ssl_verify_result' => 'int',
81+
'ssl_verifyresult' => 'int',
82+
'starttransfer_time' => 'float',
83+
'starttransfer_time_us' => 'int',
84+
'total_time' => 'float',
85+
'total_time_us' => 'int',
86+
'upload_content_length' => 'float',
87+
'url' => 'string',
88+
])]
89+
public function getInfo() : array
90+
{
91+
return $this->info;
92+
}
93+
}

tests/ClientTest.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use Framework\HTTP\Client\Client;
1313
use Framework\HTTP\Client\Request;
14+
use Framework\HTTP\Client\RequestException;
1415
use Framework\HTTP\Client\Response;
1516
use Framework\HTTP\Client\ResponseError;
1617
use Framework\HTTP\Protocol;
@@ -95,8 +96,27 @@ public function testMethods() : void
9596
public function testRunError() : void
9697
{
9798
$request = new Request('http://domain.tld');
98-
$this->expectException(\RuntimeException::class);
99-
$this->client->run($request);
99+
try {
100+
$this->client->run($request);
101+
} catch (RequestException $exception) {
102+
self::assertSame(
103+
'Failed to connect to domain.tld port 80: No route to host',
104+
$exception->getMessage()
105+
);
106+
self::assertSame(7, $exception->getCode());
107+
self::assertEmpty($exception->getInfo());
108+
}
109+
}
110+
111+
public function testRunErrorWithInfo() : void
112+
{
113+
$request = new Request('http://domain.tld');
114+
$request->setGetResponseInfo();
115+
try {
116+
$this->client->run($request);
117+
} catch (RequestException $exception) {
118+
self::assertSame(0, $exception->getInfo()['http_code']);
119+
}
100120
}
101121

102122
public function testRunWithRequestDownloadFunction() : void

0 commit comments

Comments
 (0)