Skip to content

Commit 18717bf

Browse files
committed
🐛! Details property in Error become an object because it can handle both string and object
1 parent 12f9d73 commit 18717bf

File tree

6 files changed

+138
-2
lines changed

6 files changed

+138
-2
lines changed

src/main/java/com/mindee/parsing/common/Error.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class Error {
1616
* Details about it.
1717
*/
1818
@JsonProperty("details")
19-
private String details;
19+
private ErrorDetails details;
2020
/**
2121
* More precise information about the current error.
2222
*/
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.mindee.parsing.common;
2+
3+
4+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
5+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
6+
import lombok.Getter;
7+
8+
/**
9+
* Represent error details.
10+
*/
11+
@Getter
12+
@JsonIgnoreProperties(ignoreUnknown = true)
13+
@JsonDeserialize(using = ErrorDetailsDeserializer.class)
14+
public class ErrorDetails {
15+
16+
/**
17+
* More precise information about the current error.
18+
*/
19+
private final String value;
20+
21+
public ErrorDetails(String details) {
22+
this.value = details;
23+
}
24+
25+
@Override
26+
public String toString() {
27+
return this.value;
28+
}
29+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.mindee.parsing.common;
2+
3+
import com.fasterxml.jackson.core.JsonParser;
4+
import com.fasterxml.jackson.databind.DeserializationContext;
5+
import com.fasterxml.jackson.databind.JsonNode;
6+
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
7+
8+
import java.io.IOException;
9+
import java.io.NotSerializableException;
10+
11+
public class ErrorDetailsDeserializer extends StdDeserializer<ErrorDetails> {
12+
13+
public ErrorDetailsDeserializer(Class<?> vc) {
14+
super(vc);
15+
}
16+
17+
public ErrorDetailsDeserializer() {
18+
this(null);
19+
}
20+
21+
@Override
22+
public ErrorDetails deserialize(JsonParser jsonParser,
23+
DeserializationContext deserializationContext) throws IOException {
24+
25+
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
26+
String details;
27+
28+
if(node.isObject())
29+
{
30+
details = node.toString();
31+
} else if (node.isTextual()) {
32+
details = node.textValue();
33+
} else {
34+
throw new NotSerializableException();
35+
}
36+
37+
return new ErrorDetails(details);
38+
}
39+
}

src/test/java/com/mindee/http/MindeeHttpApiTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.mindee.MindeeSettings;
55
import com.mindee.parsing.common.Document;
66
import com.mindee.parsing.invoice.InvoiceV4Inference;
7+
import com.mindee.utils.MindeeException;
78
import junit.framework.TestCase;
89
import okhttp3.mockwebserver.MockResponse;
910
import okhttp3.mockwebserver.MockWebServer;
@@ -60,4 +61,27 @@ void givenAResponseFromTheEndpoint_whenDeserialized_mustHaveValidSummary()
6061
Assertions.assertNotNull(document);
6162
Assertions.assertEquals(expectedSummary, actualSummary);
6263
}
64+
65+
@Test
66+
void givenError_withDetailNodeAsAnObject_mustThrowMindeeException()
67+
throws IOException {
68+
69+
String url = String.format("http://localhost:%s", mockWebServer.getPort());
70+
Path path = Paths.get("src/test/resources/data/errors/with_object_response_in_detail.json");
71+
mockWebServer.enqueue(new MockResponse()
72+
.setResponseCode(400)
73+
.setBody(new String(Files.readAllBytes(path))));
74+
75+
File file = new File("src/test/resources/data/invoice/invoice.pdf");
76+
MindeeHttpApi client = new MindeeHttpApi(new MindeeSettings("abc", url));
77+
78+
Assertions.assertThrows(
79+
MindeeException.class,
80+
() -> client.predict(
81+
InvoiceV4Inference.class,
82+
ParseParameter.builder()
83+
.file(Files.readAllBytes(file.toPath()))
84+
.fileName(file.getName())
85+
.build()));
86+
}
6387
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.mindee.parsing.common;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import junit.framework.TestCase;
5+
import org.junit.jupiter.api.Assertions;
6+
import org.junit.jupiter.api.Test;
7+
8+
import java.io.File;
9+
import java.io.IOException;
10+
11+
public class ErrorTest extends TestCase {
12+
13+
@Test
14+
void given_details_as_object_mustBeDeserialized() throws IOException {
15+
16+
ObjectMapper objectMapper = new ObjectMapper();
17+
objectMapper.findAndRegisterModules();
18+
19+
Error error = objectMapper.readValue(
20+
new File("src/test/resources/data/errors/with_object_response_in_detail.json"),
21+
Error.class);
22+
23+
Assertions.assertNotNull(error);
24+
Assertions.assertNotNull(error.getDetails());
25+
Assertions.assertEquals(
26+
"{\"document\":[\"error message\"]}",
27+
error.getDetails().toString());
28+
}
29+
30+
@Test
31+
void given_details_as_string_mustBeDeserialized() throws IOException {
32+
33+
ObjectMapper objectMapper = new ObjectMapper();
34+
objectMapper.findAndRegisterModules();
35+
36+
Error error = objectMapper.readValue(
37+
new File("src/test/resources/data/errors/with_string_response_in_detail.json"),
38+
Error.class);
39+
40+
Assertions.assertNotNull(error);
41+
Assertions.assertNotNull(error.getDetails());
42+
Assertions.assertEquals("error message", error.getDetails().toString());
43+
}
44+
}

0 commit comments

Comments
 (0)