mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-18 18:44:16 +00:00
Improve error handling in API.
This commit is contained in:
@@ -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.');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -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');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -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');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -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);
|
||||
|
@@ -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');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -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);
|
||||
|
@@ -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');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -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']);
|
||||
}
|
||||
|
@@ -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;
|
||||
|
@@ -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;
|
||||
|
@@ -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);
|
||||
|
@@ -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
|
||||
|
@@ -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;
|
||||
}
|
||||
|
@@ -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;
|
||||
|
||||
|
@@ -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;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -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;
|
||||
|
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -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
|
||||
|
Reference in New Issue
Block a user