Skip to content

Commit f54ada9

Browse files
authored
fix: batch error response processing (#255)
1 parent 3a8316e commit f54ada9

File tree

6 files changed

+128
-11
lines changed

6 files changed

+128
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.crowdin.client.core.http.exceptions;
2+
3+
import lombok.Data;
4+
import lombok.EqualsAndHashCode;
5+
6+
import java.io.Serializable;
7+
import java.util.Collections;
8+
import java.util.List;
9+
10+
@EqualsAndHashCode(callSuper = true)
11+
@Data
12+
public class HttpBatchBadRequestException extends CrowdinApiException {
13+
private final List<BatchErrors> errors;
14+
15+
public HttpBatchBadRequestException() {
16+
this.errors = Collections.emptyList();
17+
}
18+
19+
@Data
20+
public static class BatchErrors implements Serializable {
21+
private Integer index;
22+
private List<ErrorHolder> errors;
23+
24+
}
25+
26+
@Data
27+
public static class ErrorHolder {
28+
private ErrorKey error;
29+
}
30+
31+
@Data
32+
public static class ErrorKey implements Serializable {
33+
private String key;
34+
private List<Error> errors;
35+
}
36+
37+
@Data
38+
public static class Error {
39+
private String code;
40+
private String message;
41+
}
42+
}

src/main/java/com/crowdin/client/core/http/impl/json/CrowdinApiExceptionDeserializer.java

+13-11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.crowdin.client.core.http.exceptions.CrowdinApiException;
44
import com.crowdin.client.core.http.exceptions.HttpBadRequestException;
5+
import com.crowdin.client.core.http.exceptions.HttpBatchBadRequestException;
56
import com.crowdin.client.core.http.exceptions.HttpException;
67
import com.fasterxml.jackson.core.JsonParser;
78
import com.fasterxml.jackson.core.TreeNode;
@@ -10,9 +11,6 @@
1011
import com.fasterxml.jackson.databind.ObjectMapper;
1112

1213
import java.io.IOException;
13-
import java.util.List;
14-
import java.util.stream.Collectors;
15-
import java.util.stream.StreamSupport;
1614

1715
public 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
}

src/test/java/com/crowdin/client/core/http/impl/json/CrowdinApiExceptionDeserializerTest.java

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.crowdin.client.core.http.exceptions.CrowdinApiException;
44
import com.crowdin.client.core.http.exceptions.HttpBadRequestException;
5+
import com.crowdin.client.core.http.exceptions.HttpBatchBadRequestException;
56
import com.crowdin.client.core.http.exceptions.HttpException;
67
import com.fasterxml.jackson.core.JsonFactory;
78
import com.fasterxml.jackson.core.JsonParser;
@@ -21,16 +22,19 @@ public class CrowdinApiExceptionDeserializerTest {
2122

2223
private String error;
2324
private String errorsResponse;
25+
private String batchErrorsResponse;
2426
private String unrecognizedError;
2527

2628
@BeforeEach
2729
void setUp() throws IOException {
2830
String resourceDir = "api/core/error.json";
2931
String errorResponseDir = "api/core/errorsResponse.json";
32+
String batchErrorsResponseDir = "api/core/batchErrorsResponse.json";
3033
String unrecognizedErrorDir = "api/core/unrecognizedError.json";
3134

3235
error = getFile(resourceDir);
3336
errorsResponse = getFile(errorResponseDir);
37+
batchErrorsResponse = getFile(batchErrorsResponseDir);
3438
unrecognizedError = getFile(unrecognizedErrorDir);
3539
}
3640

@@ -48,6 +52,10 @@ private String getFile(String resourcePath) throws IOException {
4852
void testDeserializeHttpBadRequestException() throws IOException {
4953
testDeserialize(errorsResponse, HttpBadRequestException.class);
5054
}
55+
@Test
56+
void testDeserializeHttpBatchBadRequestException() throws IOException {
57+
testDeserialize(batchErrorsResponse, HttpBatchBadRequestException.class);
58+
}
5159

5260
@Test
5361
void testDeserializeHttpException() throws IOException {

src/test/java/com/crowdin/client/sourcestrings/SourceStringsApiTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.crowdin.client.sourcestrings;
22

3+
import com.crowdin.client.core.http.exceptions.HttpBatchBadRequestException;
34
import com.crowdin.client.core.model.PatchOperation;
45
import com.crowdin.client.core.model.PatchRequest;
56
import com.crowdin.client.core.model.ResponseList;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"errors": [
3+
{
4+
"index": 0,
5+
"errors": [
6+
{
7+
"error": {
8+
"key": "identifier",
9+
"errors": [
10+
{
11+
"code": "notUnique",
12+
"message": "Invalid identifier given. Identifier must be unique"
13+
}
14+
]
15+
}
16+
}
17+
]
18+
},
19+
{
20+
"index": 2,
21+
"errors": [
22+
{
23+
"error": {
24+
"key": "identifier",
25+
"errors": [
26+
{
27+
"code": "notUnique",
28+
"message": "Invalid identifier given. Identifier must be unique"
29+
}
30+
]
31+
}
32+
}
33+
]
34+
}
35+
]
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[
2+
{
3+
"op": "add",
4+
"path": "/-",
5+
"value": {
6+
"text": "new added string",
7+
"identifier": "a.b.c",
8+
"context": "context for new string",
9+
"fileId": 5,
10+
"isHidden": false
11+
}
12+
},
13+
{
14+
"op": "remove",
15+
"path": "/2815"
16+
},
17+
{
18+
"op": "add",
19+
"path": "/-",
20+
"value": {
21+
"text": "new added string",
22+
"identifier": "a.b.c",
23+
"context": "context for new string",
24+
"fileId": 5,
25+
"isHidden": false
26+
}
27+
}
28+
]

0 commit comments

Comments
 (0)