From dd9303b7cccf7939957958c69f4da5aa12cf955f Mon Sep 17 00:00:00 2001 From: Matthew Wu Date: Fri, 19 Jan 2024 17:04:00 -0500 Subject: [PATCH] add error messages --- src/IndexingService.php | 18 ++++++++++++++++++ .../JobType/TriplestoreIndexJob.php | 13 ++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/IndexingService.php b/src/IndexingService.php index 0d06dfb..1d300c8 100644 --- a/src/IndexingService.php +++ b/src/IndexingService.php @@ -144,6 +144,8 @@ public function delete(string $uri) { $config = \Drupal::config('triplestore_indexer.settings'); $server = $config->get("server_url"); $namespace = $config->get("namespace"); + $username = $config->get("admin_username"); + $password = $config->get("admin_password"); $opts = [ @@ -158,12 +160,28 @@ public function delete(string $uri) { CURLOPT_POSTFIELDS => "", CURLOPT_HTTPHEADER => [ 'Content-type: text/plain', + 'Authorization: Basic ' . base64_encode("$username:$password"), ], ]; curl_setopt_array($curl, $opts); $response = curl_exec($curl); + + // Check for cURL errors + if ($response === false) { + $error_message = curl_error($curl); + curl_close($curl); + throw new \Exception("cURL error: $error_message"); + } + + // Check HTTP status code for errors + $http_status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); curl_close($curl); + + if ($http_status_code < 200 || $http_status_code >= 300) { + throw new \Exception("HTTP error: $http_status_code"); + } + return $response; } diff --git a/src/Plugin/AdvancedQueue/JobType/TriplestoreIndexJob.php b/src/Plugin/AdvancedQueue/JobType/TriplestoreIndexJob.php index 668e7b0..582b225 100644 --- a/src/Plugin/AdvancedQueue/JobType/TriplestoreIndexJob.php +++ b/src/Plugin/AdvancedQueue/JobType/TriplestoreIndexJob.php @@ -52,7 +52,18 @@ public function process(Job $job) { $type = str_replace("_", "/", $payload['type']); $urijld = "<$base_url/$type/$nid" . '?_format=jsonld>'; - $response = $service->delete($urijld); + + try { + $response = $service->delete($urijld); + } catch (\Exception $e) { + if ($e->getCode() == 401) { + // Retry with authorization headers if 401 Unauthorized + $response = $service->delete($urijld); + } else { + // Handle other exceptions + throw $e; + } + } $result = simplexml_load_string($response); if ($result['modified'] <= 0) {