Skip to content

Commit 3eb41a6

Browse files
committed
put error codes to curl
1 parent 113ddfb commit 3eb41a6

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/Shared/Exceptions/RequestException.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,13 @@
99
*/
1010
class RequestException extends Exception
1111
{
12-
12+
/**
13+
* @param string $message
14+
* @param int $code
15+
* @param \Throwable $previous
16+
*/
17+
public function __construct($message = "", $code = 0, $previous = null)
18+
{
19+
parent::__construct($message, (int)$code, $previous);
20+
}
1321
}

src/Shared/HttpClient.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,38 @@ public function request($url, $method, $params, $headers)
3434
curl_setopt($s, CURLOPT_RETURNTRANSFER, true);
3535
curl_setopt($s, CURLOPT_CUSTOMREQUEST, $method);
3636
curl_setopt($s, CURLOPT_HTTPHEADER, $this->getHeader($headers));
37+
38+
// Set very short timeout for testing (1 millisecond)
39+
curl_setopt($s, CURLOPT_TIMEOUT, 1);
40+
curl_setopt($s, CURLOPT_CONNECTTIMEOUT, 1);
3741

3842
if (($response = curl_exec($s)) === false) {
3943
$error = curl_error($s);
44+
$errno = curl_errno($s);
4045
curl_close($s);
41-
throw new RequestException('curl_error: ' . $error);
46+
47+
// Map CURL error codes to appropriate HTTP status codes
48+
$httpCode = 500; // Default to 500 for unknown errors
49+
50+
switch ($errno) {
51+
case CURLE_OPERATION_TIMEDOUT:
52+
case CURLE_OPERATION_TIMEOUTED:
53+
$httpCode = 408; // Request Timeout
54+
break;
55+
case CURLE_COULDNT_CONNECT:
56+
case CURLE_COULDNT_RESOLVE_HOST:
57+
$httpCode = 503; // Service Unavailable
58+
break;
59+
case CURLE_SSL_CONNECT_ERROR:
60+
case CURLE_SSL_CACERT:
61+
$httpCode = 495; // SSL Certificate Error
62+
break;
63+
default:
64+
$httpCode = 500; // Internal Server Error
65+
break;
66+
}
67+
68+
throw new RequestException('curl_error: ' . $error, $httpCode);
4269
} else {
4370
$response = HttpResponse::create(curl_getinfo($s)['http_code'], $response);
4471
}

0 commit comments

Comments
 (0)