Skip to content

Commit

Permalink
Refactor error handling and URL parsing logic
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeWithDennis committed Jan 29, 2025
1 parent b905f35 commit aba409a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
2 changes: 1 addition & 1 deletion resources/views/404.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
Go back to previous page
</x-filament::button>
@endif

<x-filament::button icon="heroicon-s-home" tag="a" color="primary" :href="\Filament\Facades\Filament::getCurrentPanel()->getUrl()">
Go back to home
</x-filament::button>
Expand Down
34 changes: 20 additions & 14 deletions src/FilamentErrorPagesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Cmsmaxinc\FilamentErrorPages;

use Filament\Facades\Filament;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Routing\Redirector;
use Illuminate\Support\Facades\App;
Expand Down Expand Up @@ -46,7 +45,6 @@ public function packageRegistered(): void {}

public function packageBooted(): void
{
// TODO: Parse the URL (https://leadpulse.test/company/52/woops) grab the firt part /company/52 and append the /woops to it
$this->registerCustomErrorHandler();
}

Expand All @@ -68,24 +66,32 @@ protected function registerCustomErrorHandler(): void
// Set the current panel if it exists in the available panels
if (filament()->getPanels()[$panelName] ?? false) {
filament()->setCurrentPanel($panel);
}

// Check if the previous request was redirected to the woops page
$isRedirected = $request->url() === route(
'filament.' . $panel->getId() . '.pages.woops',
filament()->getCurrentPanel()->getTenantModel() ? $tenantId : null
);
// Get the plugins of the current panel
$plugins = filament()->getCurrentPanel()->getPlugins();

// Handle NotFoundHttpException for panels
if ($exception instanceof NotFoundHttpException && ! $isRedirected) {
$isDefaultPanel = filament()->getCurrentPanel()->getId() === filament()->getDefaultPanel()->getId();
// Check if the FilamentErrorPagesPlugin is used by the current panel
$usedByPanel = collect($plugins)->first(fn ($plugin) => $plugin instanceof FilamentErrorPagesPlugin);

if (filament()->getPanels()[$panelName] ?? $isDefaultPanel) {
// https://github.com/livewire/livewire/discussions/4905#discussioncomment-7115155
return (new Redirector(App::get('url')))->route(
if ($usedByPanel) {
// Check if the previous request was redirected to the woops page
$isRedirected = $request->url() === route(
'filament.' . $panel->getId() . '.pages.woops',
filament()->getCurrentPanel()->getTenantModel() ? $tenantId : null
);

// Handle NotFoundHttpException for panels
if ($exception instanceof NotFoundHttpException && ! $isRedirected) {
$isDefaultPanel = filament()->getCurrentPanel()->getId() === filament()->getDefaultPanel()->getId();

if (filament()->getPanels()[$panelName] ?? $isDefaultPanel) {
// https://github.com/livewire/livewire/discussions/4905#discussioncomment-7115155
return (new Redirector(App::get('url')))->route(
'filament.' . $panel->getId() . '.pages.woops',
filament()->getCurrentPanel()->getTenantModel() ? $tenantId : null
);
}
}
}
}

Expand Down

0 comments on commit aba409a

Please sign in to comment.