2
2
3
3
import com .crowdin .client .core .http .exceptions .CrowdinApiException ;
4
4
import com .crowdin .client .core .http .exceptions .HttpBadRequestException ;
5
+ import com .crowdin .client .core .http .exceptions .HttpBatchBadRequestException ;
5
6
import com .crowdin .client .core .http .exceptions .HttpException ;
6
7
import com .fasterxml .jackson .core .JsonParser ;
7
8
import com .fasterxml .jackson .core .TreeNode ;
10
11
import com .fasterxml .jackson .databind .ObjectMapper ;
11
12
12
13
import java .io .IOException ;
13
- import java .util .List ;
14
- import java .util .stream .Collectors ;
15
- import java .util .stream .StreamSupport ;
16
14
17
15
public class CrowdinApiExceptionDeserializer extends JsonDeserializer <CrowdinApiException > {
18
16
@@ -25,14 +23,18 @@ public CrowdinApiExceptionDeserializer(ObjectMapper objectMapper) {
25
23
@ Override
26
24
public CrowdinApiException deserialize (JsonParser p , DeserializationContext ctxt ) throws IOException {
27
25
TreeNode treeNode = p .getCodec ().readTree (p );
28
- Iterable <String > iterable = treeNode ::fieldNames ;
29
- List <String > fields = StreamSupport
30
- .stream (iterable .spliterator (), false )
31
- .collect (Collectors .toList ());
32
- if (fields .contains ("errors" )) {
33
- return this .objectMapper .readValue (treeNode .toString (), HttpBadRequestException .class );
34
- } else if (fields .contains ("error" )) {
35
- return this .objectMapper .readValue (treeNode .toString (), HttpException .class );
26
+ TreeNode errors = treeNode .get ("errors" );
27
+
28
+ if (errors != null ) {
29
+ TreeNode firstElement = errors .get (0 );
30
+
31
+ if (firstElement != null && firstElement .get ("index" ) != null ) {
32
+ return this .objectMapper .treeToValue (treeNode , HttpBatchBadRequestException .class );
33
+ }
34
+
35
+ return this .objectMapper .treeToValue (treeNode , HttpBadRequestException .class );
36
+ } else if (treeNode .get ("error" ) != null ) {
37
+ return this .objectMapper .treeToValue (treeNode , HttpException .class );
36
38
} else {
37
39
return HttpException .fromMessage (treeNode .toString ());
38
40
}
0 commit comments