From adca50a372255e8869c5ec06398783ac506cb6e0 Mon Sep 17 00:00:00 2001 From: Charles Date: Thu, 20 Mar 2025 10:38:24 -0400 Subject: [PATCH] Catch 500 on backup page when you hit the backup rate limit (#1132) * Catch backup throwable * phpstan * Update notification --- .../BackupResource/Pages/ListBackups.php | 30 ++++++++++++------- .../Backups/InitiateBackupService.php | 2 +- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/app/Filament/Server/Resources/BackupResource/Pages/ListBackups.php b/app/Filament/Server/Resources/BackupResource/Pages/ListBackups.php index 25810133e..6377d6d06 100644 --- a/app/Filament/Server/Resources/BackupResource/Pages/ListBackups.php +++ b/app/Filament/Server/Resources/BackupResource/Pages/ListBackups.php @@ -31,6 +31,7 @@ use Filament\Tables\Columns\IconColumn; use Filament\Tables\Columns\TextColumn; use Filament\Tables\Table; use Illuminate\Http\Request; +use Symfony\Component\HttpKernel\Exception\HttpException; class ListBackups extends ListRecords { @@ -166,18 +167,27 @@ class ListBackups extends ListRecords $action->setIsLocked((bool) $data['is_locked']); } - $backup = $action->handle($server, $data['name']); + try { + $backup = $action->handle($server, $data['name']); - Activity::event('server:backup.start') - ->subject($backup) - ->property(['name' => $backup->name, 'locked' => (bool) $data['is_locked']]) - ->log(); + Activity::event('server:backup.start') + ->subject($backup) + ->property(['name' => $backup->name, 'locked' => (bool) $data['is_locked']]) + ->log(); - return Notification::make() - ->title('Backup Created') - ->body($backup->name . ' created.') - ->success() - ->send(); + return Notification::make() + ->title('Backup Created') + ->body($backup->name . ' created.') + ->success() + ->send(); + + } catch (HttpException $e) { + return Notification::make() + ->danger() + ->title('Backup Failed') + ->body($e->getMessage() . ' Try again' . ($e->getHeaders()['Retry-After'] ? ' in ' . $e->getHeaders()['Retry-After'] . ' seconds.' : '')) + ->send(); + } }), ]; } diff --git a/app/Services/Backups/InitiateBackupService.php b/app/Services/Backups/InitiateBackupService.php index 7a60da8fc..98002b5ea 100644 --- a/app/Services/Backups/InitiateBackupService.php +++ b/app/Services/Backups/InitiateBackupService.php @@ -85,7 +85,7 @@ class InitiateBackupService if ($previous->count() >= $limit) { $message = sprintf('Only %d backups may be generated within a %d second span of time.', $limit, $period); - throw new TooManyRequestsHttpException((int) now()->diffInSeconds($previous->last()->created_at->addSeconds($period)), $message); + throw new TooManyRequestsHttpException((int) now()->diffInSeconds($previous->last()->created_at->addSeconds((int) $period)), $message); } }