diff --git a/modules/roblib_search_eds/js/eds_results_article.js b/modules/roblib_search_eds/js/eds_results_article.js index cc29880..31b5280 100644 --- a/modules/roblib_search_eds/js/eds_results_article.js +++ b/modules/roblib_search_eds/js/eds_results_article.js @@ -84,6 +84,9 @@ Drupal.behaviors.roblib_search_eds_articles = { jQuery('#roblib-eds-articles-more-results').empty().append(upei_link); jQuery('#roblib-eds-articles-toc').empty().append('Articles (' + data.recordCount + ')'); + }).fail(function () { + // Don't leave the spinner running if the request fails. + jQuery('#roblib-search-content-eds-articles').empty().append(Drupal.t('Results are currently unavailable.')); }); } } diff --git a/modules/roblib_search_eds/js/eds_results_book.js b/modules/roblib_search_eds/js/eds_results_book.js index f33d076..70c2102 100644 --- a/modules/roblib_search_eds/js/eds_results_book.js +++ b/modules/roblib_search_eds/js/eds_results_book.js @@ -86,6 +86,9 @@ Drupal.behaviors.roblib_search_eds = { jQuery('#roblib-eds-books-more-results').empty().append(upei_link); jQuery('#roblib-eds-books-toc').empty().append('Books (' + data.recordCount + ')'); + }).fail(function () { + // Don't leave the spinner running if the request fails. + jQuery('#roblib-search-content-eds').empty().append(Drupal.t('Results are currently unavailable.')); }); } } diff --git a/modules/roblib_search_eds/src/Lib/EBSCOConnector.php b/modules/roblib_search_eds/src/Lib/EBSCOConnector.php index 5908cd8..39b23d6 100644 --- a/modules/roblib_search_eds/src/Lib/EBSCOConnector.php +++ b/modules/roblib_search_eds/src/Lib/EBSCOConnector.php @@ -1,5 +1,7 @@ '']; + $request_headers[$name] = $value; + } - try { - $curl = curl_init(); - curl_setopt($curl, CURLOPT_URL, $url); - curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($curl, CURLOPT_MAXREDIRS, 10 ); - curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); - curl_setopt($curl, CURLOPT_TIMEOUT, 10); - curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); - curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); - - switch ($method) - { - case 'GET': - if ($data) { $url = sprintf('%s?%s', $url, http_build_query($data)); } - curl_setopt($curl, CURLOPT_URL, $url); - break; - - case 'POST': - if ($data) { - curl_setopt($curl,CURLOPT_POST, 1); - curl_setopt($curl,CURLOPT_POSTFIELDS, $data); - } - break; - - case 'DELETE': - if ($data) { - if (count($headers)>0) { - curl_setopt($curl,CURLOPT_HTTPHEADER, $headers); - } - curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE"); - } - break; + $options = [ + 'headers' => $request_headers, + 'timeout' => 10, + 'allow_redirects' => ['max' => 10], + // Let us inspect 4xx/5xx bodies; EDS returns API errors as HTTP 400. + 'http_errors' => FALSE, + ]; + + switch ($method) { + case 'GET': + if ($params) { + $url = sprintf('%s?%s', $url, http_build_query($params)); } + break; + case 'POST': + case 'DELETE': + if ($params) { + $options['body'] = $params; + } + break; + } - $response = curl_exec($curl); - $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); - curl_close($curl); - - switch ($code) { - case self::HTTP_OK: - - $xml_str = $response; + try { + $http_response = \Drupal::httpClient()->request($method, $url, $options); + $code = $http_response->getStatusCode(); + $response = (string) $http_response->getBody(); + } + catch (GuzzleException $e) { + // Network level failure (timeout, DNS, TLS...). + \Drupal::logger('ebsco')->error('EDS request to @url failed: @message', [ + '@url' => $url, + '@message' => $e->getMessage(), + ]); + return new EBSCOException('The EDS API could not be reached.'); + } - try { - // Clean EMP namespace. - $xml_str = str_replace(array("ErrorNumber) || isset($xml->ErrorCode))) { + $error = ''; + $error_code = 0; + if (isset($xml->DetailedErrorDescription) && !empty($xml->DetailedErrorDescription)) { + $error = (string) $xml->DetailedErrorDescription; + } + elseif (isset($xml->ErrorDescription)) { + $error = (string) $xml->ErrorDescription; } - catch (Exception $e) { - $return = new EBSCOException($xml); + elseif (isset($xml->Reason)) { + $error = (string) $xml->Reason; } - break; - - case self::HTTP_BAD_REQUEST: - $xml_str = $response; - try { - $xml = simplexml_load_string($xml_str); - - // If the response is an API error. - $isError = isset($xml->ErrorNumber) || isset($xml->ErrorCode); - if ($isError) { - $error = ''; $code = 0; - if (isset($xml->DetailedErrorDescription) && !empty($xml->DetailedErrorDescription)) { - $error = (string) $xml->DetailedErrorDescription; - } - elseif (isset($xml->ErrorDescription)) { - $error = (string) $xml->ErrorDescription; - } - elseif (isset($xml->Reason)) { - $error = (string) $xml->Reason; - } - if (isset($xml->ErrorNumber)) { - $code = (integer) $xml->ErrorNumber; - } - elseif (isset($xml->ErrorCode)) { - $code = (integer) $xml->ErrorCode; - } - $return = new EBSCOException($error, $code); - } - else { - $return = new EBSCOException("HTTP {$code} : The request could not be understood by the server due to malformed syntax. Modify your search before retrying."); - } + if (isset($xml->ErrorNumber)) { + $error_code = (int) $xml->ErrorNumber; } - catch (Exception $e) { - $return = new EBSCOException($xml); + elseif (isset($xml->ErrorCode)) { + $error_code = (int) $xml->ErrorCode; } - break; + $return = new EBSCOException($error, $error_code); + } + else { + $return = new EBSCOException("HTTP {$code} : The request could not be understood by the server due to malformed syntax. Modify your search before retrying."); + } + break; - case self::HTTP_NOT_FOUND: - $return = new EBSCOException("HTTP {$code} : The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable."); - break; + case self::HTTP_NOT_FOUND: + $return = new EBSCOException("HTTP {$code} : The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable."); + break; - case self::HTTP_INTERNAL_SERVER_ERROR: - $return = new EBSCOException("HTTP {$code} : The server encountered an unexpected condition which prevented it from fulfilling the request."); - break; + case self::HTTP_INTERNAL_SERVER_ERROR: + $return = new EBSCOException("HTTP {$code} : The server encountered an unexpected condition which prevented it from fulfilling the request."); + break; - default: - $return = new EBSCOException("HTTP {$code} : Unexpected HTTP error."); - break; - } - } - catch (Exception $e) { - // Or $this->toString($response) - $message = $this->toString($e); - \Drupal::logger('ebsco')->error($message); - $return = new EBSCOException($response); + default: + $return = new EBSCOException("HTTP {$code} : Unexpected HTTP error."); + break; } - // Log any error if ($this->logAPIRequests) { - // $client = both the HTTP request and response - // $response = only the HTTP response - $message = $this->toString($client); // or $this->toString($response) - \Drupal::logger('ebsco')->error($client); + \Drupal::logger('ebsco')->debug('EDS @method @url returned HTTP @code: @response', [ + '@method' => $method, + '@url' => $url, + '@code' => $code, + '@response' => $response, + ]); } return $return; diff --git a/modules/solr_site/js/solr_site_bestbet.js b/modules/solr_site/js/solr_site_bestbet.js index 72f7467..be5229c 100644 --- a/modules/solr_site/js/solr_site_bestbet.js +++ b/modules/solr_site/js/solr_site_bestbet.js @@ -30,6 +30,9 @@ Drupal.behaviors.roblib_search_solr_bestbet_results = { }); jQuery('#' + 'roblib-search-content-solr-site-bestbet').empty().append(items.join('')); } + }).fail(function () { + // Don't leave the spinner running if the request fails. + jQuery('#roblib-search-content-solr-site-bestbet').empty().append(Drupal.t('Results are currently unavailable.')); }); } } diff --git a/modules/solr_site/js/solr_site_results.js b/modules/solr_site/js/solr_site_results.js index 38281d6..cc779ba 100644 --- a/modules/solr_site/js/solr_site_results.js +++ b/modules/solr_site/js/solr_site_results.js @@ -39,6 +39,9 @@ Drupal.behaviors.roblib_search_solr_site_results = { jQuery('#' + 'roblib-solr-more-results-results').empty().append(results_url); jQuery('#' + 'roblib-solr-search-toc-results').empty().append(move_me_div_url); } + }).fail(function () { + // Don't leave the spinner running if the request fails. + jQuery('#roblib-search-content-solr-site-results').empty().append(Drupal.t('Results are currently unavailable.')); }); } }