From 9c9a00047dced85c2057ba4e621f808ce785e8ff Mon Sep 17 00:00:00 2001 From: Mark Unwin Date: Mon, 18 Mar 2024 10:00:57 +1000 Subject: [PATCH] Improved error catching in NMIS Integrations helper. --- app/Helpers/integrations_nmis_helper.php | 42 +++++++++++++++--------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/app/Helpers/integrations_nmis_helper.php b/app/Helpers/integrations_nmis_helper.php index 47f77ee56..4b58a6f45 100644 --- a/app/Helpers/integrations_nmis_helper.php +++ b/app/Helpers/integrations_nmis_helper.php @@ -874,7 +874,17 @@ function integrations_create($integration, $devices) unlink($ckfile); return array(); } - $external_device = @json_decode($output); + try { + $external_device = json_decode($output); + } catch (Exception $e) { + $sql = "INSERT INTO integrations_log VALUES (null, ?, null, ?, 'error', '[integrations_create] Invalid JSON in result from NMIS. Result: ' . (string)$output)"; + $data = array($integration->id, microtime(true)); + $query = $CI->db->query($sql, $data); + log_message('error', '[integrations_create] Invalid JSON in result from NMIS. Result: ' . (string)$output); + curl_close($ch); + unlink($ckfile); + return array(); + } if (empty($external_device)) { $message = '[integrations_create] No JSON in result from NMIS. Result: ' . (string)$output; log_message('error', $message); @@ -883,21 +893,21 @@ function integrations_create($integration, $devices) curl_close($ch); unlink($ckfile); return array(); - } else { - if (empty($external_device->error)) { - $external_devices[] = $external_device; - $message = '[integrations_create] Device ' . $device->configuration->host . ' created in NMIS.'; - $count = count($external_devices); - $sql = "INSERT INTO integrations_log VALUES (null, ?, null, ?, 'info', ?)"; - $db->query($sql, [$integration->id, microtime(true), $message]); - if ($integration->attributes->debug) { - $message = '[integrations_create] Received device creation data: ' . $output; - $sql = "INSERT INTO integrations_log VALUES (null, ?, null, ?, 'debug', ?)"; - $db->query($sql, [$integration->id, microtime(true), $message]); - } - } else { - $message = '[integrations_create] Error: ' . $external_device->error; - $sql = "INSERT INTO integrations_log VALUES (null, ?, null, ?, 'error', ?)"; + } + if (!empty($external_device->error)) { + $message = '[integrations_create] Error: ' . $external_device->error; + $sql = "INSERT INTO integrations_log VALUES (null, ?, null, ?, 'error', ?)"; + $db->query($sql, [$integration->id, microtime(true), $message]); + } + if (empty($external_device->error)) { + $external_devices[] = $external_device; + $message = '[integrations_create] Device ' . $device->configuration->host . ' created in NMIS.'; + $count = count($external_devices); + $sql = "INSERT INTO integrations_log VALUES (null, ?, null, ?, 'info', ?)"; + $db->query($sql, [$integration->id, microtime(true), $message]); + if ($integration->attributes->debug) { + $message = '[integrations_create] Received device creation data: ' . $output; + $sql = "INSERT INTO integrations_log VALUES (null, ?, null, ?, 'debug', ?)"; $db->query($sql, [$integration->id, microtime(true), $message]); } }