mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Code cleanup.
This commit is contained in:
@@ -24,12 +24,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class BadHttpHeaderException extends Exception
|
||||
class BadHttpHeaderException extends \Exception
|
||||
{
|
||||
public int $statusCode = 406;
|
||||
}
|
||||
|
@@ -24,11 +24,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Class DuplicateTransactionException
|
||||
*/
|
||||
class DuplicateTransactionException extends Exception
|
||||
{
|
||||
}
|
||||
class DuplicateTransactionException extends \Exception {}
|
||||
|
@@ -24,13 +24,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Class FireflyException.
|
||||
*
|
||||
|
||||
*/
|
||||
class FireflyException extends Exception
|
||||
{
|
||||
}
|
||||
class FireflyException extends \Exception {}
|
||||
|
@@ -33,7 +33,6 @@ use FireflyIII\User;
|
||||
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
|
||||
use Illuminate\Http\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Class GracefulNotFoundHandler
|
||||
@@ -43,13 +42,11 @@ class GracefulNotFoundHandler 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
|
||||
{
|
||||
$route = $request->route();
|
||||
if (null === $route) {
|
||||
@@ -65,57 +62,69 @@ class GracefulNotFoundHandler extends ExceptionHandler
|
||||
app('log')->warning(sprintf('GracefulNotFoundHandler cannot handle route with name "%s"', $name));
|
||||
|
||||
return parent::render($request, $e);
|
||||
|
||||
case 'accounts.show':
|
||||
case 'accounts.edit':
|
||||
case 'accounts.show.all':
|
||||
return $this->handleAccount($request, $e);
|
||||
|
||||
case 'transactions.show':
|
||||
case 'transactions.edit':
|
||||
return $this->handleGroup($request, $e);
|
||||
|
||||
case 'attachments.show':
|
||||
case 'attachments.edit':
|
||||
case 'attachments.download':
|
||||
case 'attachments.view':
|
||||
// redirect to original attachment holder.
|
||||
return $this->handleAttachment($request, $e);
|
||||
|
||||
case 'bills.show':
|
||||
$request->session()->reflash();
|
||||
|
||||
return redirect(route('bills.index'));
|
||||
|
||||
case 'currencies.show':
|
||||
$request->session()->reflash();
|
||||
|
||||
return redirect(route('currencies.index'));
|
||||
|
||||
case 'budgets.show':
|
||||
case 'budgets.edit':
|
||||
case 'budgets.show.limit':
|
||||
$request->session()->reflash();
|
||||
|
||||
return redirect(route('budgets.index'));
|
||||
|
||||
case 'piggy-banks.show':
|
||||
$request->session()->reflash();
|
||||
|
||||
return redirect(route('piggy-banks.index'));
|
||||
|
||||
case 'recurring.show':
|
||||
case 'recurring.edit':
|
||||
$request->session()->reflash();
|
||||
|
||||
return redirect(route('recurring.index'));
|
||||
|
||||
case 'tags.show.all':
|
||||
case 'tags.show':
|
||||
case 'tags.edit':
|
||||
$request->session()->reflash();
|
||||
|
||||
return redirect(route('tags.index'));
|
||||
|
||||
case 'categories.show':
|
||||
case 'categories.show.all':
|
||||
$request->session()->reflash();
|
||||
|
||||
return redirect(route('categories.index'));
|
||||
|
||||
case 'rules.edit':
|
||||
$request->session()->reflash();
|
||||
|
||||
return redirect(route('rules.index'));
|
||||
|
||||
case 'transactions.mass.edit':
|
||||
case 'transactions.mass.delete':
|
||||
case 'transactions.bulk.edit':
|
||||
@@ -130,15 +139,12 @@ class GracefulNotFoundHandler extends ExceptionHandler
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param Throwable $exception
|
||||
*
|
||||
* @return Response
|
||||
* @throws Throwable
|
||||
* @throws \Throwable
|
||||
*/
|
||||
private function handleAccount(Request $request, Throwable $exception): Response
|
||||
private function handleAccount(Request $request, \Throwable $exception): Response
|
||||
{
|
||||
app('log')->debug('404 page is probably a deleted account. Redirect to overview of account types.');
|
||||
|
||||
/** @var User $user */
|
||||
$user = auth()->user();
|
||||
$route = $request->route();
|
||||
@@ -150,7 +156,8 @@ class GracefulNotFoundHandler extends ExceptionHandler
|
||||
if (!($param instanceof Account) && !is_object($param)) {
|
||||
$accountId = (int)$param;
|
||||
}
|
||||
/** @var Account|null $account */
|
||||
|
||||
/** @var null|Account $account */
|
||||
$account = $user->accounts()->with(['accountType'])->withTrashed()->find($accountId);
|
||||
if (null === $account) {
|
||||
app('log')->error(sprintf('Could not find account %d, so give big fat error.', $accountId));
|
||||
@@ -165,29 +172,29 @@ class GracefulNotFoundHandler extends ExceptionHandler
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param Throwable $exception
|
||||
*
|
||||
* @return Response
|
||||
* @throws Throwable
|
||||
*
|
||||
* @throws \Throwable
|
||||
*/
|
||||
private function handleGroup(Request $request, Throwable $exception)
|
||||
private function handleGroup(Request $request, \Throwable $exception)
|
||||
{
|
||||
app('log')->debug('404 page is probably a deleted group. Redirect to overview of group types.');
|
||||
|
||||
/** @var User $user */
|
||||
$user = auth()->user();
|
||||
$route = $request->route();
|
||||
$param = $route->parameter('transactionGroup');
|
||||
$groupId = !is_object($param) ? (int)$param : 0;
|
||||
|
||||
/** @var TransactionGroup|null $group */
|
||||
/** @var null|TransactionGroup $group */
|
||||
$group = $user->transactionGroups()->withTrashed()->find($groupId);
|
||||
if (null === $group) {
|
||||
app('log')->error(sprintf('Could not find group %d, so give big fat error.', $groupId));
|
||||
|
||||
return parent::render($request, $exception);
|
||||
}
|
||||
/** @var TransactionJournal|null $journal */
|
||||
|
||||
/** @var null|TransactionJournal $journal */
|
||||
$journal = $group->transactionJournals()->withTrashed()->first();
|
||||
if (null === $journal) {
|
||||
app('log')->error(sprintf('Could not find journal for group %d, so give big fat error.', $groupId));
|
||||
@@ -205,21 +212,21 @@ class GracefulNotFoundHandler extends ExceptionHandler
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param Throwable $exception
|
||||
*
|
||||
* @return Response
|
||||
* @throws Throwable
|
||||
*
|
||||
* @throws \Throwable
|
||||
*/
|
||||
private function handleAttachment(Request $request, Throwable $exception)
|
||||
private function handleAttachment(Request $request, \Throwable $exception)
|
||||
{
|
||||
app('log')->debug('404 page is probably a deleted attachment. Redirect to parent object.');
|
||||
|
||||
/** @var User $user */
|
||||
$user = auth()->user();
|
||||
$route = $request->route();
|
||||
$param = $route->parameter('attachment');
|
||||
$attachmentId = is_object($param) ? 0 : (int)$param;
|
||||
/** @var Attachment|null $attachment */
|
||||
|
||||
/** @var null|Attachment $attachment */
|
||||
$attachment = $user->attachments()->withTrashed()->find($attachmentId);
|
||||
if (null === $attachment) {
|
||||
app('log')->error(sprintf('Could not find attachment %d, so give big fat error.', $attachmentId));
|
||||
@@ -229,7 +236,7 @@ class GracefulNotFoundHandler extends ExceptionHandler
|
||||
// get bindable.
|
||||
if (TransactionJournal::class === $attachment->attachable_type) {
|
||||
// is linked to journal, get group of journal (if not also deleted)
|
||||
/** @var TransactionJournal|null $journal */
|
||||
/** @var null|TransactionJournal $journal */
|
||||
$journal = $user->transactionJournals()->withTrashed()->find($attachment->attachable_id);
|
||||
if (null !== $journal) {
|
||||
return redirect(route('transactions.show', [$journal->transaction_group_id]));
|
||||
@@ -237,7 +244,7 @@ class GracefulNotFoundHandler extends ExceptionHandler
|
||||
}
|
||||
if (Bill::class === $attachment->attachable_type) {
|
||||
// is linked to bill.
|
||||
/** @var Bill|null $bill */
|
||||
/** @var null|Bill $bill */
|
||||
$bill = $user->bills()->withTrashed()->find($attachment->attachable_id);
|
||||
if (null !== $bill) {
|
||||
return redirect(route('bills.show', [$bill->id]));
|
||||
|
@@ -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
|
||||
{
|
||||
|
@@ -1,6 +1,5 @@
|
||||
<?php
|
||||
|
||||
|
||||
/*
|
||||
* IntervalException.php
|
||||
* Copyright (c) 2023 james@firefly-iii.org
|
||||
@@ -25,42 +24,32 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Exceptions;
|
||||
|
||||
use Exception;
|
||||
use FireflyIII\Support\Calendar\Periodicity;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Class IntervalException
|
||||
*/
|
||||
final class IntervalException extends Exception
|
||||
final class IntervalException extends \Exception
|
||||
{
|
||||
public array $availableIntervals;
|
||||
public Periodicity $periodicity;
|
||||
|
||||
/** @var mixed */
|
||||
protected $message = 'The periodicity %s is unknown. Choose one of available periodicity: %s';
|
||||
|
||||
public function __construct(string $message = '', int $code = 0, ?Throwable $previous = null)
|
||||
public function __construct(string $message = '', int $code = 0, ?\Throwable $previous = null)
|
||||
{
|
||||
parent::__construct($message, $code, $previous);
|
||||
$this->availableIntervals = [];
|
||||
$this->periodicity = Periodicity::Monthly;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Periodicity $periodicity
|
||||
* @param array $intervals
|
||||
* @param int $code
|
||||
* @param Throwable|null $previous
|
||||
*
|
||||
* @return IntervalException
|
||||
*/
|
||||
public static function unavailable(
|
||||
Periodicity $periodicity,
|
||||
array $intervals,
|
||||
int $code = 0,
|
||||
?Throwable $previous = null
|
||||
): self
|
||||
{
|
||||
?\Throwable $previous = null
|
||||
): self {
|
||||
$message = sprintf(
|
||||
'The periodicity %s is unknown. Choose one of available periodicity: %s',
|
||||
$periodicity->name,
|
||||
@@ -70,6 +59,7 @@ final class IntervalException extends Exception
|
||||
$exception = new self($message, $code, $previous);
|
||||
$exception->periodicity = $periodicity;
|
||||
$exception->availableIntervals = $intervals;
|
||||
|
||||
return $exception;
|
||||
}
|
||||
}
|
||||
|
@@ -24,13 +24,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Class NotImplementedException.
|
||||
*
|
||||
|
||||
*/
|
||||
class NotImplementedException extends Exception
|
||||
{
|
||||
}
|
||||
class NotImplementedException extends \Exception {}
|
||||
|
@@ -24,13 +24,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Class ValidationExceptions.
|
||||
*
|
||||
|
||||
*/
|
||||
class ValidationException extends Exception
|
||||
{
|
||||
}
|
||||
class ValidationException extends \Exception {}
|
||||
|
Reference in New Issue
Block a user