Skip to content

Commit

Permalink
TASK: Localizes exception messages thrown by the package backend
Browse files Browse the repository at this point in the history
  • Loading branch information
markusguenther authored and Sebobo committed Nov 4, 2024
1 parent f100b66 commit a3bf910
Show file tree
Hide file tree
Showing 4 changed files with 519 additions and 8 deletions.
48 changes: 44 additions & 4 deletions Classes/GraphQL/Resolver/Type/MutationResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Flowpack\Media\Ui\GraphQL\Context\AssetSourceContext;
use Flowpack\Media\Ui\Service\AssetCollectionService;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\I18n\Translator;
use Neos\Flow\Persistence\Exception\IllegalObjectTypeException;
use Neos\Flow\Persistence\Exception\InvalidQueryException;
use Neos\Flow\Persistence\PersistenceManagerInterface;
Expand Down Expand Up @@ -113,6 +114,23 @@ class MutationResolver implements ResolverInterface
*/
protected $assetCollectionService;

/**
* @Flow\Inject
* @var Translator
*/
protected $translator;

protected function localizedMessage(string $id, string $fallback = '', array $arguments = []): string
{
return $this->translator->translateById($id, [], null, null, 'Main', 'Flowpack.Media.Ui') ?? $fallback;
}

protected function localizedMessageFromException(\Exception $exception): string
{
$labelIdentifier = 'errors.' . $exception->getCode() . '.message';
return $this->localizedMessage($labelIdentifier, $exception->getMessage());
}

/**
* @throws Exception
*/
Expand All @@ -125,23 +143,45 @@ public function deleteAsset($_, array $variables, AssetSourceContext $assetSourc

$assetProxy = $assetSourceContext->getAssetProxy($id, $assetSourceId);
if (!$assetProxy) {
return new MutationResult(false, ['Asset could not be resolved']);
return new MutationResult(
false,
[$this->localizedMessage(
'actions.deleteAssets.noProxy',
'Asset could not be resolved')
]
);
}
$asset = $assetSourceContext->getAssetForProxy($assetProxy);

if (!$asset) {
return new MutationResult(false, ['Cannot delete asset that was never imported']);
return new MutationResult(
false,
[
$this->localizedMessage(
'actions.deleteAssets.noImportExists',
'Cannot delete asset that was never imported'
)
]
);
}

try {
$this->assetRepository->remove($asset);
} catch (AssetServiceException $e) {
return new MutationResult(false, [$e->getMessage()]);
return new MutationResult(false, [$this->localizedMessageFromException($e)]);
} catch (\Exception $e) {
throw new Exception('Failed to delete asset', 1591537315);
}

return new MutationResult(true, ['Asset deleted']);
return new MutationResult(
true,
[
$this->localizedMessage(
'actions.deleteAssets.success',
'Asset deleted'
)
]
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@ const createErrorHandler = (notify: NeosNotification) => {
if (graphQLErrors) {
graphQLErrors.map((data) => {
const isInternalError = data.extensions.category === 'internal';
let errorTitleLabel = isInternalError ? 'errors.internal.title' : 'errors.graphql.title';
const defaultErrorTitle = isInternalError
? translate('errors.internal.title', 'Internal server error')
: translate('errors.graphql.title', 'Communication error');
let errorMessageLabel = '';
let errorTitleLabel = '';
if (data.extensions.errorCode) {
errorTitleLabel = `errors.${data.extensions.errorCode}.title`;
errorMessageLabel = `errors.${data.extensions.errorCode}.message`;
}

notify.error(
translate(errorTitleLabel),
translate(errorTitleLabel, defaultErrorTitle),
errorMessageLabel.length ? translate(errorMessageLabel) : data.message
);
});
Expand Down
Loading

0 comments on commit a3bf910

Please sign in to comment.