diff --git a/packages/Webkul/Admin/src/Exceptions/Handler.php b/packages/Webkul/Admin/src/Exceptions/Handler.php index 4a6d2d88f..109c25392 100644 --- a/packages/Webkul/Admin/src/Exceptions/Handler.php +++ b/packages/Webkul/Admin/src/Exceptions/Handler.php @@ -4,11 +4,11 @@ namespace Webkul\Admin\Exceptions; use Exception; use Illuminate\Auth\AuthenticationException; -use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; -use Symfony\Component\HttpKernel\Exception\HttpException; -use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Database\Eloquent\PDOException; use Illuminate\Database\Eloquent\ErrorException; +use Illuminate\Database\Eloquent\ModelNotFoundException; +use Symfony\Component\HttpKernel\Exception\HttpException; +use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; class Handler extends ExceptionHandler { @@ -22,60 +22,38 @@ class Handler extends ExceptionHandler */ public function render($request, Exception $exception) { + $path = $this->isAdminUri() ? 'admin' : 'shop'; if ($exception instanceof HttpException) { $statusCode = $exception->getStatusCode(); - if (strpos($_SERVER['REQUEST_URI'], 'admin') !== false) { - switch ($statusCode) { - case 404: - return response()->view('admin::errors.404', [], 404); - break; - case 403: - return response()->view('admin::errors.403', [], 403); - break; - case 401: - return response()->view('admin::errors.401', [], 401); - break; - default: - return response()->view('admin::errors.500', [], 500); - } - } else { - switch ($statusCode) { - case 404: - return response()->view('shop::errors.404', [], 404); - break; - case 403: - return response()->view('shop::errors.403', [], 403); - break; - case 401: - return response()->view('shop::errors.401', [], 401); - break; - default: - return response()->view('shop::errors.500', [], 500); - } - } - } else if ($exception instanceof \ModelNotFoundException) { - if (strpos($_SERVER['REQUEST_URI'], 'admin') !== false){ - return response()->view('admin::errors.404', [], 404); - }else { - return response()->view('shop::errors.404', [], 404); - } - } else if ($exception instanceof \PDOException) { - if (strpos($_SERVER['REQUEST_URI'], 'admin') !== false){ - return response()->view('admin::errors.500', [], 500); - } else { - return response()->view('shop::errors.500', [], 500); - } + $statusCode = in_array($statusCode, [401, 403, 404]) ? $statusCode : 500; + return $this->response($path, $statusCode); + } else if ($exception instanceof ModelNotFoundException) { + return $this->response($path, 404); + } else if ($exception instanceof PDOException) { + return $this->response($path, 500); } - // else if ($exception instanceof \ErrorException) { + // else if ($exception instanceof ErrorException) { // if(strpos($_SERVER['REQUEST_URI'], 'admin') !== false){ // return response()->view('admin::errors.500', [], 500); // }else { // return response()->view('shop::errors.500', [], 500); // } + // } return parent::render($request, $exception); } + + private function isAdminUri() + { + return strpos($_SERVER['REQUEST_URI'], 'admin') !== false ? true : false; + } + + private function response($path, $statusCode) + { + return response()->view("{$path}::errors.{$statusCode}", [], $statusCode); + } + } \ No newline at end of file