Error codes in Firefly III API

This commit is contained in:
James Cole
2019-11-02 08:19:50 +01:00
parent 2ea1852a94
commit d749d550ee
9 changed files with 31 additions and 25 deletions

View File

@@ -79,10 +79,10 @@ class CurrencyExchangeRateController extends Controller
$toCurrency = $this->repository->findByCodeNull($request->get('to') ?? 'USD'); $toCurrency = $this->repository->findByCodeNull($request->get('to') ?? 'USD');
if (null === $fromCurrency) { if (null === $fromCurrency) {
throw new FireflyException(trans('api.error_unknown_source_currency')); throw new FireflyException('200007: Unknown source currency');
} }
if (null === $toCurrency) { if (null === $toCurrency) {
throw new FireflyException(trans('api.error_unknown_destination_currency')); throw new FireflyException('200007: Unknown destination currency');
} }
/** @var Carbon $dateObj */ /** @var Carbon $dateObj */

View File

@@ -86,7 +86,7 @@ class LinkTypeController extends Controller
public function delete(LinkType $linkType): JsonResponse public function delete(LinkType $linkType): JsonResponse
{ {
if (false === $linkType->editable) { if (false === $linkType->editable) {
throw new FireflyException(trans('api.error_delete_link_type', [':id' => $linkType->id, ':name' => $linkType->name])); throw new FireflyException('200020: Link type cannot be changed.');
} }
$this->repository->destroy($linkType); $this->repository->destroy($linkType);
@@ -160,7 +160,7 @@ class LinkTypeController extends Controller
$admin = auth()->user(); $admin = auth()->user();
if (!$this->userRepository->hasRole($admin, 'owner')) { if (!$this->userRepository->hasRole($admin, 'owner')) {
throw new FireflyException(trans('api.error_owner_role_needed')); throw new FireflyException('200005: You need the "owner" role to do this.'); // @codeCoverageIgnore
} }
$data = $request->getAll(); $data = $request->getAll();
// if currency ID is 0, find the currency by the code: // if currency ID is 0, find the currency by the code:
@@ -247,14 +247,14 @@ class LinkTypeController extends Controller
public function update(LinkTypeRequest $request, LinkType $linkType): JsonResponse public function update(LinkTypeRequest $request, LinkType $linkType): JsonResponse
{ {
if (false === $linkType->editable) { if (false === $linkType->editable) {
throw new FireflyException(trans('api.error_edit_link_type', [':id' => $linkType->id, ':name' => $linkType->name])); throw new FireflyException('200020: Link type cannot be changed.');
} }
/** @var User $admin */ /** @var User $admin */
$admin = auth()->user(); $admin = auth()->user();
if (!$this->userRepository->hasRole($admin, 'owner')) { if (!$this->userRepository->hasRole($admin, 'owner')) {
throw new FireflyException(trans('api.error_owner_role_needed')); throw new FireflyException('200005: You need the "owner" role to do this.'); // @codeCoverageIgnore
} }
$data = $request->getAll(); $data = $request->getAll();

View File

@@ -181,19 +181,15 @@ class PiggyBankController extends Controller
public function store(PiggyBankRequest $request): JsonResponse public function store(PiggyBankRequest $request): JsonResponse
{ {
$piggyBank = $this->repository->store($request->getAll()); $piggyBank = $this->repository->store($request->getAll());
if (null !== $piggyBank) { $manager = $this->getManager();
$manager = $this->getManager();
/** @var PiggyBankTransformer $transformer */ /** @var PiggyBankTransformer $transformer */
$transformer = app(PiggyBankTransformer::class); $transformer = app(PiggyBankTransformer::class);
$transformer->setParameters($this->parameters); $transformer->setParameters($this->parameters);
$resource = new Item($piggyBank, $transformer, 'piggy_banks'); $resource = new Item($piggyBank, $transformer, 'piggy_banks');
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
}
throw new FireflyException(trans('api.error_store_new_piggybank'));
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
} }
/** /**

View File

@@ -236,7 +236,7 @@ class RecurrenceController extends Controller
$result = $recurring->fire(); $result = $recurring->fire();
} catch (FireflyException $e) { } catch (FireflyException $e) {
Log::error($e->getMessage()); Log::error($e->getMessage());
throw new FireflyException(trans('api.error_fire_cronjob')); throw new FireflyException('200022: Error in cron job.');
} }
if (false === $result) { if (false === $result) {
return response()->json([], 204); return response()->json([], 204);

View File

@@ -257,7 +257,7 @@ class RuleGroupController extends Controller
/** @var Collection $rules */ /** @var Collection $rules */
$rules = $this->ruleGroupRepository->getActiveRules($group); $rules = $this->ruleGroupRepository->getActiveRules($group);
if (0 === $rules->count()) { if (0 === $rules->count()) {
throw new FireflyException(trans('api.error_no_rules_in_rule_group')); throw new FireflyException('200023: No rules in this rule group.');
} }
$parameters = $request->getTestParameters(); $parameters = $request->getTestParameters();
$matchingTransactions = []; $matchingTransactions = [];

View File

@@ -165,7 +165,7 @@ class TransactionLinkController extends Controller
$inward = $this->journalRepository->findNull($data['inward_id'] ?? 0); $inward = $this->journalRepository->findNull($data['inward_id'] ?? 0);
$outward = $this->journalRepository->findNull($data['outward_id'] ?? 0); $outward = $this->journalRepository->findNull($data['outward_id'] ?? 0);
if (null === $inward || null === $outward) { if (null === $inward || null === $outward) {
throw new FireflyException(trans('api.error_source_or_dest_null')); throw new FireflyException('200024: Source or destination does not exist.');
} }
$data['direction'] = 'inward'; $data['direction'] = 'inward';
@@ -196,7 +196,7 @@ class TransactionLinkController extends Controller
$data['inward'] = $this->journalRepository->findNull($data['inward_id'] ?? 0); $data['inward'] = $this->journalRepository->findNull($data['inward_id'] ?? 0);
$data['outward'] = $this->journalRepository->findNull($data['outward_id'] ?? 0); $data['outward'] = $this->journalRepository->findNull($data['outward_id'] ?? 0);
if (null === $data['inward'] || null === $data['outward']) { if (null === $data['inward'] || null === $data['outward']) {
throw new FireflyException(trans('api.error_source_or_dest_null')); throw new FireflyException('200024: Source or destination does not exist.');
} }
$data['direction'] = 'inward'; $data['direction'] = 'inward';
$journalLink = $this->repository->updateLink($journalLink, $data); $journalLink = $this->repository->updateLink($journalLink, $data);

View File

@@ -83,7 +83,7 @@ class UserController extends Controller
return response()->json([], 204); return response()->json([], 204);
} }
throw new FireflyException(trans('api.error_no_access')); // @codeCoverageIgnore throw new FireflyException('200025: No access to function.'); // @codeCoverageIgnore
} }
/** /**

View File

@@ -24,6 +24,7 @@ namespace FireflyIII\Repositories\PiggyBank;
use Carbon\Carbon; use Carbon\Carbon;
use Exception; use Exception;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Note; use FireflyIII\Models\Note;
use FireflyIII\Models\PiggyBank; use FireflyIII\Models\PiggyBank;
use FireflyIII\Models\PiggyBankEvent; use FireflyIII\Models\PiggyBankEvent;
@@ -33,6 +34,7 @@ use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface; use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\User; use FireflyIII\User;
use Illuminate\Database\QueryException;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Log; use Log;
@@ -556,13 +558,19 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
/** /**
* @param array $data * @param array $data
* *
* @return PiggyBank|null * @return PiggyBank
* @throws FireflyException
*/ */
public function store(array $data): ?PiggyBank public function store(array $data): PiggyBank
{ {
$data['order'] = $this->getMaxOrder() + 1; $data['order'] = $this->getMaxOrder() + 1;
try {
/** @var PiggyBank $piggyBank */ /** @var PiggyBank $piggyBank */
$piggyBank = PiggyBank::create($data); $piggyBank = PiggyBank::create($data);
} catch(QueryException $e) {
Log::error(sprintf('Could not store piggy bank: %s',$e->getMessage()));
throw new FireflyException('400005: Could not store new piggy bank.');
}
$this->updateNote($piggyBank, $data['notes']); $this->updateNote($piggyBank, $data['notes']);

View File

@@ -23,6 +23,7 @@ declare(strict_types=1);
namespace FireflyIII\Repositories\PiggyBank; namespace FireflyIII\Repositories\PiggyBank;
use Carbon\Carbon; use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\PiggyBank; use FireflyIII\Models\PiggyBank;
use FireflyIII\Models\PiggyBankEvent; use FireflyIII\Models\PiggyBankEvent;
use FireflyIII\Models\PiggyBankRepetition; use FireflyIII\Models\PiggyBankRepetition;
@@ -245,9 +246,10 @@ interface PiggyBankRepositoryInterface
* *
* @param array $data * @param array $data
* *
* @return PiggyBank|null * @return PiggyBank
* @throws FireflyException
*/ */
public function store(array $data): ?PiggyBank; public function store(array $data): PiggyBank;
/** /**
* Update existing piggy bank. * Update existing piggy bank.