Skip to content

Commit 0f9cddc

Browse files
GuiteNyholm
authored andcommitted
catch exceptions when storing translations (#391)
1 parent dd41bc9 commit 0f9cddc

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

Controller/WebUIController.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function __construct(
7676
public function indexAction(?string $configName = null): Response
7777
{
7878
if (!$this->isWebUIEnabled) {
79-
return new Response('You are not allowed here. Check you config. ', 400);
79+
return new Response('You are not allowed here. Check your config.', Response::HTTP_BAD_REQUEST);
8080
}
8181

8282
$config = $this->configurationManager->getConfiguration($configName);
@@ -124,7 +124,7 @@ public function indexAction(?string $configName = null): Response
124124
public function showAction(string $configName, string $locale, string $domain): Response
125125
{
126126
if (!$this->isWebUIEnabled) {
127-
return new Response('You are not allowed here. Check you config. ', 400);
127+
return new Response('You are not allowed here. Check your config.', Response::HTTP_BAD_REQUEST);
128128
}
129129
$config = $this->configurationManager->getConfiguration($configName);
130130

@@ -154,7 +154,7 @@ public function showAction(string $configName, string $locale, string $domain):
154154
public function createAction(Request $request, string $configName, string $locale, string $domain): Response
155155
{
156156
if (!$this->isWebUIEnabled || !$this->isWebUIAllowCreate) {
157-
return new Response('You are not allowed to create. Check you config. ', 400);
157+
return new Response('You are not allowed to create. Check your config.', Response::HTTP_BAD_REQUEST);
158158
}
159159

160160
/** @var StorageService $storage */
@@ -166,13 +166,15 @@ public function createAction(Request $request, string $configName, string $local
166166
$message = $message->withLocale($locale);
167167
$this->validateMessage($message, ['Create']);
168168
} catch (MessageValidationException $e) {
169-
return new Response($e->getMessage(), 400);
169+
return new Response($e->getMessage(), Response::HTTP_BAD_REQUEST);
170170
}
171171

172172
try {
173173
$storage->create($message);
174174
} catch (StorageException $e) {
175175
throw new BadRequestHttpException(\sprintf('Key "%s" does already exist for "%s" on domain "%s".', $message->getKey(), $locale, $domain), $e);
176+
} catch (\Exception $e) {
177+
return new Response($e->getMessage(), Response::HTTP_BAD_REQUEST);
176178
}
177179

178180
return $this->render('@Translation/WebUI/create.html.twig', [
@@ -183,7 +185,7 @@ public function createAction(Request $request, string $configName, string $local
183185
public function editAction(Request $request, string $configName, string $locale, string $domain): Response
184186
{
185187
if (!$this->isWebUIEnabled) {
186-
return new Response('You are not allowed here. Check you config. ', 400);
188+
return new Response('You are not allowed here. Check your config.', Response::HTTP_BAD_REQUEST);
187189
}
188190

189191
try {
@@ -192,20 +194,24 @@ public function editAction(Request $request, string $configName, string $locale,
192194
$message = $message->withLocale($locale);
193195
$this->validateMessage($message, ['Edit']);
194196
} catch (MessageValidationException $e) {
195-
return new Response($e->getMessage(), 400);
197+
return new Response($e->getMessage(), Response::HTTP_BAD_REQUEST);
196198
}
197199

198200
/** @var StorageService $storage */
199201
$storage = $this->storageManager->getStorage($configName);
200-
$storage->update($message);
202+
try {
203+
$storage->update($message);
204+
} catch (\Exception $e) {
205+
return new Response($e->getMessage(), Response::HTTP_BAD_REQUEST);
206+
}
201207

202208
return new Response('Translation updated');
203209
}
204210

205211
public function deleteAction(Request $request, string $configName, string $locale, string $domain): Response
206212
{
207213
if (!$this->isWebUIEnabled || !$this->isWebUIAllowDelete) {
208-
return new Response('You are not allowed to create. Check you config. ', 400);
214+
return new Response('You are not allowed to create. Check your config.', Response::HTTP_BAD_REQUEST);
209215
}
210216

211217
try {
@@ -214,12 +220,16 @@ public function deleteAction(Request $request, string $configName, string $local
214220
$message = $message->withDomain($domain);
215221
$this->validateMessage($message, ['Delete']);
216222
} catch (MessageValidationException $e) {
217-
return new Response($e->getMessage(), 400);
223+
return new Response($e->getMessage(), Response::HTTP_BAD_REQUEST);
218224
}
219225

220226
/** @var StorageService $storage */
221227
$storage = $this->storageManager->getStorage($configName);
222-
$storage->delete($locale, $domain, $message->getKey());
228+
try {
229+
$storage->delete($locale, $domain, $message->getKey());
230+
} catch (\Exception $e) {
231+
return new Response($e->getMessage(), Response::HTTP_BAD_REQUEST);
232+
}
223233

224234
return new Response('Message was deleted');
225235
}

0 commit comments

Comments
 (0)