Skip to content

Commit

Permalink
Merge pull request #194 from ifwe/PLAT-955
Browse files Browse the repository at this point in the history
RFC / WIP (do not merge): surface cURL errors
  • Loading branch information
rmccue committed Feb 3, 2016
2 parents e2511c9 + a434d5e commit 25ee8f6
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
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

0 comments on commit 25ee8f6

Please sign in to comment.