22
33import com .crowdin .client .core .http .exceptions .CrowdinApiException ;
44import com .crowdin .client .core .http .exceptions .HttpBadRequestException ;
5+ import com .crowdin .client .core .http .exceptions .HttpBatchBadRequestException ;
56import com .crowdin .client .core .http .exceptions .HttpException ;
67import com .fasterxml .jackson .core .JsonParser ;
78import com .fasterxml .jackson .core .TreeNode ;
1011import com .fasterxml .jackson .databind .ObjectMapper ;
1112
1213import java .io .IOException ;
13- import java .util .List ;
14- import java .util .stream .Collectors ;
15- import java .util .stream .StreamSupport ;
1614
1715public class CrowdinApiExceptionDeserializer extends JsonDeserializer <CrowdinApiException > {
1816
@@ -25,14 +23,18 @@ public CrowdinApiExceptionDeserializer(ObjectMapper objectMapper) {
2523 @ Override
2624 public CrowdinApiException deserialize (JsonParser p , DeserializationContext ctxt ) throws IOException {
2725 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 );
3638 } else {
3739 return HttpException .fromMessage (treeNode .toString ());
3840 }
0 commit comments