Skip to content

Commit 6c54385

Browse files
authored
✨ handle job errors separately from API errors (#150)
1 parent 13dac92 commit 6c54385

File tree

9 files changed

+48
-16
lines changed

9 files changed

+48
-16
lines changed

src/main/java/com/mindee/http/MindeeHttpApi.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,18 @@ public <DocT extends Inference> AsyncPredictResponse<DocT> documentQueueGet(
147147
String rawResponse = readRawResponse(responseEntity);
148148
AsyncPredictResponse<DocT> mappedResponse = mapper.readValue(rawResponse, type);
149149
mappedResponse.setRawResponse(rawResponse);
150+
if (
151+
mappedResponse.getJob() != null
152+
&& mappedResponse.getJob().getError() != null
153+
&& mappedResponse.getJob().getError().getCode() != null
154+
) {
155+
throw new MindeeHttpException(
156+
500,
157+
mappedResponse.getJob().getError().getMessage(),
158+
mappedResponse.getJob().getError().getDetails().toString(),
159+
mappedResponse.getJob().getError().getCode()
160+
);
161+
}
150162
return mappedResponse;
151163
} catch (IOException err) {
152164
throw new MindeeException(err.getMessage(), err);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
public class ApiRequest {
1717

1818
/**
19-
* Represent an error information from the API response.
19+
* Information about an error that occurred during the API request.
2020
*/
2121
@JsonProperty("error")
2222
private Error error;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
@EqualsAndHashCode
1515
@JsonIgnoreProperties(ignoreUnknown = true)
1616
public class Cropper {
17-
/*
17+
/**
1818
* List of positions within the image.
1919
*/
2020
@JsonProperty("cropping")

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import lombok.Getter;
66

77
/**
8-
* Represent an error information from the API response.
8+
* Error information from an API response.
99
*/
1010
@Getter
1111
@JsonIgnoreProperties(ignoreUnknown = true)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
@EqualsAndHashCode
1313
@JsonIgnoreProperties(ignoreUnknown = true)
1414
public class Extras {
15-
/*
15+
/**
1616
* Cropping result.
1717
*/
1818
@JsonProperty("cropper")

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,36 @@
2828
@NoArgsConstructor
2929
public class Job {
3030

31-
/*
31+
/**
3232
* The time at which the job finished
3333
*/
3434
@JsonProperty("available_at")
3535
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
3636
private LocalDateTime availableAt;
37-
/*
37+
38+
/**
3839
* Identifier for the job
3940
*/
4041
private String id;
41-
/*
42+
43+
/**
4244
* The time at which the job started
4345
*/
4446
@JsonProperty("issued_at")
4547
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
4648
private LocalDateTime issuedAt;
47-
/*
49+
50+
/**
4851
* Job Status
4952
*/
5053
private String status;
5154

55+
/**
56+
* Information about an error that occurred during the job processing.
57+
*/
58+
@JsonProperty("error")
59+
private Error error;
60+
5261
/**
5362
* Private Deserializer for LocalDateTime
5463
*/

src/test/java/com/mindee/MindeeClientTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ void givenAnAsyncDoc_whenEnqueued_shouldInvokeApiCorrectly() throws IOException
285285
File file = new File("src/test/resources/file_types/pdf/blank_1.pdf");
286286
LocalInputSource localInputSource = new LocalInputSource(file);
287287

288-
Job job = new Job(LocalDateTime.now(),"someid",LocalDateTime.now(),"Completed");
288+
Job job = new Job(LocalDateTime.now(), "someid", LocalDateTime.now(), "Completed", null);
289289
AsyncPredictResponse predictResponse = new AsyncPredictResponse();
290290
predictResponse.setDocument(new Document<>());
291291
predictResponse.setApiRequest(null);
@@ -325,7 +325,7 @@ void givenAnAsyncDoc_whenEnqueuedNoParams_shouldInvokeApiCorrectly() throws IOEx
325325
File file = new File("src/test/resources/file_types/pdf/blank_1.pdf");
326326
LocalInputSource localInputSource = new LocalInputSource(file);
327327

328-
Job job = new Job(LocalDateTime.now(),"someid",LocalDateTime.now(),"Completed");
328+
Job job = new Job(LocalDateTime.now(), "someid", LocalDateTime.now(), "Completed", null);
329329
AsyncPredictResponse predictResponse = new AsyncPredictResponse();
330330
predictResponse.setDocument(new Document<>());
331331
predictResponse.setApiRequest(null);
@@ -361,7 +361,7 @@ void givenAnAsyncDoc_whenEnqueuedNoParams_shouldInvokeApiCorrectly() throws IOEx
361361
@Test
362362
void givenAnAsyncUrl_whenEnqueued_shouldInvokeApiCorrectly() throws IOException {
363363

364-
Job job = new Job(LocalDateTime.now(),"someid",LocalDateTime.now(),"completed");
364+
Job job = new Job(LocalDateTime.now(), "someid", LocalDateTime.now(), "completed", null);
365365
AsyncPredictResponse predictResponse = new AsyncPredictResponse();
366366
predictResponse.setDocument(new Document<>());
367367
predictResponse.setApiRequest(null);

src/test/java/com/mindee/parsing/common/AsyncPredictResponseTest.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,7 @@ void whenAsyncPost_returnsErrorForbidden_mustBeDeserialized() throws IOException
4040
Assertions.assertNotNull(response);
4141
Assertions.assertEquals("failure", response.getApiRequest().getStatus());
4242
Assertions.assertEquals(403, response.getApiRequest().getStatusCode());
43-
Assertions.assertNull(response.getJob().getStatus());
44-
Assertions.assertEquals("2023-01-01T00:00", response.getJob().getIssuedAt().toString());
45-
Assertions.assertNull(response.getJob().getAvailableAt());
46-
Assertions.assertFalse(response.getDocument().isPresent());
43+
Assertions.assertNull(response.getJob());
4744
}
4845

4946
@Test
@@ -87,4 +84,18 @@ void whenAsyncGet_returnsCompleted_mustBeDeserialized() throws IOException {
8784
Assertions.assertEquals(2, response.getDocumentObj().getNPages());
8885
}
8986

87+
@Test
88+
void whenAsyncGet_returnsJobFailed_mustBeDeserialized() throws IOException {
89+
AsyncPredictResponse<InvoiceSplitterV1> response = loadAsyncResponse(
90+
"src/test/resources/async/get_failed_job_error.json"
91+
);
92+
Assertions.assertNotNull(response);
93+
Assertions.assertEquals("success", response.getApiRequest().getStatus());
94+
Assertions.assertEquals(200, response.getApiRequest().getStatusCode());
95+
Assertions.assertEquals("failed", response.getJob().getStatus());
96+
Assertions.assertEquals("ServerError", response.getJob().getError().getCode());
97+
Assertions.assertEquals("2024-02-20T10:31:06.878599", response.getJob().getIssuedAt().toString());
98+
Assertions.assertEquals("2024-02-20T10:31:06.878599", response.getJob().getAvailableAt().toString());
99+
Assertions.assertFalse(response.getDocument().isPresent());
100+
}
90101
}

0 commit comments

Comments
 (0)