Skip to content

Commit

Permalink
Improved error catching in NMIS Integrations helper.
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-unwin committed Mar 18, 2024
1 parent 68f0402 commit 9c9a000
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions app/Helpers/integrations_nmis_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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]);
}
}
Expand Down

0 comments on commit 9c9a000

Please sign in to comment.