Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit df11ec7

Browse files
committed
[Translator] Crowdin provider throw Exception when response status is 50x
1 parent 0ad8230 commit df11ec7

File tree

2 files changed

+459
-9
lines changed

2 files changed

+459
-9
lines changed

src/Symfony/Component/Translation/Bridge/Crowdin/CrowdinProvider.php

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,12 @@ public function write(TranslatorBagInterface $translatorBag): void
9595
}
9696

9797
foreach ($responses as $response) {
98-
if (200 !== $response->getStatusCode()) {
98+
if (200 !== $statusCode = $response->getStatusCode()) {
9999
$this->logger->error(sprintf('Unable to upload translations to Crowdin: "%s".', $response->getContent(false)));
100+
101+
if (500 <= $statusCode) {
102+
throw new ProviderException('Unable to upload translations to Crowdin.', $response);
103+
}
100104
}
101105
}
102106
}
@@ -135,9 +139,13 @@ public function read(array $domains, array $locales): TranslatorBag
135139
continue;
136140
}
137141

138-
if (200 !== $response->getStatusCode()) {
142+
if (200 !== $statusCode = $response->getStatusCode()) {
139143
$this->logger->error(sprintf('Unable to export file: "%s".', $response->getContent(false)));
140144

145+
if (500 <= $statusCode) {
146+
throw new ProviderException('Unable to export file.', $response);
147+
}
148+
141149
continue;
142150
}
143151

@@ -146,9 +154,13 @@ public function read(array $domains, array $locales): TranslatorBag
146154
}
147155

148156
foreach ($downloads as [$response, $locale, $domain]) {
149-
if (200 !== $response->getStatusCode()) {
157+
if (200 !== $statusCode = $response->getStatusCode()) {
150158
$this->logger->error(sprintf('Unable to download file content: "%s".', $response->getContent(false)));
151159

160+
if (500 <= $statusCode) {
161+
throw new ProviderException('Unable to download file content.', $response);
162+
}
163+
152164
continue;
153165
}
154166

@@ -192,8 +204,12 @@ public function delete(TranslatorBagInterface $translatorBag): void
192204
continue;
193205
}
194206

195-
if (204 !== $response->getStatusCode()) {
207+
if (204 !== $statusCode = $response->getStatusCode()) {
196208
$this->logger->warning(sprintf('Unable to delete string: "%s".', $response->getContent(false)));
209+
210+
if (500 <= $statusCode) {
211+
throw new ProviderException('Unable to delete string.', $response);
212+
}
197213
}
198214
}
199215
}
@@ -238,9 +254,13 @@ private function addFile(string $domain, string $content): ?array
238254
],
239255
]);
240256

241-
if (201 !== $response->getStatusCode()) {
257+
if (201 !== $statusCode = $response->getStatusCode()) {
242258
$this->logger->error(sprintf('Unable to create a File in Crowdin for domain "%s": "%s".', $domain, $response->getContent(false)));
243259

260+
if (500 <= $statusCode) {
261+
throw new ProviderException(sprintf('Unable to create a File in Crowdin for domain "%s".', $domain), $response);
262+
}
263+
244264
return null;
245265
}
246266

@@ -261,9 +281,13 @@ private function updateFile(int $fileId, string $domain, string $content): ?arra
261281
],
262282
]);
263283

264-
if (200 !== $response->getStatusCode()) {
284+
if (200 !== $statusCode = $response->getStatusCode()) {
265285
$this->logger->error(sprintf('Unable to update file in Crowdin for file ID "%d" and domain "%s": "%s".', $fileId, $domain, $response->getContent(false)));
266286

287+
if (500 <= $statusCode) {
288+
throw new ProviderException(sprintf('Unable to update file in Crowdin for file ID "%d" and domain "%s".', $fileId, $domain), $response);
289+
}
290+
267291
return null;
268292
}
269293

@@ -324,9 +348,7 @@ private function listStrings(int $fileId, int $limit, int $offset): array
324348
]);
325349

326350
if (200 !== $response->getStatusCode()) {
327-
$this->logger->error(sprintf('Unable to list strings for file %d: "%s".', $fileId, $response->getContent()));
328-
329-
return [];
351+
throw new ProviderException(sprintf('Unable to list strings for file "%d".', $fileId), $response);
330352
}
331353

332354
return $response->toArray()['data'];

0 commit comments

Comments
 (0)