17
17
import java .util .Map ;
18
18
import java .util .function .BiFunction ;
19
19
import java .util .function .BiPredicate ;
20
+ import java .util .function .Function ;
20
21
import java .util .function .Supplier ;
21
22
import java .util .stream .Collectors ;
22
23
@@ -26,29 +27,35 @@ abstract class CrowdinClientCore {
26
27
27
28
private static final long defaultMillisToRetry = 100 ;
28
29
29
- private static final Map <BiPredicate <String , String >, RuntimeException > standardErrorHandlers =
30
+ protected static final Map <BiPredicate <String , String >, Function < String , RuntimeException > > standardErrorHandlers =
30
31
new LinkedHashMap <>() {{
31
32
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" )));
33
34
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" )));
35
36
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" )));
37
38
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" )));
39
40
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" )));
41
44
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" )));
43
46
put ((code , message ) -> StringUtils .containsAny (message ,
44
47
"PKIX path building failed" ,
45
48
"sun.security.provider.certpath.SunCertPathBuilderException" ,
46
49
"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" )));
48
51
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" )));
50
53
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 );
52
59
}};
53
60
54
61
/**
@@ -128,9 +135,9 @@ private static <R extends Exception> void searchErrorHandler(Map<BiPredicate<Str
128
135
throw errorHandler .getValue ();
129
136
}
130
137
}
131
- for (Map .Entry <BiPredicate <String , String >, RuntimeException > errorHandler : standardErrorHandlers .entrySet ()) {
138
+ for (Map .Entry <BiPredicate <String , String >, Function < String , RuntimeException > > errorHandler : standardErrorHandlers .entrySet ()) {
132
139
if (errorHandler .getKey ().test (code , message )) {
133
- throw errorHandler .getValue ();
140
+ throw errorHandler .getValue (). apply ( message ) ;
134
141
}
135
142
}
136
143
}
0 commit comments