Skip to content

Commit 5d00302

Browse files
authored
fix: bundle download error message (#805)
1 parent 7ccb01b commit 5d00302

File tree

5 files changed

+29
-28
lines changed

5 files changed

+29
-28
lines changed

src/main/java/com/crowdin/cli/client/ClientBundle.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66

77
import java.net.URL;
88
import java.util.List;
9-
import java.util.Optional;
109

1110
public interface ClientBundle extends Client {
1211

1312
List<Bundle> listBundle();
1413

1514
Bundle addBundle(AddBundleRequest addBundleRequest);
1615

17-
Optional<Bundle> getBundle(Long id);
16+
Bundle getBundle(Long id);
1817

1918
URL downloadBundle(Long id, String exportId);
2019

src/main/java/com/crowdin/cli/client/CrowdinClientBundle.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package com.crowdin.cli.client;
22

3-
43
import com.crowdin.client.bundles.model.AddBundleRequest;
54
import com.crowdin.client.bundles.model.Bundle;
65
import com.crowdin.client.bundles.model.BundleExport;
6+
import lombok.SneakyThrows;
77

88
import java.net.URL;
99
import java.util.List;
10-
import java.util.Optional;
1110

1211
public class CrowdinClientBundle extends CrowdinClientCore implements ClientBundle {
1312

@@ -31,15 +30,12 @@ public Bundle addBundle(AddBundleRequest addBundleRequest) {
3130
.getData());
3231
}
3332

33+
@SneakyThrows
3434
@Override
35-
public Optional<Bundle> getBundle(Long bundleId) {
36-
try {
37-
return Optional.of(executeRequest(() -> this.client.getBundlesApi()
38-
.getBundle(Long.valueOf(projectId), bundleId))
39-
.getData());
40-
} catch (Exception e) {
41-
return Optional.empty();
42-
}
35+
public Bundle getBundle(Long bundleId) {
36+
return executeRequest(() -> this.client.getBundlesApi()
37+
.getBundle(Long.valueOf(projectId), bundleId))
38+
.getData();
4339
}
4440

4541
@Override

src/main/java/com/crowdin/cli/client/CrowdinClientCore.java

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.util.Map;
1818
import java.util.function.BiFunction;
1919
import java.util.function.BiPredicate;
20+
import java.util.function.Function;
2021
import java.util.function.Supplier;
2122
import java.util.stream.Collectors;
2223

@@ -26,29 +27,35 @@ abstract class CrowdinClientCore {
2627

2728
private static final long defaultMillisToRetry = 100;
2829

29-
private static final Map<BiPredicate<String, String>, RuntimeException> standardErrorHandlers =
30+
protected static final Map<BiPredicate<String, String>, Function<String, RuntimeException>> standardErrorHandlers =
3031
new LinkedHashMap<>() {{
3132
put((code, message) -> code.equals("401"),
32-
new ExitCodeExceptionMapper.AuthorizationException(RESOURCE_BUNDLE.getString("error.response.401")));
33+
(msg) -> new ExitCodeExceptionMapper.AuthorizationException(RESOURCE_BUNDLE.getString("error.response.401")));
3334
put((code, message) -> code.equals("403") && message.contains("upgrade your subscription plan to upload more file formats"),
34-
new ExitCodeExceptionMapper.ForbiddenException(RESOURCE_BUNDLE.getString("error.response.403_upgrade_subscription")));
35+
(msg) -> new ExitCodeExceptionMapper.ForbiddenException(RESOURCE_BUNDLE.getString("error.response.403_upgrade_subscription")));
3536
put((code, message) -> code.equals("403") && !message.contains("Merge is possible only into main branch"),
36-
new ExitCodeExceptionMapper.ForbiddenException(RESOURCE_BUNDLE.getString("error.response.403")));
37+
(msg) -> new ExitCodeExceptionMapper.ForbiddenException(RESOURCE_BUNDLE.getString("error.response.403")));
3738
put((code, message) -> code.equals("404") && StringUtils.containsIgnoreCase(message, "Project Not Found"),
38-
new ExitCodeExceptionMapper.NotFoundException(RESOURCE_BUNDLE.getString("error.response.404_project_not_found")));
39+
(msg) -> new ExitCodeExceptionMapper.NotFoundException(RESOURCE_BUNDLE.getString("error.response.404_project_not_found")));
3940
put((code, message) -> code.equals("404") && StringUtils.containsIgnoreCase(message, "Organization Not Found"),
40-
new ExitCodeExceptionMapper.NotFoundException(RESOURCE_BUNDLE.getString("error.response.404_organization_not_found")));
41+
(msg) -> new ExitCodeExceptionMapper.NotFoundException(RESOURCE_BUNDLE.getString("error.response.404_organization_not_found")));
42+
put((code, message) -> code.equals("404") && StringUtils.containsIgnoreCase(message, "Bundle Not Found"),
43+
(msg) -> new ExitCodeExceptionMapper.NotFoundException(RESOURCE_BUNDLE.getString("error.bundle.not_found_by_id")));
4144
put((code, message) -> code.equals("429"),
42-
new ExitCodeExceptionMapper.RateLimitException(RESOURCE_BUNDLE.getString("error.response.429")));
45+
(msg) -> new ExitCodeExceptionMapper.RateLimitException(RESOURCE_BUNDLE.getString("error.response.429")));
4346
put((code, message) -> StringUtils.containsAny(message,
4447
"PKIX path building failed",
4548
"sun.security.provider.certpath.SunCertPathBuilderException",
4649
"unable to find valid certification path to requested target"),
47-
new RuntimeException(RESOURCE_BUNDLE.getString("error.response.certificate")));
50+
(msg) -> new RuntimeException(RESOURCE_BUNDLE.getString("error.response.certificate")));
4851
put((code, message) -> message.equals("Name or service not known"),
49-
new RuntimeException(RESOURCE_BUNDLE.getString("error.response.url_not_known")));
52+
(msg) -> new RuntimeException(RESOURCE_BUNDLE.getString("error.response.url_not_known")));
5053
put((code, message) -> code.equals("<empty_code>") && message.equals("<empty_message>"),
51-
new RuntimeException("Empty error message from server"));
54+
(msg) -> new RuntimeException("Empty error message from server"));
55+
put((code, message) -> code.equals("404"),
56+
ExitCodeExceptionMapper.NotFoundException::new);
57+
put((code, message) -> code.equals("403"),
58+
ExitCodeExceptionMapper.ForbiddenException::new);
5259
}};
5360

5461
/**
@@ -128,9 +135,9 @@ private static <R extends Exception> void searchErrorHandler(Map<BiPredicate<Str
128135
throw errorHandler.getValue();
129136
}
130137
}
131-
for (Map.Entry<BiPredicate<String, String>, RuntimeException> errorHandler : standardErrorHandlers.entrySet()) {
138+
for (Map.Entry<BiPredicate<String, String>, Function<String, RuntimeException>> errorHandler : standardErrorHandlers.entrySet()) {
132139
if (errorHandler.getKey().test(code, message)) {
133-
throw errorHandler.getValue();
140+
throw errorHandler.getValue().apply(message);
134141
}
135142
}
136143
}

src/main/java/com/crowdin/cli/commands/actions/BundleDownloadAction.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ public void act(Outputter out, ProjectProperties pb, ClientBundle client) {
8383
}
8484

8585
private Bundle getBundle(ClientBundle client) {
86-
return client.getBundle(id)
87-
.orElseThrow(() -> new RuntimeException(RESOURCE_BUNDLE.getString("error.bundle.not_found_by_id")));
86+
return client.getBundle(id);
8887
}
8988

9089
private BundleExport buildBundle(Outputter out, ClientBundle client, Long bundleId) {

src/test/java/com/crowdin/cli/commands/actions/BundleDownloadActionTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public void testDownloadBundle() {
5555
when(client.downloadBundle(bundle.getId(), null))
5656
.thenReturn(urlMock);
5757
when(client.getBundle(bundle.getId()))
58-
.thenReturn(Optional.of(bundle));
58+
.thenReturn(bundle);
5959

6060
when(client.startExportingBundle(bundle.getId()))
6161
.thenReturn(export);
@@ -93,7 +93,7 @@ public void testDryRun() {
9393
when(client.downloadBundle(bundle.getId(), null))
9494
.thenReturn(urlMock);
9595
when(client.getBundle(bundle.getId()))
96-
.thenReturn(Optional.of(bundle));
96+
.thenReturn(bundle);
9797

9898
when(client.startExportingBundle(bundle.getId()))
9999
.thenReturn(export);

0 commit comments

Comments
 (0)