Skip to content

A free open source IT asset/license management system

Better sync error logging

snipe6f71a740134d
diff --git a/app/Console/Commands/PullInventory.php b/app/Console/Commands/PullInventory.php
index 532f163357..05ba7f450a 100644
--- a/app/Console/Commands/PullInventory.php
+++ b/app/Console/Commands/PullInventory.php
@@ -5,6 +5,7 @@ namespace App\Console\Commands;
use App\Models\Actionlog;
use App\Models\SyncAdapterInstance;
use App\SyncAdapters\SyncAdapter;
+use App\SyncAdapters\SyncErrorFormatter;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Throwable;
@@ -138,17 +139,20 @@ class PullInventory extends Command
}
});
} catch (Throwable $e) {
- $message = $e->getMessage();
- $abortSummary = "Sync aborted: {$message}";
+ // Brief summary for the UI "Last Synced" field and the
+ // console error output.
+ $summary = SyncErrorFormatter::summary($e);
+ $abortSummary = "Sync aborted: {$summary}";
$instance->last_synced_at = now();
$instance->last_sync_result = $abortSummary;
$instance->save();
$elapsed = self::formatElapsed($startedAt);
$this->error("{$slug} {$abortSummary} ({$elapsed})");
- Log::channel('sync-adapters')->warning("{$slug} sync aborted after {$seen} record(s), {$skipped} skipped, {$errors} error(s), elapsed {$elapsed}: {$message}", [
- 'exception' => $e,
- ]);
+ Log::channel('sync-adapters')->warning(
+ "{$slug} sync aborted after {$seen} record(s), {$skipped} skipped, {$errors} error(s), elapsed {$elapsed}: {$summary}",
+ SyncErrorFormatter::logContext($e),
+ );
$rows[] = [$slug, 'Aborted', $seen, $skipped, $errors, $elapsed];
return self::FAILURE;
diff --git a/app/SyncAdapters/SyncErrorFormatter.php b/app/SyncAdapters/SyncErrorFormatter.php
new file mode 100644
index 0000000000..d9bd3770e3
--- /dev/null
+++ b/app/SyncAdapters/SyncErrorFormatter.php
@@ -0,0 +1,178 @@
+<?php
+
+namespace App\SyncAdapters;
+
+use App\Exceptions\SyncAdapterVendorException;
+use Illuminate\Http\Client\ConnectionException;
+use Illuminate\Http\Client\RequestException;
+use Illuminate\Http\Client\Response;
+use Throwable;
+
+class SyncErrorFormatter
+{
+ /**
+ * Short, UI-safe one-liner used by the "Last Synced" field on the
+ * sync-adapter settings page and by the log-line headline.
+ *
+ * For an HTTP error, returns "HTTP 401: Forbidden" when the vendor
+ * body yields something readable, "HTTP 401" otherwise.
+ * For a connection error, returns the configured network-failure
+ * translation.
+ * For a vendor-raised exception (wrong base URL, unexpected content
+ * type), returns the exception's own actionable message.
+ * Everything else falls back to the exception's short class name.
+ */
+ public static function summary(Throwable $e): string
+ {
+ if ($e instanceof RequestException) {
+ $status = $e->response->status();
+ $vendorMessage = self::extractVendorErrorMessage($e->response);
+
+ return $vendorMessage !== null
+ ? sprintf('HTTP %d: %s', $status, $vendorMessage)
+ : 'HTTP '.$status;
+ }
+
+ if ($e instanceof ConnectionException) {
+ return trans('admin/settings/sync_adapters.sync_failed_network');
+ }
+
+ if ($e instanceof SyncAdapterVendorException) {
+ return $e->getMessage();
+ }
+
+ return class_basename($e);
+ }
+
+ /**
+ * Structured context fields for the sync-adapters log channel.
+ * Carries the full, unescaped vendor response body and the raw
+ * HTTP status rather than Laravel's RequestException-constructed
+ * message, which is both truncated and re-escaped.
+ *
+ * @return array<string, mixed>
+ */
+ public static function logContext(Throwable $e): array
+ {
+ $context = ['exception' => $e];
+
+ if ($e instanceof RequestException) {
+ $context['http_status'] = $e->response->status();
+ $context['response_body'] = $e->response->body();
+ $vendorMessage = self::extractVendorErrorMessage($e->response);
+ if ($vendorMessage !== null) {
+ $context['vendor_message'] = $vendorMessage;
+ }
+ }
+
+ return $context;
+ }
+
+ /**
+ * Extract the first human-readable message from a vendor error
+ * response body. Handles the common shapes:
+ * - {"message": "..."} Fleet, WS1, JumpCloud, Mosyle
+ * - {"detail": "..."} DRF-based (Zentral, some others)
+ * - {"error": "..."} plain string
+ * - {"error": {"message": "..."}} Microsoft Graph
+ * - {"errorMessage": "..."} NinjaOne
+ * - {"errors": [{"description|reason|message|detail": "..."}]} Jamf, Fleet nested
+ * - {"errors": ["..."]} Meraki
+ * Response bodies get truncated so a vendor returning a novel
+ * doesn't blow up the flash bar.
+ */
+ public static function extractVendorErrorMessage(Response $response): ?string
+ {
+ $body = $response->json();
+ if (! is_array($body)) {
+ return null;
+ }
+
+ $candidate = self::pluckErrorCandidate($body);
+ if (! is_string($candidate) || trim($candidate) === '') {
+ return null;
+ }
+
+ $candidate = trim($candidate);
+
+ return mb_strlen($candidate) > 200
+ ? mb_substr($candidate, 0, 200).'…'
+ : $candidate;
+ }
+
+ /**
+ * Walk the shapes we know about in order (top-level scalar keys,
+ * nested error object, errors array of strings, errors array of
+ * objects). Returns the first string it lands on or null if none
+ * of the paths yield anything.
+ *
+ * @param array<string, mixed> $body
+ */
+ private static function pluckErrorCandidate(array $body): ?string
+ {
+ $candidate = $body['message']
+ ?? $body['detail']
+ ?? $body['errorMessage']
+ ?? null;
+ if (is_string($candidate)) {
+ return $candidate;
+ }
+
+ $candidate = self::pluckErrorFromErrorKey($body);
+ if (is_string($candidate)) {
+ return $candidate;
+ }
+
+ return self::pluckErrorFromErrorsArray($body);
+ }
+
+ /**
+ * Extract from a top-level `error` key. Handles both the string
+ * shape ({"error": "..."}) and the Microsoft Graph nested-object
+ * shape ({"error": {"message": "..."}}).
+ *
+ * @param array<string, mixed> $body
+ */
+ private static function pluckErrorFromErrorKey(array $body): ?string
+ {
+ if (! isset($body['error'])) {
+ return null;
+ }
+ if (is_string($body['error'])) {
+ return $body['error'];
+ }
+ if (is_array($body['error']) && isset($body['error']['message']) && is_string($body['error']['message'])) {
+ return $body['error']['message'];
+ }
+
+ return null;
+ }
+
+ /**
+ * Extract from an `errors` array. First entry wins. Handles the
+ * Meraki string-array shape and the Jamf / Fleet object-array
+ * shape whose entries carry description / reason / message /
+ * detail keys.
+ *
+ * @param array<string, mixed> $body
+ */
+ private static function pluckErrorFromErrorsArray(array $body): ?string
+ {
+ if (! isset($body['errors']) || ! is_array($body['errors'])) {
+ return null;
+ }
+ $first = $body['errors'][0] ?? null;
+ if (is_string($first)) {
+ return $first;
+ }
+ if (! is_array($first)) {
+ return null;
+ }
+
+ return $first['description']
+ ?? $first['reason']
+ ?? $first['message']
+ ?? $first['detail']
+ ?? null;
+ }
+}