2017-06-09 02:28:26 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Exceptions;
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
use Illuminate\Auth\AuthenticationException;
|
2018-02-21 12:33:09 +08:00
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
2017-06-09 02:28:26 +08:00
|
|
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
|
2018-02-21 12:33:09 +08:00
|
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
use Log;
|
2018-02-23 00:14:12 +08:00
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
2017-06-09 02:28:26 +08:00
|
|
|
|
2018-02-23 05:15:00 +08:00
|
|
|
/**
|
|
|
|
* Class Handler
|
|
|
|
* @package App\Exceptions
|
|
|
|
*/
|
2017-06-09 02:28:26 +08:00
|
|
|
class Handler extends ExceptionHandler
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* A list of the exception types that should not be reported.
|
|
|
|
*/
|
|
|
|
protected $dontReport = [
|
|
|
|
\Illuminate\Auth\AuthenticationException::class,
|
|
|
|
\Illuminate\Auth\Access\AuthorizationException::class,
|
|
|
|
\Symfony\Component\HttpKernel\Exception\HttpException::class,
|
2018-02-05 01:53:02 +08:00
|
|
|
\Illuminate\Database\Eloquent\ModelNotFoundException::class,
|
2017-06-09 02:28:26 +08:00
|
|
|
\Illuminate\Session\TokenMismatchException::class,
|
|
|
|
\Illuminate\Validation\ValidationException::class,
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Report or log an exception.
|
|
|
|
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
|
2018-02-05 01:53:02 +08:00
|
|
|
* @param \Exception $exception
|
2017-06-09 02:28:26 +08:00
|
|
|
* @return void
|
2018-02-05 01:53:02 +08:00
|
|
|
* @throws Exception
|
2017-06-09 02:28:26 +08:00
|
|
|
*/
|
|
|
|
public function report(Exception $exception)
|
|
|
|
{
|
|
|
|
parent::report($exception);
|
|
|
|
}
|
|
|
|
|
2018-02-23 00:14:12 +08:00
|
|
|
/**
|
|
|
|
* Create an error message
|
|
|
|
* @param $status_code
|
|
|
|
* @param $message
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function createError($status_code, $message)
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'error' => [
|
|
|
|
'status' => $status_code,
|
|
|
|
'message' => $message,
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2017-06-09 02:28:26 +08:00
|
|
|
/**
|
|
|
|
* Render an exception into an HTTP response.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param \Exception $exception
|
2018-02-23 00:14:12 +08:00
|
|
|
* @return mixed
|
2017-06-09 02:28:26 +08:00
|
|
|
*/
|
|
|
|
public function render($request, Exception $exception)
|
|
|
|
{
|
2018-02-23 05:15:00 +08:00
|
|
|
if ($request->expectsJson() || $request->is('api/*')) {
|
2018-01-30 10:30:29 +08:00
|
|
|
|
2018-02-23 00:14:12 +08:00
|
|
|
$headers = [];
|
2018-01-30 10:30:29 +08:00
|
|
|
|
2018-02-23 00:14:12 +08:00
|
|
|
Log::error('API Error', $exception->getTrace());
|
2018-02-05 01:53:02 +08:00
|
|
|
|
2018-02-23 00:14:12 +08:00
|
|
|
if($exception instanceof ModelNotFoundException ||
|
|
|
|
$exception instanceof NotFoundHttpException) {
|
|
|
|
$error = $this->createError(404, $exception->getMessage());
|
2017-12-30 23:03:45 +08:00
|
|
|
}
|
|
|
|
|
2018-02-23 05:15:00 +08:00
|
|
|
# Custom exceptions should be extending HttpException
|
2018-02-23 00:14:12 +08:00
|
|
|
elseif ($exception instanceof HttpException) {
|
|
|
|
$error = $this->createError(
|
|
|
|
$exception->getStatusCode(),
|
|
|
|
$exception->getMessage()
|
|
|
|
);
|
2018-02-05 01:53:02 +08:00
|
|
|
|
2018-02-23 00:14:12 +08:00
|
|
|
$headers = $exception->getHeaders();
|
|
|
|
}
|
|
|
|
|
|
|
|
# Create the detailed errors from the validation errors
|
|
|
|
elseif($exception instanceof ValidationException) {
|
2018-02-05 01:53:02 +08:00
|
|
|
$error_messages = [];
|
2018-02-23 00:14:12 +08:00
|
|
|
$errors = $exception->errors();
|
2018-02-05 01:53:02 +08:00
|
|
|
foreach($errors as $field => $error) {
|
|
|
|
$error_messages[] = implode(', ', $error);
|
|
|
|
}
|
|
|
|
|
2018-02-23 00:14:12 +08:00
|
|
|
$message = implode(', ', $error_messages);
|
|
|
|
$error = $this->createError(400, $message);
|
2018-02-05 01:53:02 +08:00
|
|
|
$error['error']['errors'] = $errors;
|
2018-02-23 00:14:12 +08:00
|
|
|
|
2018-02-05 01:53:02 +08:00
|
|
|
Log::error('Validation errors', $errors);
|
2018-01-30 10:30:29 +08:00
|
|
|
}
|
|
|
|
|
2018-02-23 00:14:12 +08:00
|
|
|
else {
|
|
|
|
$error = $this->createError(400, $exception->getMessage());
|
|
|
|
}
|
|
|
|
|
2018-02-05 01:53:02 +08:00
|
|
|
# Only add trace if in dev
|
|
|
|
if(config('app.env') === 'dev') {
|
|
|
|
$error['error']['trace'] = $exception->getTrace()[0];
|
|
|
|
}
|
2018-01-25 04:14:47 +08:00
|
|
|
|
2018-02-23 00:14:12 +08:00
|
|
|
return response()->json($error, $error['error']['status'], $headers);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($exception instanceof HttpException
|
|
|
|
&& $exception->getStatusCode() === 403) {
|
|
|
|
return redirect()->guest('login');
|
2017-12-30 23:03:45 +08:00
|
|
|
}
|
|
|
|
|
2017-06-09 02:28:26 +08:00
|
|
|
return parent::render($request, $exception);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert an authentication exception into an unauthenticated response.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param \Illuminate\Auth\AuthenticationException $exception
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
protected function unauthenticated($request, AuthenticationException $exception)
|
|
|
|
{
|
2018-02-23 05:15:00 +08:00
|
|
|
if ($request->expectsJson() || $request->is('api/*')) {
|
|
|
|
$error = $this->createError(401, 'Unauthenticated');
|
|
|
|
return response()->json($error, 401);
|
2017-06-09 02:28:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->guest('login');
|
|
|
|
}
|
2017-08-23 22:23:48 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Render the given HttpException.
|
2018-02-23 05:15:00 +08:00
|
|
|
* @param HttpException $e
|
|
|
|
* @return \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response
|
2017-08-23 22:23:48 +08:00
|
|
|
*/
|
2018-02-23 05:15:00 +08:00
|
|
|
protected function renderHttpException(HttpException $e)
|
2017-08-23 22:23:48 +08:00
|
|
|
{
|
|
|
|
$status = $e->getStatusCode();
|
|
|
|
view()->replaceNamespace('errors', [
|
|
|
|
resource_path('views/layouts/' . config('phpvms.skin') . '/errors'),
|
|
|
|
resource_path('views/errors'),
|
|
|
|
__DIR__ . '/views',
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (view()->exists("errors::{$status}")) {
|
2017-12-24 01:17:29 +08:00
|
|
|
#if (view()->exists('layouts' . config('phpvms.skin') .'.errors.' .$status)) {
|
2018-02-11 07:58:11 +08:00
|
|
|
return response()->view("errors::{$status}", [
|
|
|
|
'exception' => $e,
|
|
|
|
'SKIN_NAME' => config('phpvms.skin'),
|
|
|
|
], $status, $e->getHeaders());
|
2017-08-23 22:23:48 +08:00
|
|
|
} else {
|
|
|
|
return $this->convertExceptionToResponse($e);
|
|
|
|
}
|
|
|
|
}
|
2017-06-09 02:28:26 +08:00
|
|
|
}
|