Skip to content

Commit cfe96aa

Browse files
fmt
1 parent 5cd0459 commit cfe96aa

File tree

4 files changed

+97
-35
lines changed

4 files changed

+97
-35
lines changed

databricks-sdk-java/src/main/java/com/databricks/sdk/core/error/ApiErrorBody.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ public abstract class ApiErrorBody {
4141
@JsonProperty("details")
4242
@Nullable public abstract ErrorDetails errorDetails();
4343

44+
@JsonProperty("errorDetailsList")
45+
@Nullable public abstract List<ErrorDetail> errorDetailsList();
46+
4447
/**
4548
* Returns a builder for constructing ApiErrorBody instances with the same values as this
4649
* instance.
@@ -66,6 +69,12 @@ public static Builder builder() {
6669
* @return a list of ErrorDetail objects, or an empty list if errorDetails() is null
6770
*/
6871
public List<ErrorDetail> getErrorDetailsList() {
72+
// If we have a direct errorDetailsList from JSON, use it
73+
if (errorDetailsList() != null) {
74+
return errorDetailsList();
75+
}
76+
77+
// Fallback to converting from ErrorDetails for backward compatibility
6978
if (errorDetails() == null) {
7079
return Collections.emptyList();
7180
}
@@ -150,6 +159,15 @@ public abstract static class Builder {
150159
@JsonProperty("details")
151160
public abstract Builder setErrorDetails(@Nullable ErrorDetails errorDetails);
152161

162+
/**
163+
* Sets the error details list.
164+
*
165+
* @param errorDetailsList the error details list
166+
* @return this builder for method chaining
167+
*/
168+
@JsonProperty("errorDetailsList")
169+
public abstract Builder setErrorDetailsList(@Nullable List<ErrorDetail> errorDetailsList);
170+
153171
/**
154172
* Builds the ApiErrorBody instance.
155173
*

databricks-sdk-java/src/main/java/com/databricks/sdk/core/error/ApiErrors.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.databricks.sdk.core.DatabricksError;
44
import com.databricks.sdk.core.DatabricksException;
55
import com.databricks.sdk.core.http.Response;
6+
import com.databricks.sdk.core.utils.SerDeUtils;
67
import com.fasterxml.jackson.databind.ObjectMapper;
78
import java.io.*;
89
import java.nio.charset.StandardCharsets;
@@ -12,7 +13,7 @@
1213

1314
/** Helper methods for inspecting the response and errors thrown during API requests. */
1415
public class ApiErrors {
15-
private static final ObjectMapper MAPPER = new ObjectMapper();
16+
private static final ObjectMapper MAPPER = SerDeUtils.createMapper();
1617
private static final Pattern HTML_ERROR_REGEX = Pattern.compile("<pre>(.*)</pre>");
1718
private static final ErrorMapper ERROR_MAPPER = new ErrorMapper();
1819

databricks-sdk-java/src/test/java/com/databricks/sdk/core/ApiClientTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ void errorDetails() throws JsonProcessingException {
345345
MyEndpointResponse.class,
346346
DatabricksError.class);
347347
List<ErrorDetail> responseErrors = error.getErrorInfo();
348-
assertEquals(responseErrors.size(), 1);
348+
assertEquals(1, responseErrors.size());
349349
ErrorDetail responseError = responseErrors.get(0);
350350
assertEquals("type.googleapis.com/google.rpc.ErrorInfo", responseError.getType());
351351
assertEquals("reason", responseError.getReason());

databricks-sdk-java/src/test/java/com/databricks/sdk/core/error/ApiErrorBodyDeserializationSuite.java

Lines changed: 76 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44

5+
import com.databricks.sdk.core.error.details.ErrorDetails;
6+
import com.databricks.sdk.core.error.details.ErrorInfo;
7+
import com.databricks.sdk.core.error.details.RequestInfo;
8+
import com.databricks.sdk.core.utils.SerDeUtils;
59
import com.fasterxml.jackson.core.JsonProcessingException;
610
import com.fasterxml.jackson.databind.ObjectMapper;
711
import java.util.*;
@@ -10,69 +14,108 @@
1014
public class ApiErrorBodyDeserializationSuite {
1115
@Test
1216
void deserializeErrorResponse() throws JsonProcessingException {
13-
ObjectMapper mapper = new ObjectMapper();
17+
ObjectMapper mapper = SerDeUtils.createMapper();
1418
String rawResponse =
1519
"{\"error_code\":\"theerrorcode\",\"message\":\"themessage\",\"detail\":\"thescimdetail\",\"status\":\"thescimstatus\",\"scimType\":\"thescimtype\",\"error\":\"theerror\"}";
16-
ApiErrorBody error = mapper.readValue(rawResponse, ApiErrorBody.class);
17-
assertEquals(error.errorCode(), "theerrorcode");
18-
assertEquals(error.message(), "themessage");
19-
assertEquals(error.scimDetail(), "thescimdetail");
20-
assertEquals(error.scimStatus(), "thescimstatus");
21-
assertEquals(error.scimType(), "thescimtype");
22-
assertEquals(error.api12Error(), "theerror");
20+
ApiErrorBody actual = mapper.readValue(rawResponse, ApiErrorBody.class);
21+
22+
ApiErrorBody expected =
23+
ApiErrorBody.builder()
24+
.setErrorCode("theerrorcode")
25+
.setMessage("themessage")
26+
.setScimDetail("thescimdetail")
27+
.setScimStatus("thescimstatus")
28+
.setScimType("thescimtype")
29+
.setApi12Error("theerror")
30+
.build();
31+
32+
assertEquals(expected, actual);
2333
}
2434

35+
@Test
2536
void deserializeErrorResponseWitIntErrorCode() throws JsonProcessingException {
26-
ObjectMapper mapper = new ObjectMapper();
37+
ObjectMapper mapper = SerDeUtils.createMapper();
2738
String rawResponse =
2839
"{\"error_code\":42,\"message\":\"themessage\",\"detail\":\"thescimdetail\",\"status\":\"thescimstatus\",\"scimType\":\"thescimtype\",\"error\":\"theerror\"}";
29-
ApiErrorBody error = mapper.readValue(rawResponse, ApiErrorBody.class);
30-
assertEquals(error.errorCode(), "42");
31-
assertEquals(error.message(), "themessage");
32-
assertEquals(error.scimDetail(), "thescimdetail");
33-
assertEquals(error.scimStatus(), "thescimstatus");
34-
assertEquals(error.scimType(), "thescimtype");
35-
assertEquals(error.api12Error(), "theerror");
40+
ApiErrorBody actual = mapper.readValue(rawResponse, ApiErrorBody.class);
41+
42+
ApiErrorBody expected =
43+
ApiErrorBody.builder()
44+
.setErrorCode("42")
45+
.setMessage("themessage")
46+
.setScimDetail("thescimdetail")
47+
.setScimStatus("thescimstatus")
48+
.setScimType("thescimtype")
49+
.setApi12Error("theerror")
50+
.build();
51+
52+
assertEquals(expected, actual);
3653
}
3754

3855
@Test
3956
void deserializeDetailedResponse() throws JsonProcessingException {
40-
ObjectMapper mapper = new ObjectMapper();
57+
ObjectMapper mapper = SerDeUtils.createMapper();
4158
String rawResponse =
4259
"{\"error_code\":\"theerrorcode\",\"message\":\"themessage\","
4360
+ "\"details\":["
4461
+ "{\"@type\": \"type.googleapis.com/google.rpc.ErrorInfo\", \"reason\":\"detailreason\", \"domain\":\"detaildomain\",\"metadata\":{\"etag\":\"detailsetag\"}}"
4562
+ "]}";
46-
ApiErrorBody error = mapper.readValue(rawResponse, ApiErrorBody.class);
47-
Map<String, String> metadata = new HashMap<>();
48-
metadata.put("etag", "detailsetag");
49-
ErrorDetail errorDetails = error.getErrorDetailsList().get(0);
50-
assertEquals(errorDetails.getType(), "type.googleapis.com/google.rpc.ErrorInfo");
51-
assertEquals(errorDetails.getReason(), "detailreason");
52-
assertEquals(errorDetails.getDomain(), "detaildomain");
53-
assertEquals(errorDetails.getMetadata(), metadata);
63+
ApiErrorBody actual = mapper.readValue(rawResponse, ApiErrorBody.class);
64+
65+
ApiErrorBody expected =
66+
ApiErrorBody.builder()
67+
.setErrorCode("theerrorcode")
68+
.setMessage("themessage")
69+
.setErrorDetails(
70+
ErrorDetails.builder()
71+
.setErrorInfo(
72+
ErrorInfo.builder()
73+
.setReason("detailreason")
74+
.setDomain("detaildomain")
75+
.setMetadata(Collections.singletonMap("etag", "detailsetag"))
76+
.build())
77+
.build())
78+
.build();
79+
80+
assertEquals(expected, actual);
5481
}
5582

5683
// Test that an ApiErrorBody can be deserialized, even if the response includes unexpected
5784
// parameters.
5885
@Test
5986
void handleUnexpectedFieldsInErrorResponse() throws JsonProcessingException {
60-
ObjectMapper mapper = new ObjectMapper();
87+
ObjectMapper mapper = SerDeUtils.createMapper();
6188
String rawResponse =
6289
"{\"error_code\":\"theerrorcode\",\"message\":\"themessage\",\"unexpectedField\":[\"unexpected\"]}";
63-
ApiErrorBody error = mapper.readValue(rawResponse, ApiErrorBody.class);
64-
assertEquals(error.errorCode(), "theerrorcode");
65-
assertEquals(error.message(), "themessage");
90+
ApiErrorBody actual = mapper.readValue(rawResponse, ApiErrorBody.class);
91+
92+
ApiErrorBody expected =
93+
ApiErrorBody.builder().setErrorCode("theerrorcode").setMessage("themessage").build();
94+
95+
assertEquals(expected, actual);
6696
}
6797

6898
@Test
6999
void handleNullMetadataFieldInErrorResponse() throws JsonProcessingException {
70-
ObjectMapper mapper = new ObjectMapper();
100+
ObjectMapper mapper = SerDeUtils.createMapper();
71101
String rawResponse =
72102
"{\"error_code\":\"METASTORE_DOES_NOT_EXIST\",\"message\":\"No metastore assigned for the current workspace.\",\"details\":[{\"@type\":\"type.googleapis.com/google.rpc.RequestInfo\",\"request_id\":\"1888e822-f1b5-4996-85eb-0d2b5cc402e6\",\"serving_data\":\"\"}]}";
73-
ApiErrorBody error = mapper.readValue(rawResponse, ApiErrorBody.class);
103+
ApiErrorBody actual = mapper.readValue(rawResponse, ApiErrorBody.class);
104+
105+
ApiErrorBody expected =
106+
ApiErrorBody.builder()
107+
.setErrorCode("METASTORE_DOES_NOT_EXIST")
108+
.setMessage("No metastore assigned for the current workspace.")
109+
.setErrorDetails(
110+
ErrorDetails.builder()
111+
.setRequestInfo(
112+
RequestInfo.builder()
113+
.setRequestId("1888e822-f1b5-4996-85eb-0d2b5cc402e6")
114+
.setServingData("")
115+
.build())
116+
.build())
117+
.build();
74118

75-
assertEquals(error.errorCode(), "METASTORE_DOES_NOT_EXIST");
76-
assertEquals(error.message(), "No metastore assigned for the current workspace.");
119+
assertEquals(expected, actual);
77120
}
78121
}

0 commit comments

Comments
 (0)