Merge pull request #3033 from Haendlerbund/flexible-exception-handling
Improve exception handler
This commit is contained in:
commit
f3a49b35de
|
|
@ -3,9 +3,6 @@
|
|||
namespace Webkul\Admin\Providers;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Webkul\Admin\Providers\EventServiceProvider;
|
||||
use Illuminate\Contracts\Debug\ExceptionHandler;
|
||||
use Webkul\Admin\Exceptions\Handler;
|
||||
use Webkul\Core\Tree;
|
||||
|
||||
class AdminServiceProvider extends ServiceProvider
|
||||
|
|
@ -32,11 +29,6 @@ class AdminServiceProvider extends ServiceProvider
|
|||
$this->registerACL();
|
||||
|
||||
$this->app->register(EventServiceProvider::class);
|
||||
|
||||
$this->app->bind(
|
||||
ExceptionHandler::class,
|
||||
Handler::class
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -96,7 +88,7 @@ class AdminServiceProvider extends ServiceProvider
|
|||
view()->composer(['admin::users.roles.create', 'admin::users.roles.edit'], function ($view) {
|
||||
$view->with('acl', $this->createACL());
|
||||
});
|
||||
|
||||
|
||||
view()->composer(['admin::catalog.products.create'], function ($view) {
|
||||
$items = array();
|
||||
|
||||
|
|
|
|||
|
|
@ -1,15 +1,15 @@
|
|||
<?php
|
||||
|
||||
namespace Webkul\Admin\Exceptions;
|
||||
namespace Webkul\Core\Exceptions;
|
||||
|
||||
use Exception;
|
||||
use Illuminate\Auth\AuthenticationException;
|
||||
use Illuminate\Database\Eloquent\PDOException;
|
||||
use Doctrine\DBAL\Driver\PDOException;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
|
||||
use App\Exceptions\Handler as AppExceptionHandler;
|
||||
|
||||
class Handler extends ExceptionHandler
|
||||
class Handler extends AppExceptionHandler
|
||||
{
|
||||
protected $jsonErrorMessages = [
|
||||
404 => 'Resource not found',
|
||||
|
|
@ -2,11 +2,13 @@
|
|||
|
||||
namespace Webkul\Core\Providers;
|
||||
|
||||
use Illuminate\Contracts\Debug\ExceptionHandler;
|
||||
use Illuminate\Database\Eloquent\Factory as EloquentFactory;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Foundation\AliasLoader;
|
||||
use Webkul\Core\Core;
|
||||
use Webkul\Core\Exceptions\Handler;
|
||||
use Webkul\Core\Facades\Core as CoreFacade;
|
||||
use Webkul\Core\Models\SliderProxy;
|
||||
use Webkul\Core\Observers\SliderObserver;
|
||||
|
|
@ -41,6 +43,11 @@ class CoreServiceProvider extends ServiceProvider
|
|||
dirname(__DIR__) . '/Config/concord.php' => config_path('concord.php'),
|
||||
]);
|
||||
|
||||
$this->app->bind(
|
||||
ExceptionHandler::class,
|
||||
Handler::class
|
||||
);
|
||||
|
||||
SliderProxy::observe(SliderObserver::class);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,79 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Webkul\Shop\Exceptions;
|
||||
|
||||
use Exception;
|
||||
use Illuminate\Auth\AuthenticationException;
|
||||
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
|
||||
{
|
||||
protected $jsonErrorMessages = [
|
||||
404 => 'Resource not found',
|
||||
403 => '403 forbidden Error',
|
||||
401 => 'Unauthenticated',
|
||||
500 => '500 Internal Server Error',
|
||||
];
|
||||
|
||||
/**
|
||||
* Render an exception into an HTTP response.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Exception $exception
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function render($request, Exception $exception)
|
||||
{
|
||||
$path = 'shop';
|
||||
|
||||
if ($exception instanceof HttpException) {
|
||||
$statusCode = in_array($exception->getStatusCode(), [401, 403, 404, 503]) ? $exception->getStatusCode() : 500;
|
||||
|
||||
return $this->response($path, $statusCode);
|
||||
} elseif ($exception instanceof ModelNotFoundException) {
|
||||
return $this->response($path, 404);
|
||||
} elseif ($exception instanceof PDOException) {
|
||||
return $this->response($path, 500);
|
||||
}
|
||||
|
||||
return parent::render($request, $exception);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert an authentication exception into a response.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Illuminate\Auth\AuthenticationException $exception
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
protected function unauthenticated($request, AuthenticationException $exception)
|
||||
{
|
||||
if ($request->expectsJson()) {
|
||||
return response()->json(['error' => $this->jsonErrorMessages[401]], 401);
|
||||
}
|
||||
|
||||
return redirect()->guest(route('auth.login'));
|
||||
}
|
||||
|
||||
private function isSopUri()
|
||||
{
|
||||
return strpos(\Illuminate\Support\Facades\Request::path(), 'shop') !== false ? true : false;
|
||||
}
|
||||
|
||||
private function response($path, $statusCode)
|
||||
{
|
||||
if (request()->expectsJson()) {
|
||||
return response()->json([
|
||||
'error' => isset($this->jsonErrorMessages[$statusCode])
|
||||
? $this->jsonErrorMessages[$statusCode]
|
||||
: 'Something went wrong, please try again later.'
|
||||
], $statusCode);
|
||||
}
|
||||
|
||||
return response()->view("{$path}::errors.{$statusCode}", [], $statusCode);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue