mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-05 04:03:26 +00:00
Code cleanup.
This commit is contained in:
@@ -24,7 +24,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Exceptions;
|
||||
|
||||
use ErrorException;
|
||||
use FireflyIII\Jobs\MailError;
|
||||
use Illuminate\Auth\AuthenticationException;
|
||||
use Illuminate\Database\QueryException;
|
||||
@@ -43,17 +42,14 @@ use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Class Handler
|
||||
*
|
||||
|
||||
*/
|
||||
class Handler extends ExceptionHandler
|
||||
{
|
||||
/**
|
||||
* @var array<int, class-string<Throwable>>
|
||||
* @var array<int, class-string<\Throwable>>
|
||||
*/
|
||||
protected $dontReport
|
||||
= [
|
||||
@@ -71,13 +67,11 @@ class Handler extends ExceptionHandler
|
||||
/**
|
||||
* Render an exception into an HTTP response.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Throwable $e
|
||||
* @param Request $request
|
||||
*
|
||||
* @return Response
|
||||
* @throws Throwable
|
||||
* @throws \Throwable
|
||||
*/
|
||||
public function render($request, Throwable $e): Response
|
||||
public function render($request, \Throwable $e): Response
|
||||
{
|
||||
$expectsJson = $request->expectsJson();
|
||||
// if the user requests anything /api/, assume the user wants to see JSON.
|
||||
@@ -90,33 +84,39 @@ class Handler extends ExceptionHandler
|
||||
if ($e instanceof LaravelValidationException && $expectsJson) {
|
||||
// ignore it: controller will handle it.
|
||||
app('log')->debug(sprintf('Return to parent to handle LaravelValidationException(%d)', $e->status));
|
||||
|
||||
return parent::render($request, $e);
|
||||
}
|
||||
if ($e instanceof NotFoundHttpException && $expectsJson) {
|
||||
// JSON error:
|
||||
app('log')->debug('Return JSON not found error.');
|
||||
|
||||
return response()->json(['message' => 'Resource not found', 'exception' => 'NotFoundHttpException'], 404);
|
||||
}
|
||||
|
||||
if ($e instanceof AuthenticationException && $expectsJson) {
|
||||
// somehow Laravel handler does not catch this:
|
||||
app('log')->debug('Return JSON unauthenticated error.');
|
||||
|
||||
return response()->json(['message' => 'Unauthenticated', 'exception' => 'AuthenticationException'], 401);
|
||||
}
|
||||
|
||||
if ($e instanceof OAuthServerException && $expectsJson) {
|
||||
app('log')->debug('Return JSON OAuthServerException.');
|
||||
|
||||
// somehow Laravel handler does not catch this:
|
||||
return response()->json(['message' => $e->getMessage(), 'exception' => 'OAuthServerException'], 401);
|
||||
}
|
||||
if ($e instanceof BadRequestHttpException) {
|
||||
app('log')->debug('Return JSON BadRequestHttpException.');
|
||||
|
||||
return response()->json(['message' => $e->getMessage(), 'exception' => 'BadRequestHttpException'], 400);
|
||||
}
|
||||
|
||||
if ($e instanceof BadHttpHeaderException) {
|
||||
// is always API exception.
|
||||
app('log')->debug('Return JSON BadHttpHeaderException.');
|
||||
|
||||
return response()->json(['message' => $e->getMessage(), 'exception' => 'BadHttpHeaderException'], $e->statusCode);
|
||||
}
|
||||
|
||||
@@ -127,6 +127,7 @@ class Handler extends ExceptionHandler
|
||||
$isDebug = (bool)config('app.debug', false);
|
||||
if ($isDebug) {
|
||||
app('log')->debug(sprintf('Return JSON %s with debug.', get_class($e)));
|
||||
|
||||
return response()->json(
|
||||
[
|
||||
'message' => $e->getMessage(),
|
||||
@@ -139,6 +140,7 @@ class Handler extends ExceptionHandler
|
||||
);
|
||||
}
|
||||
app('log')->debug(sprintf('Return JSON %s.', get_class($e)));
|
||||
|
||||
return response()->json(
|
||||
['message' => sprintf('Internal Firefly III Exception: %s', $e->getMessage()), 'exception' => get_class($e)],
|
||||
$errorCode
|
||||
@@ -160,7 +162,7 @@ class Handler extends ExceptionHandler
|
||||
return response()->view('errors.DatabaseException', ['exception' => $e, 'debug' => $isDebug], 500);
|
||||
}
|
||||
|
||||
if ($e instanceof FireflyException || $e instanceof ErrorException || $e instanceof OAuthServerException) {
|
||||
if ($e instanceof FireflyException || $e instanceof \ErrorException || $e instanceof OAuthServerException) {
|
||||
app('log')->debug('Return Firefly III error view.');
|
||||
$isDebug = config('app.debug');
|
||||
|
||||
@@ -175,13 +177,9 @@ class Handler extends ExceptionHandler
|
||||
/**
|
||||
* Report or log an exception.
|
||||
*
|
||||
* @param Throwable $e
|
||||
*
|
||||
* @return void
|
||||
* @throws Throwable
|
||||
*
|
||||
* @throws \Throwable
|
||||
*/
|
||||
public function report(Throwable $e)
|
||||
public function report(\Throwable $e)
|
||||
{
|
||||
$doMailError = (bool)config('firefly.send_error_message');
|
||||
if ($this->shouldntReportLocal($e) || !$doMailError) {
|
||||
@@ -224,30 +222,12 @@ class Handler extends ExceptionHandler
|
||||
parent::report($e);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Throwable $e
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function shouldntReportLocal(Throwable $e): bool
|
||||
{
|
||||
return null !== Arr::first(
|
||||
$this->dontReport,
|
||||
static function ($type) use ($e) {
|
||||
return $e instanceof $type;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a validation exception into a response.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param LaravelValidationException $exception
|
||||
*
|
||||
* @return JsonResponse| RedirectResponse |\Illuminate\Http\Response
|
||||
* @param Request $request
|
||||
*/
|
||||
protected function invalid($request, LaravelValidationException $exception): JsonResponse | RedirectResponse | \Illuminate\Http\Response
|
||||
protected function invalid($request, LaravelValidationException $exception): \Illuminate\Http\Response|JsonResponse|RedirectResponse
|
||||
{
|
||||
// protect against open redirect when submitting invalid forms.
|
||||
$previous = app('steam')->getSafePreviousUrl();
|
||||
@@ -255,15 +235,22 @@ class Handler extends ExceptionHandler
|
||||
|
||||
return redirect($redirect ?? $previous)
|
||||
->withInput(Arr::except($request->input(), $this->dontFlash))
|
||||
->withErrors($exception->errors(), $request->input('_error_bag', $exception->errorBag));
|
||||
->withErrors($exception->errors(), $request->input('_error_bag', $exception->errorBag))
|
||||
;
|
||||
}
|
||||
|
||||
private function shouldntReportLocal(\Throwable $e): bool
|
||||
{
|
||||
return null !== Arr::first(
|
||||
$this->dontReport,
|
||||
static function ($type) use ($e) {
|
||||
return $e instanceof $type;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Only return the redirectTo property from the exception if it is a valid URL. Return NULL otherwise.
|
||||
*
|
||||
* @param LaravelValidationException $exception
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
private function getRedirectUrl(LaravelValidationException $exception): ?string
|
||||
{
|
||||
|
Reference in New Issue
Block a user