Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC / WIP (do not merge): surface cURL errors #194

Merged
merged 2 commits into from
Feb 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions library/Requests/Exception/Transport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

class Requests_Exception_Transport extends Requests_Exception {

}
56 changes: 56 additions & 0 deletions library/Requests/Exception/Transport/cURL.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

class Requests_Exception_Transport_cURL extends Requests_Exception_Transport {

const EASY = 'cURLEasy';
const MULTI = 'cURLMulti';
const SHARE = 'cURLShare';

/**
* cURL error code
*
* @var integer
*/
protected $code = -1;

/**
* Which type of cURL error
*
* EASY|MULTI|SHARE
*
* @var string
*/
protected $type = 'Unknown';

/**
* Clear text error message
*
* @var string
*/
protected $reason = 'Unknown';

public function __construct($message, $type, $data = null, $code = 0) {
if ($type !== null) {
$this->type = $type;
}

if ($code !== null) {
$this->code = $code;
}

if ($message !== null) {
$this->reason = $message;
}

$message = sprintf('%d %s', $this->code, $this->reason);
parent::__construct($message, $this->type, $data, $this->code);
}

/**
* Get the error message
*/
public function getReason() {
return $this->reason;
}

}
18 changes: 16 additions & 2 deletions library/Requests/Transport/cURL.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,23 @@ public function request_multiple($requests, $options) {
// Parse the finished requests before we start getting the new ones
foreach ($to_process as $key => $done) {
$options = $requests[$key]['options'];
$responses[$key] = $subrequests[$key]->process_response($subrequests[$key]->response_data, $options);
if (CURLE_OK !== $done['result']) {
//get error string for handle.
$reason = curl_error($done['handle']);
$exception = new Requests_Exception_Transport_cURL(
$reason,
Requests_Exception_Transport_cURL::EASY,
$done['handle'],
$done['result']
);
$responses[$key] = $exception;
$options['hooks']->dispatch('transport.internal.parse_error', array(&$responses[$key], $requests[$key]));
}
else {
$responses[$key] = $subrequests[$key]->process_response($subrequests[$key]->response_data, $options);

$options['hooks']->dispatch('transport.internal.parse_response', array(&$responses[$key], $requests[$key]));
$options['hooks']->dispatch('transport.internal.parse_response', array(&$responses[$key], $requests[$key]));
}

curl_multi_remove_handle($multihandle, $done['handle']);
curl_close($done['handle']);
Expand Down