Remove exception methods because of memory bombing (#750)

* remove exception methods

* throw Halt instead of return

* manually throw Halt to make phpstan happy
This commit is contained in:
Boy132 2024-12-02 22:27:25 +01:00 committed by GitHub
parent c6977e57c8
commit 918ba02075
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 26 additions and 38 deletions

View File

@ -4,8 +4,6 @@ namespace App\Filament\Resources\DatabaseHostResource\Pages;
use App\Filament\Resources\DatabaseHostResource; use App\Filament\Resources\DatabaseHostResource;
use App\Services\Databases\Hosts\HostCreationService; use App\Services\Databases\Hosts\HostCreationService;
use Closure;
use Exception;
use Filament\Forms; use Filament\Forms;
use Filament\Forms\Components\Section; use Filament\Forms\Components\Section;
use Filament\Forms\Components\Select; use Filament\Forms\Components\Select;
@ -13,6 +11,7 @@ use Filament\Forms\Components\TextInput;
use Filament\Forms\Form; use Filament\Forms\Form;
use Filament\Notifications\Notification; use Filament\Notifications\Notification;
use Filament\Resources\Pages\CreateRecord; use Filament\Resources\Pages\CreateRecord;
use Filament\Support\Exceptions\Halt;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use PDOException; use PDOException;
@ -104,21 +103,18 @@ class CreateDatabaseHost extends CreateRecord
protected function handleRecordCreation(array $data): Model protected function handleRecordCreation(array $data): Model
{ {
try {
return $this->service->handle($data); return $this->service->handle($data);
} } catch (PDOException $exception) {
public function exception(Exception $e, Closure $stopPropagation): void
{
if ($e instanceof PDOException) {
Notification::make() Notification::make()
->title('Error connecting to database host') ->title('Error connecting to database host')
->body($e->getMessage()) ->body($exception->getMessage())
->color('danger') ->color('danger')
->icon('tabler-database') ->icon('tabler-database')
->danger() ->danger()
->send(); ->send();
$stopPropagation(); throw new Halt();
} }
} }
} }

View File

@ -6,8 +6,6 @@ use App\Filament\Resources\DatabaseHostResource;
use App\Filament\Resources\DatabaseHostResource\RelationManagers\DatabasesRelationManager; use App\Filament\Resources\DatabaseHostResource\RelationManagers\DatabasesRelationManager;
use App\Models\DatabaseHost; use App\Models\DatabaseHost;
use App\Services\Databases\Hosts\HostUpdateService; use App\Services\Databases\Hosts\HostUpdateService;
use Closure;
use Exception;
use Filament\Actions; use Filament\Actions;
use Filament\Forms; use Filament\Forms;
use Filament\Forms\Components\Section; use Filament\Forms\Components\Section;
@ -16,6 +14,7 @@ use Filament\Forms\Components\TextInput;
use Filament\Forms\Form; use Filament\Forms\Form;
use Filament\Notifications\Notification; use Filament\Notifications\Notification;
use Filament\Resources\Pages\EditRecord; use Filament\Resources\Pages\EditRecord;
use Filament\Support\Exceptions\Halt;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use PDOException; use PDOException;
@ -117,21 +116,18 @@ class EditDatabaseHost extends EditRecord
return $record; return $record;
} }
try {
return $this->hostUpdateService->handle($record, $data); return $this->hostUpdateService->handle($record, $data);
} } catch (PDOException $exception) {
public function exception(Exception $e, Closure $stopPropagation): void
{
if ($e instanceof PDOException) {
Notification::make() Notification::make()
->title('Error connecting to database host') ->title('Error connecting to database host')
->body($e->getMessage()) ->body($exception->getMessage())
->color('danger') ->color('danger')
->icon('tabler-database') ->icon('tabler-database')
->danger() ->danger()
->send(); ->send();
$stopPropagation(); throw new Halt();
} }
} }
} }

View File

@ -14,9 +14,7 @@ use chillerlan\QRCode\Common\EccLevel;
use chillerlan\QRCode\Common\Version; use chillerlan\QRCode\Common\Version;
use chillerlan\QRCode\QRCode; use chillerlan\QRCode\QRCode;
use chillerlan\QRCode\QROptions; use chillerlan\QRCode\QROptions;
use Closure;
use DateTimeZone; use DateTimeZone;
use Exception;
use Filament\Forms\Components\Actions; use Filament\Forms\Components\Actions;
use Filament\Forms\Components\Actions\Action; use Filament\Forms\Components\Actions\Action;
use Filament\Forms\Components\Grid; use Filament\Forms\Components\Grid;
@ -32,6 +30,7 @@ use Filament\Forms\Components\TextInput;
use Filament\Forms\Get; use Filament\Forms\Get;
use Filament\Notifications\Notification; use Filament\Notifications\Notification;
use Filament\Support\Enums\MaxWidth; use Filament\Support\Enums\MaxWidth;
use Filament\Support\Exceptions\Halt;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Hash;
@ -352,27 +351,24 @@ class EditProfile extends \Filament\Pages\Auth\EditProfile
} }
if ($token = $data['2fa-disable-code'] ?? null) { if ($token = $data['2fa-disable-code'] ?? null) {
try {
$this->toggleTwoFactorService->handle($record, $token, false); $this->toggleTwoFactorService->handle($record, $token, false);
} catch (TwoFactorAuthenticationTokenInvalid $exception) {
cache()->forget("users.$record->id.2fa.state");
}
return parent::handleRecordUpdate($record, $data);
}
public function exception(Exception $e, Closure $stopPropagation): void
{
if ($e instanceof TwoFactorAuthenticationTokenInvalid) {
Notification::make() Notification::make()
->title('Invalid 2FA Code') ->title('Invalid 2FA Code')
->body($e->getMessage()) ->body($exception->getMessage())
->color('danger') ->color('danger')
->icon('tabler-2fa') ->icon('tabler-2fa')
->danger() ->danger()
->send(); ->send();
$stopPropagation(); throw new Halt();
} }
cache()->forget("users.$record->id.2fa.state");
}
return parent::handleRecordUpdate($record, $data);
} }
protected function getFormActions(): array protected function getFormActions(): array