2018-10-17 07:40:08 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Webkul\Admin\Exceptions;
|
|
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
|
use Illuminate\Auth\AuthenticationException;
|
2018-10-25 08:07:21 +00:00
|
|
|
use Illuminate\Database\Eloquent\PDOException;
|
2018-12-01 10:03:32 +00:00
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
|
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
|
|
|
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
|
2018-10-17 07:40:08 +00:00
|
|
|
|
|
|
|
|
class Handler extends ExceptionHandler
|
|
|
|
|
{
|
2019-03-13 11:17:52 +00:00
|
|
|
protected $jsonErrorMessages = [
|
|
|
|
|
404 => 'Resource not found',
|
|
|
|
|
403 => '403 forbidden Error',
|
|
|
|
|
401 => 'Unauthenticated',
|
|
|
|
|
500 => '500 Internal Server Error',
|
|
|
|
|
];
|
2018-10-17 07:40:08 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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)
|
|
|
|
|
{
|
2019-11-05 13:32:07 +00:00
|
|
|
$path = $this->isAdminUri() ? 'admin' : 'shop';
|
2018-11-29 12:09:39 +00:00
|
|
|
|
2018-10-17 07:40:08 +00:00
|
|
|
if ($exception instanceof HttpException) {
|
2019-03-13 11:17:52 +00:00
|
|
|
$statusCode = in_array($exception->getStatusCode(), [401, 403, 404, 503]) ? $exception->getStatusCode() : 500;
|
|
|
|
|
|
2018-12-01 10:03:32 +00:00
|
|
|
return $this->response($path, $statusCode);
|
2020-02-24 12:19:12 +00:00
|
|
|
} elseif ($exception instanceof ModelNotFoundException) {
|
2018-12-01 10:03:32 +00:00
|
|
|
return $this->response($path, 404);
|
2020-02-24 12:19:12 +00:00
|
|
|
} elseif ($exception instanceof PDOException) {
|
2018-12-01 10:03:32 +00:00
|
|
|
return $this->response($path, 500);
|
2018-10-17 07:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return parent::render($request, $exception);
|
|
|
|
|
}
|
2018-12-01 10:03:32 +00:00
|
|
|
|
2019-03-13 11:17:52 +00:00
|
|
|
/**
|
|
|
|
|
* 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'));
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-01 10:03:32 +00:00
|
|
|
private function isAdminUri()
|
|
|
|
|
{
|
2019-10-19 09:59:45 +00:00
|
|
|
return strpos(\Illuminate\Support\Facades\Request::path(), 'admin') !== false ? true : false;
|
2018-12-01 10:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function response($path, $statusCode)
|
|
|
|
|
{
|
2019-03-13 11:17:52 +00:00
|
|
|
if (request()->expectsJson()) {
|
|
|
|
|
return response()->json([
|
|
|
|
|
'error' => isset($this->jsonErrorMessages[$statusCode])
|
|
|
|
|
? $this->jsonErrorMessages[$statusCode]
|
|
|
|
|
: 'Something went wrong, please try again later.'
|
|
|
|
|
], $statusCode);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-01 10:03:32 +00:00
|
|
|
return response()->view("{$path}::errors.{$statusCode}", [], $statusCode);
|
|
|
|
|
}
|
2018-10-17 07:40:08 +00:00
|
|
|
}
|