diff --git a/app/Jobs/Speedtests/ExecuteOoklaSpeedtest.php b/app/Jobs/Speedtests/ExecuteOoklaSpeedtest.php index 3e3d591f9..3d298a7bc 100644 --- a/app/Jobs/Speedtests/ExecuteOoklaSpeedtest.php +++ b/app/Jobs/Speedtests/ExecuteOoklaSpeedtest.php @@ -61,11 +61,34 @@ public function handle(): void } catch (ProcessFailedException $exception) { $messages = explode(PHP_EOL, $exception->getMessage()); - $message = collect(array_filter($messages, 'json_validate'))->last(); - + // Extract only the "message" part from each JSON error message + $errorMessages = array_map(function ($message) { + $decoded = json_decode($message, true); + if (json_last_error() === JSON_ERROR_NONE && isset($decoded['message'])) { + return $decoded['message']; + } + + return ''; // If it's not valid JSON or doesn't contain "message", return an empty string + }, $messages); + + // Filter out empty messages and concatenate + $errorMessage = implode(' | ', array_filter($errorMessages)); + + // Prepare the error message data + $data = [ + 'type' => 'log', + 'level' => 'error', + 'message' => $errorMessage, + ]; + + // Add server ID if it exists + if ($this->serverId !== null) { + $data['server'] = ['id' => $this->serverId]; + } + + // Update the result with the error data $this->result->update([ - 'server_id' => $this->serverId, - 'data' => json_decode($message, true), + 'data' => $data, 'status' => ResultStatus::Failed, ]); @@ -87,6 +110,9 @@ public function handle(): void SpeedtestCompleted::dispatch($this->result); } + /** + * Check for internet connection. + */ protected function checkForInternetConnection(): bool { $url = config('speedtest.ping_url'); @@ -98,7 +124,6 @@ protected function checkForInternetConnection(): bool if (! URL::isValidUrl($url)) { $this->result->update([ - 'server_id' => $this->serverId, 'data' => [ 'type' => 'log', 'level' => 'error', @@ -122,7 +147,6 @@ protected function checkForInternetConnection(): bool if ($ping->ping() === false) { $this->result->update([ - 'server_id' => $this->serverId, 'data' => [ 'type' => 'log', 'level' => 'error',