Skip to content

Commit

Permalink
Merge pull request #2772 from dguhl/2695-localise-api-errors
Browse files Browse the repository at this point in the history
Fix issue #2695
  • Loading branch information
JC5 committed Oct 26, 2019
2 parents cfd9828 + 13a29a6 commit 0d072b4
Show file tree
Hide file tree
Showing 15 changed files with 69 additions and 24 deletions.
8 changes: 4 additions & 4 deletions app/Api/V1/Controllers/AttachmentController.php
Expand Up @@ -98,15 +98,15 @@ public function delete(Attachment $attachment): JsonResponse
public function download(Attachment $attachment): LaravelResponse
{
if (false === $attachment->uploaded) {
throw new FireflyException('No file has been uploaded for this attachment (yet).');
throw new FireflyException(trans('api.error_no_upload'));
}
if (0 === $attachment->size) {
throw new FireflyException('No file has been uploaded for this attachment (yet).');
throw new FireflyException(trans('api.error_no_upload'));
}
if ($this->repository->exists($attachment)) {
$content = $this->repository->getContent($attachment);
if ('' === $content) {
throw new FireflyException('No file has been uploaded for this attachment (yet).');
throw new FireflyException(trans('api.error_no_upload'));
}
$quoted = sprintf('"%s"', addcslashes(basename($attachment->filename), '"\\'));

Expand All @@ -125,7 +125,7 @@ public function download(Attachment $attachment): LaravelResponse

return $response;
}
throw new FireflyException('Could not find the indicated attachment. The file is no longer there.');
throw new FireflyException(trans('api.error_file_lost'));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Api/V1/Controllers/BillController.php
Expand Up @@ -223,7 +223,7 @@ public function store(BillRequest $request): JsonResponse

return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
}
throw new FireflyException('Could not store new bill.'); // @codeCoverageIgnore
throw new FireflyException(trans('api.error_store_bill')); // @codeCoverageIgnore

}

Expand Down
2 changes: 1 addition & 1 deletion app/Api/V1/Controllers/BudgetController.php
Expand Up @@ -199,7 +199,7 @@ public function store(BudgetRequest $request): JsonResponse

return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
}
throw new FireflyException('Could not store new budget.'); // @codeCoverageIgnore
throw new FireflyException(trans('api.error_store_budget')); // @codeCoverageIgnore
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Api/V1/Controllers/BudgetLimitController.php
Expand Up @@ -165,7 +165,7 @@ public function store(BudgetLimitRequest $request): JsonResponse
$data = $request->getAll();
$budget = $this->repository->findNull($data['budget_id']);
if (null === $budget) {
throw new FireflyException('Unknown budget.');
throw new FireflyException(trans('api.error_unknown_budget'));
}
$data['budget'] = $budget;
$budgetLimit = $this->blRepository->storeBudgetLimit($data);
Expand Down
2 changes: 1 addition & 1 deletion app/Api/V1/Controllers/CategoryController.php
Expand Up @@ -163,7 +163,7 @@ public function store(CategoryRequest $request): JsonResponse

return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
}
throw new FireflyException('Could not store new category.'); // @codeCoverageIgnore
throw new FireflyException(trans('api.error_store_new_category')); // @codeCoverageIgnore
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Api/V1/Controllers/ConfigurationController.php
Expand Up @@ -57,7 +57,7 @@ function ($request, $next) {
$admin = auth()->user();

if (!$this->repository->hasRole($admin, 'owner')) {
throw new FireflyException('No access to method.'); // @codeCoverageIgnore
throw new FireflyException(trans('api.error_no_access')); // @codeCoverageIgnore
}

return $next($request);
Expand Down
6 changes: 3 additions & 3 deletions app/Api/V1/Controllers/CurrencyController.php
Expand Up @@ -313,10 +313,10 @@ public function delete(TransactionCurrency $currency): JsonResponse

if (!$this->userRepository->hasRole($admin, 'owner')) {
// access denied:
throw new FireflyException('No access to method, user is not owner.'); // @codeCoverageIgnore
throw new FireflyException(trans('api.error_no_access_ownership')); // @codeCoverageIgnore
}
if ($this->repository->currencyInUse($currency)) {
throw new FireflyException('No access to method, currency is in use.'); // @codeCoverageIgnore
throw new FireflyException(trans('api.error_no_access_currency_in_use')); // @codeCoverageIgnore
}
$this->repository->destroy($currency);

Expand Down Expand Up @@ -592,7 +592,7 @@ public function store(CurrencyRequest $request): JsonResponse

return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
}
throw new FireflyException('Could not store new currency.'); // @codeCoverageIgnore
throw new FireflyException(trans('api.error_store_new_currency')); // @codeCoverageIgnore

}

Expand Down
4 changes: 2 additions & 2 deletions app/Api/V1/Controllers/CurrencyExchangeRateController.php
Expand Up @@ -79,10 +79,10 @@ public function index(Request $request): JsonResponse
$toCurrency = $this->repository->findByCodeNull($request->get('to') ?? 'USD');

if (null === $fromCurrency) {
throw new FireflyException('Unknown source currency.');
throw new FireflyException(trans('api.error_unknown_source_currency'));
}
if (null === $toCurrency) {
throw new FireflyException('Unknown destination currency.');
throw new FireflyException(trans('api.error_unknown_destination_currency'));
}

/** @var Carbon $dateObj */
Expand Down
8 changes: 4 additions & 4 deletions app/Api/V1/Controllers/LinkTypeController.php
Expand Up @@ -86,7 +86,7 @@ function ($request, $next) {
public function delete(LinkType $linkType): JsonResponse
{
if (false === $linkType->editable) {
throw new FireflyException(sprintf('You cannot delete this link type (#%d, "%s")', $linkType->id, $linkType->name));
throw new FireflyException(trans('api.error_delete_link_type', [':id' => $linkType->id, ':name' => $linkType->name]));
}
$this->repository->destroy($linkType);

Expand Down Expand Up @@ -160,7 +160,7 @@ public function store(LinkTypeRequest $request): JsonResponse
$admin = auth()->user();

if (!$this->userRepository->hasRole($admin, 'owner')) {
throw new FireflyException('You need the "owner"-role to do this.');
throw new FireflyException(trans('api.error_owner_role_needed'));
}
$data = $request->getAll();
// if currency ID is 0, find the currency by the code:
Expand Down Expand Up @@ -247,14 +247,14 @@ public function transactions(Request $request, LinkType $linkType): JsonResponse
public function update(LinkTypeRequest $request, LinkType $linkType): JsonResponse
{
if (false === $linkType->editable) {
throw new FireflyException(sprintf('You cannot edit this link type (#%d, "%s")', $linkType->id, $linkType->name));
throw new FireflyException(trans('api.error_edit_link_type', [':id' => $linkType->id, ':name' => $linkType->name]));
}

/** @var User $admin */
$admin = auth()->user();

if (!$this->userRepository->hasRole($admin, 'owner')) {
throw new FireflyException('You need the "owner"-role to do this.');
throw new FireflyException(trans('api.error_owner_role_needed'));
}

$data = $request->getAll();
Expand Down
2 changes: 1 addition & 1 deletion app/Api/V1/Controllers/PiggyBankController.php
Expand Up @@ -192,7 +192,7 @@ public function store(PiggyBankRequest $request): JsonResponse

return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
}
throw new FireflyException('Could not store new piggy bank.');
throw new FireflyException(trans('api.error_store_new_piggybank'));

}

Expand Down
2 changes: 1 addition & 1 deletion app/Api/V1/Controllers/RecurrenceController.php
Expand Up @@ -236,7 +236,7 @@ public function trigger(): JsonResponse
$result = $recurring->fire();
} catch (FireflyException $e) {
Log::error($e->getMessage());
throw new FireflyException('Could not fire recurring cron job.');
throw new FireflyException(trans('api.error_fire_cronjob'));
}
if (false === $result) {
return response()->json([], 204);
Expand Down
2 changes: 1 addition & 1 deletion app/Api/V1/Controllers/RuleGroupController.php
Expand Up @@ -257,7 +257,7 @@ public function testGroup(RuleGroupTestRequest $request, RuleGroup $group): Json
/** @var Collection $rules */
$rules = $this->ruleGroupRepository->getActiveRules($group);
if (0 === $rules->count()) {
throw new FireflyException('No rules in this rule group.');
throw new FireflyException(trans('api.error_no_rules_in_rule_group'));
}
$parameters = $request->getTestParameters();
$matchingTransactions = [];
Expand Down
4 changes: 2 additions & 2 deletions app/Api/V1/Controllers/TransactionLinkController.php
Expand Up @@ -165,7 +165,7 @@ public function store(TransactionLinkRequest $request): JsonResponse
$inward = $this->journalRepository->findNull($data['inward_id'] ?? 0);
$outward = $this->journalRepository->findNull($data['outward_id'] ?? 0);
if (null === $inward || null === $outward) {
throw new FireflyException('Source or destination is NULL.');
throw new FireflyException(trans('api.error_source_or_dest_null'));
}
$data['direction'] = 'inward';

Expand Down Expand Up @@ -196,7 +196,7 @@ public function update(TransactionLinkRequest $request, TransactionJournalLink $
$data['inward'] = $this->journalRepository->findNull($data['inward_id'] ?? 0);
$data['outward'] = $this->journalRepository->findNull($data['outward_id'] ?? 0);
if (null === $data['inward'] || null === $data['outward']) {
throw new FireflyException('Source or destination is NULL.');
throw new FireflyException(trans('api.error_source_or_dest_null'));
}
$data['direction'] = 'inward';
$journalLink = $this->repository->updateLink($journalLink, $data);
Expand Down
2 changes: 1 addition & 1 deletion app/Api/V1/Controllers/UserController.php
Expand Up @@ -83,7 +83,7 @@ public function delete(User $user): JsonResponse

return response()->json([], 204);
}
throw new FireflyException('No access to method.'); // @codeCoverageIgnore
throw new FireflyException(trans('api.error_no_access')); // @codeCoverageIgnore
}

/**
Expand Down
45 changes: 45 additions & 0 deletions resources/lang/en_US/api.php
@@ -0,0 +1,45 @@
<?php
/**
* api.php
* Copyright (c) 2019 thegrumpydictator@gmail.com
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

declare(strict_types=1);

return [
'error_no_upload' => 'No file has been uploaded for this attachment (yet).',
'error_file_lost' => 'Could not find the indicated attachment. The file is no longer there.',
'error_store_bill' => 'Could not store new bill.',
'error_store_budget' => 'Could not store new budget.',
'error_unknown_budget' => 'Unknown budget.',
'error_store_new_category' => 'Could not store new category.',
'error_no_access' => 'No access to method.',
'error_no_access_ownership' => 'No access to method, user is not owner.',
'error_no_access_currency_in_use' => 'No access to method, currency is in use.',
'error_store_new_currency' => 'Could not store new currency.',
'error_unknown_source_currency' => 'Unknown source currency.',
'error_unknown_destination_currency' => 'Unknown destination currency.',
'error_delete_link_type' => 'You cannot delete this link type (:id, :name)',
'error_edit_link_type' => 'You cannot edit this link type (:id, :name)',
'error_owner_role_needed' => 'You need the "owner"-role to do this.',
'error_store_new_piggybank' => 'Could not store new piggy bank.',
'error_fire_cronjob' => 'Could not fire recurring cron job.',
'error_no_rules_in_rule_group' => 'No rules in this rule group.',
'error_source_or_dest_null' => 'Source or destination is NULL.'

];

0 comments on commit 0d072b4

Please sign in to comment.