From 6a72b7150b14eaaf4f5fca9c0a0fdcd3e82a2390 Mon Sep 17 00:00:00 2001 From: Oskar-Mikael Date: Sun, 31 Aug 2025 22:41:48 +0200 Subject: [PATCH] Show error --- app/Bots/GenericApi.php | 32 +++++++++------------ app/Jobs/RunBot.php | 11 ++++--- app/Livewire/BotLogs.php | 22 ++++++++------ app/Livewire/BotsList.php | 2 +- app/Livewire/ViewBot.php | 2 +- app/Services/BotService.php | 2 +- resources/views/livewire/bot-logs.blade.php | 17 +++++++---- 7 files changed, 49 insertions(+), 39 deletions(-) diff --git a/app/Bots/GenericApi.php b/app/Bots/GenericApi.php index a53b5d9..f7e2a78 100644 --- a/app/Bots/GenericApi.php +++ b/app/Bots/GenericApi.php @@ -19,27 +19,23 @@ class GenericApi implements BotContract public function run(): void { - try { - $this->client = new Client(); + $this->client = new Client(); - $options = []; + $options = []; - if (!empty($this->config['headers'])) { - $options['headers'] = json_decode($this->config['headers'], true); - } - - if (!empty($this->config['body'])) { - $options['json'] = json_decode($this->config['body'], true); - } - - $this->client->request( - $this->config['method'] ?? 'GET', - $this->config['url'], - $options - ); - } catch (GuzzleException $e) { - Log::error("Call to {$this->config['url']} failed" . $e->getMessage()); + if (!empty($this->config['headers'])) { + $options['headers'] = json_decode($this->config['headers'], true); } + + if (!empty($this->config['body'])) { + $options['json'] = json_decode($this->config['body'], true); + } + + $this->client->request( + $this->config['method'] ?? 'GET', + $this->config['url'], + $options + ); } public static function configSchema(): array diff --git a/app/Jobs/RunBot.php b/app/Jobs/RunBot.php index 59dc610..e3574f6 100644 --- a/app/Jobs/RunBot.php +++ b/app/Jobs/RunBot.php @@ -14,27 +14,30 @@ class RunBot implements ShouldQueue /** * Create a new job instance. */ - public function __construct(private Bot $bot, private BotLog $log) {} + public function __construct(private int $bot_id, private int $log_id) {} /** * Execute the job. */ public function handle(): void { - $class = new $this->bot->class($this->bot->config ?? []); + $bot = Bot::findOrFail($this->bot_id); + $log = BotLog::findOrFail($this->log_id); try { + $class = new $bot->class($bot->config ?? []); + $class->run(); // Update the log entry on success - $this->log->update([ + $log->update([ 'finished_at' => now(), 'status' => 'success', // 'output' => is_string($result) ? $result : json_encode($result, JSON_PRETTY_PRINT), ]); } catch (\Throwable $e) { // Log the error in the bot log - $this->log->update([ + $log->update([ 'finished_at' => now(), 'status' => 'failed', 'error' => $e->getMessage(), diff --git a/app/Livewire/BotLogs.php b/app/Livewire/BotLogs.php index f199c8e..efd3bb1 100644 --- a/app/Livewire/BotLogs.php +++ b/app/Livewire/BotLogs.php @@ -4,6 +4,7 @@ namespace App\Livewire; use App\Models\BotLog; use Illuminate\Support\Facades\Auth; +use Jantinnerezo\LivewireAlert\Facades\LivewireAlert; use Livewire\Component; use Livewire\WithoutUrlPagination; use Livewire\WithPagination; @@ -13,15 +14,18 @@ class BotLogs extends Component use WithPagination; use WithoutUrlPagination; - // public function mount() - // { - // $this->getLogs(); - // } - - // public function getLogs(): void - // { - // $this->logs = Auth::user()->bots->pluck('logs')->flatten()->sortByDesc('created_at'); - // } + public function showError(int $logId): void + { + $log = BotLog::find($logId); + if ($log) { + LivewireAlert::title('Error Details') + ->html("
{$log->error}
") + ->warning() + ->timer(null) + ->withCancelButton('Close') + ->show(); + } + } public function render() { diff --git a/app/Livewire/BotsList.php b/app/Livewire/BotsList.php index cc13936..9664bd9 100644 --- a/app/Livewire/BotsList.php +++ b/app/Livewire/BotsList.php @@ -78,7 +78,7 @@ class BotsList extends Component ]); // Dispatch the job to run the bot - dispatch(new RunBot($bot, $log)); + dispatch(new RunBot($bot->id, $log->id)); flash()->success("Bot '{$bot->name}' is being executed."); } diff --git a/app/Livewire/ViewBot.php b/app/Livewire/ViewBot.php index e2c239b..449bdc0 100644 --- a/app/Livewire/ViewBot.php +++ b/app/Livewire/ViewBot.php @@ -26,7 +26,7 @@ class ViewBot extends Component ]); // Dispatch the job to run the bot - dispatch(new RunBot($this->bot, $log)); + dispatch(new RunBot($this->bot->id, $log->id)); flash()->success("Bot '{$this->bot->name}' is being executed."); } diff --git a/app/Services/BotService.php b/app/Services/BotService.php index 8074e98..2cc0cb2 100644 --- a/app/Services/BotService.php +++ b/app/Services/BotService.php @@ -26,7 +26,7 @@ class BotService $instance = app($bot->class, ['config' => $bot->config]); if ($instance instanceof BotContract) { - dispatch(RunBot::class, $bot, $log); + dispatch(RunBot::class, $bot->id, $log->id); $log->update([ 'started_at' => now(), 'status' => 'running' diff --git a/resources/views/livewire/bot-logs.blade.php b/resources/views/livewire/bot-logs.blade.php index 28b59cb..8dd6502 100644 --- a/resources/views/livewire/bot-logs.blade.php +++ b/resources/views/livewire/bot-logs.blade.php @@ -16,19 +16,26 @@ $log->status === 'failed', - 'text-green-400' => $log->status === 'success', - 'text-yellow-400' => $log->status === 'pending', + "text-red-400" => $log->status === "failed", + "text-green-400" => $log->status === "success", + "text-yellow-400" => $log->status === "pending", ]), class="px-4 py-2" + @if ($log->status === "failed") + wire:click="showError({{ $log->id }})" + style="cursor: pointer;" + @endif > {{ $log->status }} + @if ($log->status === "failed") + + @endif - {{ $log->started_at ?? '' }} + {{ $log->started_at ?? "" }} - {{ $log->finished_at ?? '' }} + {{ $log->finished_at ?? "" }} @endforeach