Skip to content

Commit e812580

Browse files
authored
✨ add extra error information and rag metadata (#277)
1 parent f964921 commit e812580

File tree

12 files changed

+217
-42
lines changed

12 files changed

+217
-42
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.mindee.InferenceParameters;
44
import com.mindee.input.LocalInputSource;
55
import com.mindee.input.URLInputSource;
6+
import com.mindee.parsing.v2.ErrorResponse;
67
import com.mindee.parsing.v2.InferenceResponse;
78
import com.mindee.parsing.v2.JobResponse;
89
import java.io.IOException;
@@ -44,4 +45,17 @@ public abstract JobResponse reqGetJob(
4445
* @param inferenceId ID of the inference to poll.
4546
*/
4647
abstract public InferenceResponse reqGetInference(String inferenceId);
48+
49+
/**
50+
* Creates an "unknown error" response from an HTTP status code.
51+
*/
52+
protected ErrorResponse makeUnknownError(int statusCode) {
53+
return new ErrorResponse(
54+
"Unknown Error",
55+
"The server returned an Unknown error.",
56+
statusCode,
57+
statusCode + "-000",
58+
null
59+
);
60+
}
4761
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ private MindeeHttpExceptionV2 getHttpError(ClassicHttpResponse response) {
236236
ErrorResponse err = mapper.readValue(rawBody, ErrorResponse.class);
237237

238238
if (err.getDetail() == null) {
239-
err = new ErrorResponse("Unknown error", response.getCode());
239+
err = makeUnknownError(response.getCode());
240240
}
241241
return new MindeeHttpExceptionV2(err.getStatus(), err.getDetail());
242242

@@ -321,10 +321,10 @@ private <R extends CommonResponse> R deserializeOrThrow(
321321
try {
322322
err = mapper.readValue(body, ErrorResponse.class);
323323
if (err.getDetail() == null) {
324-
err = new ErrorResponse("Unknown error", httpStatus);
324+
err = makeUnknownError(httpStatus);
325325
}
326326
} catch (Exception ignored) {
327-
err = new ErrorResponse("Unknown error", httpStatus);
327+
err = makeUnknownError(httpStatus);
328328
}
329329
throw new MindeeHttpExceptionV2(err.getStatus(), err.getDetail());
330330
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.mindee.parsing.v2;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import lombok.AllArgsConstructor;
6+
import lombok.EqualsAndHashCode;
7+
import lombok.Getter;
8+
import lombok.NoArgsConstructor;
9+
10+
/**
11+
* Error item model.
12+
*/
13+
@Getter
14+
@EqualsAndHashCode
15+
@JsonIgnoreProperties(ignoreUnknown = true)
16+
@AllArgsConstructor
17+
@NoArgsConstructor
18+
public final class ErrorItem {
19+
/**
20+
* A JSON Pointer to the location of the body property.
21+
*/
22+
@JsonProperty("pointer")
23+
private String pointer;
24+
25+
/**
26+
* Explicit information on the issue.
27+
*/
28+
@JsonProperty("detail")
29+
private String detail;
30+
}

src/main/java/com/mindee/parsing/v2/ErrorResponse.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
44
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import java.util.List;
56
import lombok.AllArgsConstructor;
67
import lombok.EqualsAndHashCode;
78
import lombok.Getter;
89
import lombok.NoArgsConstructor;
910

1011
/**
11-
* Error information from the API.
12+
* Error response detailing a problem. The format adheres to RFC 9457.
1213
*/
1314
@Getter
1415
@EqualsAndHashCode
@@ -17,20 +18,38 @@
1718
@NoArgsConstructor
1819
public final class ErrorResponse {
1920
/**
20-
* Detail relevant to the error.
21+
* A short, human-readable summary of the problem.
22+
*/
23+
@JsonProperty("title")
24+
private String title;
25+
26+
/**
27+
* A human-readable explanation specific to the occurrence of the problem.
2128
*/
2229
@JsonProperty("detail")
2330
private String detail;
2431

2532
/**
26-
* HTTP error code.
33+
* The HTTP status code returned by the server.
2734
*/
2835
@JsonProperty("status")
2936
private int status;
3037

38+
/**
39+
* A machine-readable code specific to the occurrence of the problem.
40+
*/
41+
@JsonProperty("code")
42+
private String code;
43+
44+
/**
45+
* The HTTP status code returned by the server.
46+
*/
47+
@JsonProperty("errors")
48+
private List<ErrorItem> errors;
49+
3150
/** For prettier display. */
3251
@Override
3352
public String toString() {
34-
return "HTTP Status: " + status + " - " + detail;
53+
return "HTTP " + status + " - " + title + " :: " + code + " - " + detail;
3554
}
3655
}

src/main/java/com/mindee/parsing/v2/InferenceResult.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,23 @@
2020
public final class InferenceResult {
2121

2222
/**
23-
* Model fields.
23+
* Extracted fields, the key corresponds to the field's name in the data schema.
2424
*/
2525
@JsonProperty("fields")
2626
private InferenceFields fields;
2727

2828
/**
29-
* Options.
29+
* Raw text extracted from all pages in the document.
3030
*/
3131
@JsonProperty("raw_text")
3232
private RawText rawText;
3333

34+
/**
35+
* RAG metadata.
36+
*/
37+
@JsonProperty("rag")
38+
private RagMetadata rag;
39+
3440
@Override
3541
public String toString() {
3642
StringJoiner joiner = new StringJoiner("\n");
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.mindee.parsing.v2;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import lombok.AllArgsConstructor;
6+
import lombok.Getter;
7+
import lombok.NoArgsConstructor;
8+
9+
/**
10+
* RAG metadata.
11+
*/
12+
@Getter
13+
@JsonIgnoreProperties(ignoreUnknown = true)
14+
@AllArgsConstructor
15+
@NoArgsConstructor
16+
public final class RagMetadata {
17+
/**
18+
* The UUID of the matched document used during the RAG operation.
19+
*/
20+
@JsonProperty("retrieved_document_id")
21+
private String retrievedDocumentId;
22+
}

src/main/java/com/mindee/parsing/v2/RawText.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
@AllArgsConstructor
1717
@NoArgsConstructor
1818
public class RawText {
19-
/*
19+
/**
2020
* Page Number the text was found on.
2121
*/
2222
@JsonProperty("pages")

src/test/java/com/mindee/MindeeClientV2IT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
2222
@Tag("integration")
23-
@DisplayName("MindeeClientV2integration tests (V2)")
23+
@DisplayName("MindeeV2Integration Tests")
2424
class MindeeClientV2IT {
2525

2626
private MindeeClientV2 mindeeClient;

src/test/java/com/mindee/MindeeClientV2Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import static org.mockito.ArgumentMatchers.*;
2020
import static org.mockito.Mockito.*;
2121

22-
@DisplayName("MindeeClientV2client / API interaction tests")
22+
@DisplayName("MindeeV2Client and API Tests")
2323
class MindeeClientV2Test {
2424
/**
2525
* Creates a fully mocked MindeeClientV2.

src/test/java/com/mindee/TestingUtilities.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ public static Path getV1ResourcePath(String filePath) {
1818
return Paths.get("src/test/resources/v1/" + filePath);
1919
}
2020

21+
public static Path getV2ResourcePath(String filePath) {
22+
return Paths.get("src/test/resources/v2/" + filePath);
23+
}
24+
2125
public static String getV1ResourcePathString(String filePath) {
2226
return getV1ResourcePath(filePath).toString();
2327
}

0 commit comments

Comments
 (0)