You are here

function httprl_stream_connection_error_formatter in HTTP Parallel Request & Threading Library 6

Same name and namespace in other branches
  1. 7 httprl.module \httprl_stream_connection_error_formatter()

Read the error number & string and give a nice looking error in the output.

This is a flexible and powerful HTTP client implementation. Correctly handles GET, POST, PUT or any other HTTP requests.

Parameters

int $errno: Error number from stream_socket_client().

string $errstr: Error string from stream_socket_client().

object $result: An object for httprl_send_request.

1 call to httprl_stream_connection_error_formatter()
httprl_establish_stream_connection in ./httprl.module
Use stream_socket_client() to create a connection to the server.

File

./httprl.module, line 937
HTTP Parallel Request Library module.

Code

function httprl_stream_connection_error_formatter($errno, $errstr, &$result) {

  // If the t function is not available use httprl_pr.
  $t = function_exists('t') ? 't' : 'httprl_pr';
  if (function_exists('t')) {

    // Make sure drupal_convert_to_utf8() is available.
    if (defined('VERSION') && substr(VERSION, 0, 1) >= 7) {
      require_once DRUPAL_ROOT . '/includes/unicode.inc';
    }
    else {
      require_once './includes/unicode.inc';
    }

    // Convert error message to utf-8. Using ISO-8859-1 (Latin-1) as source
    // encoding could be wrong; it is a simple workaround :)
    $errstr = trim(drupal_convert_to_utf8($errstr, 'ISO-8859-1'));
  }
  if (!$errno) {

    // If $errno is 0, it is an indication that the error occurred
    // before the connect() call.
    if (empty($errstr)) {

      // If the error string is empty as well, this is most likely due to a
      // problem initializing the stream.
      $result->code = HTTPRL_ERROR_INITIALIZING_STREAM;
      $result->error = $t('Error initializing socket @socket.', array(
        '@socket' => $result->socket,
      ));
    }
    elseif (stripos($errstr, 'network_getaddresses: getaddrinfo failed:') !== FALSE) {

      // Host not found. No such host is known. The name is not an official host
      // name or alias.
      $result->code = HTTPRL_HOST_NOT_FOUND;
      $result->error = $errstr;
    }
  }
  elseif ($errno == 110) {

    // 110 means Connection timed out. This should be HTTPRL_REQUEST_TIMEOUT.
    $result->code = HTTPRL_REQUEST_TIMEOUT;
    $result->error = !empty($errstr) ? $errstr : $t('Connection timed out. TCP.');
  }
  else {

    // When a network error occurs, we use a negative number so it does not
    // clash with the HTTP status codes.
    $result->code = (int) -$errno;
    $result->error = !empty($errstr) ? $errstr : $t('Error opening socket @socket.', array(
      '@socket' => $result->socket,
    ));
  }
}