@@ -34,11 +34,38 @@ public function request($url, $method, $params, $headers)
34
34
curl_setopt ($ s , CURLOPT_RETURNTRANSFER , true );
35
35
curl_setopt ($ s , CURLOPT_CUSTOMREQUEST , $ method );
36
36
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 );
37
41
38
42
if (($ response = curl_exec ($ s )) === false ) {
39
43
$ error = curl_error ($ s );
44
+ $ errno = curl_errno ($ s );
40
45
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 );
42
69
} else {
43
70
$ response = HttpResponse::create (curl_getinfo ($ s )['http_code ' ], $ response );
44
71
}
0 commit comments