Merge pull request #256 from AliN11/master

Optimized and minified ExceptionHandler
This commit is contained in:
JItendra Singh 2018-12-03 14:42:35 +05:30 committed by GitHub
commit 4977428cca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 23 additions and 45 deletions

View File

@ -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);
}
}