diff --git a/app/Api/V1/Controllers/AttachmentController.php b/app/Api/V1/Controllers/AttachmentController.php index e5a3a93d84..83c3d74321 100644 --- a/app/Api/V1/Controllers/AttachmentController.php +++ b/app/Api/V1/Controllers/AttachmentController.php @@ -98,15 +98,15 @@ class AttachmentController extends Controller public function download(Attachment $attachment): LaravelResponse { if (false === $attachment->uploaded) { - throw new FireflyException(trans('api.error_no_upload')); + throw new FireflyException('200000: File has not been uploaded (yet).'); } if (0 === $attachment->size) { - throw new FireflyException(trans('api.error_no_upload')); + throw new FireflyException('200000: File has not been uploaded (yet).'); } if ($this->repository->exists($attachment)) { $content = $this->repository->getContent($attachment); if ('' === $content) { - throw new FireflyException(trans('api.error_no_upload')); + throw new FireflyException('200002: File is empty (zero bytes).'); } $quoted = sprintf('"%s"', addcslashes(basename($attachment->filename), '"\\')); @@ -125,7 +125,7 @@ class AttachmentController extends Controller return $response; } - throw new FireflyException(trans('api.error_file_lost')); + throw new FireflyException('200003: File does not exist.'); } /** diff --git a/app/Api/V1/Controllers/BillController.php b/app/Api/V1/Controllers/BillController.php index 9e9424a1bb..290864d023 100644 --- a/app/Api/V1/Controllers/BillController.php +++ b/app/Api/V1/Controllers/BillController.php @@ -211,20 +211,16 @@ class BillController extends Controller */ public function store(BillRequest $request): JsonResponse { - $bill = $this->repository->store($request->getAll()); - if (null !== $bill) { - $manager = $this->getManager(); + $bill = $this->repository->store($request->getAll()); + $manager = $this->getManager(); - /** @var BillTransformer $transformer */ - $transformer = app(BillTransformer::class); - $transformer->setParameters($this->parameters); + /** @var BillTransformer $transformer */ + $transformer = app(BillTransformer::class); + $transformer->setParameters($this->parameters); - $resource = new Item($bill, $transformer, 'bills'); - - return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); - } - throw new FireflyException(trans('api.error_store_bill')); // @codeCoverageIgnore + $resource = new Item($bill, $transformer, 'bills'); + return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); } /** diff --git a/app/Api/V1/Controllers/BudgetController.php b/app/Api/V1/Controllers/BudgetController.php index 5ea5793cf7..5e07482b5c 100644 --- a/app/Api/V1/Controllers/BudgetController.php +++ b/app/Api/V1/Controllers/BudgetController.php @@ -187,19 +187,16 @@ class BudgetController extends Controller */ public function store(BudgetRequest $request): JsonResponse { - $budget = $this->repository->store($request->getAll()); - if (null !== $budget) { - $manager = $this->getManager(); + $budget = $this->repository->store($request->getAll()); + $manager = $this->getManager(); - /** @var BudgetTransformer $transformer */ - $transformer = app(BudgetTransformer::class); - $transformer->setParameters($this->parameters); + /** @var BudgetTransformer $transformer */ + $transformer = app(BudgetTransformer::class); + $transformer->setParameters($this->parameters); - $resource = new Item($budget, $transformer, 'budgets'); + $resource = new Item($budget, $transformer, 'budgets'); - return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); - } - throw new FireflyException(trans('api.error_store_budget')); // @codeCoverageIgnore + return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); } /** diff --git a/app/Api/V1/Controllers/BudgetLimitController.php b/app/Api/V1/Controllers/BudgetLimitController.php index d1ac952326..2ff32cfa18 100644 --- a/app/Api/V1/Controllers/BudgetLimitController.php +++ b/app/Api/V1/Controllers/BudgetLimitController.php @@ -165,7 +165,7 @@ class BudgetLimitController extends Controller $data = $request->getAll(); $budget = $this->repository->findNull($data['budget_id']); if (null === $budget) { - throw new FireflyException(trans('api.error_unknown_budget')); + throw new FireflyException('200004: Budget does not exist.'); } $data['budget'] = $budget; $budgetLimit = $this->blRepository->storeBudgetLimit($data); diff --git a/app/Api/V1/Controllers/CategoryController.php b/app/Api/V1/Controllers/CategoryController.php index c2ff07d87b..1021fbbbca 100644 --- a/app/Api/V1/Controllers/CategoryController.php +++ b/app/Api/V1/Controllers/CategoryController.php @@ -152,18 +152,15 @@ class CategoryController extends Controller public function store(CategoryRequest $request): JsonResponse { $category = $this->repository->store($request->getAll()); - if (null !== $category) { - $manager = $this->getManager(); + $manager = $this->getManager(); - /** @var CategoryTransformer $transformer */ - $transformer = app(CategoryTransformer::class); - $transformer->setParameters($this->parameters); + /** @var CategoryTransformer $transformer */ + $transformer = app(CategoryTransformer::class); + $transformer->setParameters($this->parameters); - $resource = new Item($category, $transformer, 'categories'); + $resource = new Item($category, $transformer, 'categories'); - return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); - } - throw new FireflyException(trans('api.error_store_new_category')); // @codeCoverageIgnore + return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); } /** diff --git a/app/Api/V1/Controllers/ConfigurationController.php b/app/Api/V1/Controllers/ConfigurationController.php index 95a969b7c3..d6d426f269 100644 --- a/app/Api/V1/Controllers/ConfigurationController.php +++ b/app/Api/V1/Controllers/ConfigurationController.php @@ -57,7 +57,7 @@ class ConfigurationController extends Controller $admin = auth()->user(); if (!$this->repository->hasRole($admin, 'owner')) { - throw new FireflyException(trans('api.error_no_access')); // @codeCoverageIgnore + throw new FireflyException('200005: You need the "owner" role to do this.'); // @codeCoverageIgnore } return $next($request); diff --git a/app/Api/V1/Controllers/CurrencyController.php b/app/Api/V1/Controllers/CurrencyController.php index 550820bcf6..3f1114361a 100644 --- a/app/Api/V1/Controllers/CurrencyController.php +++ b/app/Api/V1/Controllers/CurrencyController.php @@ -313,10 +313,10 @@ class CurrencyController extends Controller if (!$this->userRepository->hasRole($admin, 'owner')) { // access denied: - throw new FireflyException(trans('api.error_no_access_ownership')); // @codeCoverageIgnore + throw new FireflyException('200005: You need the "owner" role to do this.'); // @codeCoverageIgnore } if ($this->repository->currencyInUse($currency)) { - throw new FireflyException(trans('api.error_no_access_currency_in_use')); // @codeCoverageIgnore + throw new FireflyException('200006: Currency in use.'); // @codeCoverageIgnore } $this->repository->destroy($currency); @@ -574,26 +574,21 @@ class CurrencyController extends Controller public function store(CurrencyRequest $request): JsonResponse { $currency = $this->repository->store($request->getAll()); - - if (null !== $currency) { - if (true === $request->boolean('default')) { - app('preferences')->set('currencyPreference', $currency->code); - app('preferences')->mark(); - } - $manager = $this->getManager(); - $defaultCurrency = app('amount')->getDefaultCurrencyByUser(auth()->user()); - $this->parameters->set('defaultCurrency', $defaultCurrency); - - /** @var CurrencyTransformer $transformer */ - $transformer = app(CurrencyTransformer::class); - $transformer->setParameters($this->parameters); - - $resource = new Item($currency, $transformer, 'currencies'); - - return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); + if (true === $request->boolean('default')) { + app('preferences')->set('currencyPreference', $currency->code); + app('preferences')->mark(); } - throw new FireflyException(trans('api.error_store_new_currency')); // @codeCoverageIgnore + $manager = $this->getManager(); + $defaultCurrency = app('amount')->getDefaultCurrencyByUser(auth()->user()); + $this->parameters->set('defaultCurrency', $defaultCurrency); + /** @var CurrencyTransformer $transformer */ + $transformer = app(CurrencyTransformer::class); + $transformer->setParameters($this->parameters); + + $resource = new Item($currency, $transformer, 'currencies'); + + return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); } /** diff --git a/app/Factory/BillFactory.php b/app/Factory/BillFactory.php index f958dfa7d4..3977712c6e 100644 --- a/app/Factory/BillFactory.php +++ b/app/Factory/BillFactory.php @@ -24,10 +24,12 @@ declare(strict_types=1); namespace FireflyIII\Factory; +use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\Bill; use FireflyIII\Models\TransactionCurrency; use FireflyIII\Services\Internal\Support\BillServiceTrait; use FireflyIII\User; +use Illuminate\Database\QueryException; use Log; /** @@ -55,6 +57,7 @@ class BillFactory * @param array $data * * @return Bill|null + * @throws FireflyException */ public function create(array $data): ?Bill { @@ -64,28 +67,31 @@ class BillFactory $currency = $factory->find((int)($data['currency_id'] ?? null), (string)($data['currency_code'] ?? null)); if (null === $currency) { - // use default currency: $currency = app('amount')->getDefaultCurrencyByUser($this->user); } + try { + /** @var Bill $bill */ + $bill = Bill::create( + [ + 'name' => $data['name'], + 'match' => 'MIGRATED_TO_RULES', + 'amount_min' => $data['amount_min'], + 'user_id' => $this->user->id, + 'transaction_currency_id' => $currency->id, + 'amount_max' => $data['amount_max'], + 'date' => $data['date'], + 'repeat_freq' => $data['repeat_freq'], + 'skip' => $data['skip'], + 'automatch' => true, + 'active' => $data['active'] ?? true, + ] + ); + } catch(QueryException $e) { + Log::error($e->getMessage()); + Log::error($e->getTraceAsString()); + throw new FireflyException('400000: Could not store bill.'); + } - /** @var Bill $bill */ - $bill = Bill::create( - [ - 'name' => $data['name'], - 'match' => 'MIGRATED_TO_RULES', - 'amount_min' => $data['amount_min'], - 'user_id' => $this->user->id, - 'transaction_currency_id' => $currency->id, - 'amount_max' => $data['amount_max'], - 'date' => $data['date'], - 'repeat_freq' => $data['repeat_freq'], - 'skip' => $data['skip'], - 'automatch' => true, - 'active' => $data['active'] ?? true, - ] - ); - - // update note: if (isset($data['notes'])) { $this->updateNote($bill, $data['notes']); } diff --git a/app/Factory/CategoryFactory.php b/app/Factory/CategoryFactory.php index b8690a04b6..da7396c8c4 100644 --- a/app/Factory/CategoryFactory.php +++ b/app/Factory/CategoryFactory.php @@ -24,8 +24,10 @@ declare(strict_types=1); namespace FireflyIII\Factory; +use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\Category; use FireflyIII\User; +use Illuminate\Database\QueryException; use Log; /** @@ -62,7 +64,7 @@ class CategoryFactory * @param null|string $categoryName * * @return Category|null - * + * @throws FireflyException */ public function findOrCreate(?int $categoryId, ?string $categoryName): ?Category { @@ -88,13 +90,17 @@ class CategoryFactory if (null !== $category) { return $category; } - - return Category::create( - [ - 'user_id' => $this->user->id, - 'name' => $categoryName, - ] - ); + try { + return Category::create( + [ + 'user_id' => $this->user->id, + 'name' => $categoryName, + ] + ); + } catch (QueryException $e) { + Log::error($e->getMessage()); + throw new FireflyException('400003: Could not store new category.'); + } } return null; diff --git a/app/Factory/TransactionCurrencyFactory.php b/app/Factory/TransactionCurrencyFactory.php index 52d1fa42aa..824046331c 100644 --- a/app/Factory/TransactionCurrencyFactory.php +++ b/app/Factory/TransactionCurrencyFactory.php @@ -27,6 +27,7 @@ declare(strict_types=1); namespace FireflyIII\Factory; +use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\TransactionCurrency; use Illuminate\Database\QueryException; use Log; @@ -51,9 +52,10 @@ class TransactionCurrencyFactory /** * @param array $data * - * @return TransactionCurrency|null + * @return TransactionCurrency + * @throws FireflyException */ - public function create(array $data): ?TransactionCurrency + public function create(array $data): TransactionCurrency { try { /** @var TransactionCurrency $currency */ @@ -69,6 +71,7 @@ class TransactionCurrencyFactory } catch (QueryException $e) { $result = null; Log::error(sprintf('Could not create new currency: %s', $e->getMessage())); + throw new FireflyException('400004: Could not store new currency.'); } return $result; diff --git a/app/Repositories/Bill/BillRepository.php b/app/Repositories/Bill/BillRepository.php index 2b19fb0413..8290c8479c 100644 --- a/app/Repositories/Bill/BillRepository.php +++ b/app/Repositories/Bill/BillRepository.php @@ -24,6 +24,7 @@ namespace FireflyIII\Repositories\Bill; use Carbon\Carbon; use DB; +use FireflyIII\Exceptions\FireflyException; use FireflyIII\Factory\BillFactory; use FireflyIII\Models\Bill; use FireflyIII\Models\Note; @@ -662,9 +663,10 @@ class BillRepository implements BillRepositoryInterface /** * @param array $data * - * @return Bill|null + * @return Bill + * @throws FireflyException */ - public function store(array $data): ?Bill + public function store(array $data): Bill { /** @var BillFactory $factory */ $factory = app(BillFactory::class); diff --git a/app/Repositories/Bill/BillRepositoryInterface.php b/app/Repositories/Bill/BillRepositoryInterface.php index 6df333e44a..886816c0c2 100644 --- a/app/Repositories/Bill/BillRepositoryInterface.php +++ b/app/Repositories/Bill/BillRepositoryInterface.php @@ -23,6 +23,7 @@ declare(strict_types=1); namespace FireflyIII\Repositories\Bill; use Carbon\Carbon; +use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\Bill; use FireflyIII\User; use Illuminate\Pagination\LengthAwarePaginator; @@ -266,9 +267,10 @@ interface BillRepositoryInterface /** * @param array $data * - * @return Bill|null + * @return Bill + * @throws FireflyException */ - public function store(array $data): ?Bill; + public function store(array $data): Bill; /** * @param Bill $bill diff --git a/app/Repositories/Budget/BudgetRepository.php b/app/Repositories/Budget/BudgetRepository.php index 453e437e88..b89c8b28f2 100644 --- a/app/Repositories/Budget/BudgetRepository.php +++ b/app/Repositories/Budget/BudgetRepository.php @@ -25,6 +25,7 @@ namespace FireflyIII\Repositories\Budget; use Carbon\Carbon; use DB; use Exception; +use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\Budget; use FireflyIII\Models\BudgetLimit; use FireflyIII\Models\RecurrenceTransactionMeta; @@ -32,6 +33,7 @@ use FireflyIII\Models\RuleAction; use FireflyIII\Models\RuleTrigger; use FireflyIII\Services\Internal\Destroy\BudgetDestroyService; use FireflyIII\User; +use Illuminate\Database\QueryException; use Illuminate\Support\Collection; use Log; @@ -269,16 +271,20 @@ class BudgetRepository implements BudgetRepositoryInterface * @param array $data * * @return Budget + * @throws FireflyException */ public function store(array $data): Budget { - $newBudget = new Budget( - [ - 'user_id' => $this->user->id, - 'name' => $data['name'], - ] - ); - $newBudget->save(); + try { + $newBudget = Budget::create( + [ + 'user_id' => $this->user->id, + 'name' => $data['name'], + ] + ); + } catch(QueryException $e) { + throw new FireflyException('400002: Could not store budget.'); + } return $newBudget; } diff --git a/app/Repositories/Budget/BudgetRepositoryInterface.php b/app/Repositories/Budget/BudgetRepositoryInterface.php index 90e5a4a6de..d92d8079f2 100644 --- a/app/Repositories/Budget/BudgetRepositoryInterface.php +++ b/app/Repositories/Budget/BudgetRepositoryInterface.php @@ -23,6 +23,7 @@ declare(strict_types=1); namespace FireflyIII\Repositories\Budget; use Carbon\Carbon; +use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\Budget; use FireflyIII\User; use Illuminate\Support\Collection; @@ -132,6 +133,7 @@ interface BudgetRepositoryInterface * @param array $data * * @return Budget + * @throws FireflyException */ public function store(array $data): Budget; diff --git a/app/Repositories/Category/CategoryRepository.php b/app/Repositories/Category/CategoryRepository.php index f1f20c0427..ccedd6475f 100644 --- a/app/Repositories/Category/CategoryRepository.php +++ b/app/Repositories/Category/CategoryRepository.php @@ -24,6 +24,7 @@ namespace FireflyIII\Repositories\Category; use Carbon\Carbon; use DB; +use FireflyIII\Exceptions\FireflyException; use FireflyIII\Factory\CategoryFactory; use FireflyIII\Models\Category; use FireflyIII\Models\RecurrenceTransactionMeta; @@ -239,6 +240,7 @@ class CategoryRepository implements CategoryRepositoryInterface * @param array $data * * @return Category + * @throws FireflyException */ public function store(array $data): Category { @@ -246,7 +248,13 @@ class CategoryRepository implements CategoryRepositoryInterface $factory = app(CategoryFactory::class); $factory->setUser($this->user); - return $factory->findOrCreate(null, $data['name']); + $category = $factory->findOrCreate(null, $data['name']); + + if (null === $category) { + throw new FireflyException(sprintf('400003: Could not store new category with name "%s"', $data['name'])); + } + return $category; + } /** diff --git a/app/Repositories/Category/CategoryRepositoryInterface.php b/app/Repositories/Category/CategoryRepositoryInterface.php index a72f520223..443fb227a5 100644 --- a/app/Repositories/Category/CategoryRepositoryInterface.php +++ b/app/Repositories/Category/CategoryRepositoryInterface.php @@ -23,6 +23,7 @@ declare(strict_types=1); namespace FireflyIII\Repositories\Category; use Carbon\Carbon; +use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\Category; use FireflyIII\User; use Illuminate\Support\Collection; @@ -117,7 +118,7 @@ interface CategoryRepositoryInterface /** * @param array $data - * + * @throws FireflyException * @return Category */ public function store(array $data): Category; diff --git a/app/Repositories/Currency/CurrencyRepository.php b/app/Repositories/Currency/CurrencyRepository.php index ddbc21e05e..1a763ccd67 100644 --- a/app/Repositories/Currency/CurrencyRepository.php +++ b/app/Repositories/Currency/CurrencyRepository.php @@ -23,6 +23,7 @@ declare(strict_types=1); namespace FireflyIII\Repositories\Currency; use Carbon\Carbon; +use FireflyIII\Exceptions\FireflyException; use FireflyIII\Factory\TransactionCurrencyFactory; use FireflyIII\Models\AccountMeta; use FireflyIII\Models\AvailableBudget; @@ -489,14 +490,20 @@ class CurrencyRepository implements CurrencyRepositoryInterface /** * @param array $data * - * @return TransactionCurrency|null + * @return TransactionCurrency + * @throws FireflyException */ - public function store(array $data): ?TransactionCurrency + public function store(array $data): TransactionCurrency { /** @var TransactionCurrencyFactory $factory */ $factory = app(TransactionCurrencyFactory::class); + $result = $factory->create($data); - return $factory->create($data); + if (null === $result) { + throw new FireflyException('400004: Could not store new currency.'); + } + + return $result; } /** diff --git a/app/Repositories/Currency/CurrencyRepositoryInterface.php b/app/Repositories/Currency/CurrencyRepositoryInterface.php index d5e850be84..1853e45106 100644 --- a/app/Repositories/Currency/CurrencyRepositoryInterface.php +++ b/app/Repositories/Currency/CurrencyRepositoryInterface.php @@ -234,9 +234,9 @@ interface CurrencyRepositoryInterface /** * @param array $data * - * @return TransactionCurrency|null + * @return TransactionCurrency */ - public function store(array $data): ?TransactionCurrency; + public function store(array $data): TransactionCurrency; /** * @param TransactionCurrency $currency