<?php
 namespace App\Exceptions; use App\Exceptions\NotFoundResolver; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; use Throwable; class Handler extends ExceptionHandler { protected $dontFlash = [ 'current_password', 'password', 'password_confirmation', ]; public function register(): void { $this->reportable(function (Throwable $e) { }); } public function render($request, Throwable $e) { if ( ! config('app.debug') && ! $request->expectsJson() && $e instanceof \Illuminate\Database\Eloquent\ModelNotFoundException ) { try { $context = $this->resolveContext($request); $this->prepareForContext($context); $resolver = $this->container->make(NotFoundResolver::class); $payload = $resolver->resolve($e, $context); return response()->view("errors.{$context}.404", $payload, 404); } catch (\Throwable $renderError) { report($renderError); } } return parent::render($request, $e); } protected function renderHttpException(HttpExceptionInterface $e) { if (config('app.debug')) { return parent::convertExceptionToResponse($e); } if ($e->getStatusCode() !== 404) { return parent::renderHttpException($e); } try { $request = request(); $context = $this->resolveContext($request); $this->prepareForContext($context); $resolver = $this->container->make(NotFoundResolver::class); $payload = $resolver->resolve($e, $context); return response()->view("errors.{$context}.404", $payload, 404); } catch (\Throwable $renderError) { report($renderError); return parent::renderHttpException($e); } } private function prepareForContext(string $context): void { if ($context === 'admin') { \Illuminate\Support\Facades\Auth::shouldUse('admin'); } if ($context === 'store' || $context === 'clientarea') { try { $locale = $this->container->make(\App\Services\Store\StorePreferenceResolver::class)->currentLanguage(); if ($locale) { app()->setLocale($locale); } $this->container->make(\App\Http\Middleware\SetStoreLocaleFromVisitor::class) ->shareLocaleDataWithViews(); } catch (\Throwable $e) { report($e); } } $factory = $this->container->make(\Illuminate\Contracts\View\Factory::class); if (! $factory->shared('errors')) { $factory->share('errors', new \Illuminate\Support\ViewErrorBag()); } } private function resolveContext(\Illuminate\Http\Request $request): string { if ($request->routeIs('admin.*') || $request->is('admin', 'admin/*')) { return 'admin'; } if ($request->routeIs('store.auth.*') || $request->is('clientarea', 'clientarea/*')) { return 'clientarea'; } return 'store'; } } 