diff --git a/azure-client-runtime/src/main/java/com/microsoft/azure/CloudError.java b/azure-client-runtime/src/main/java/com/microsoft/azure/CloudError.java index 05075d81ba..4f2858dd87 100644 --- a/azure-client-runtime/src/main/java/com/microsoft/azure/CloudError.java +++ b/azure-client-runtime/src/main/java/com/microsoft/azure/CloudError.java @@ -6,6 +6,8 @@ package com.microsoft.azure; +import com.fasterxml.jackson.databind.node.ObjectNode; + import java.util.ArrayList; import java.util.List; @@ -33,6 +35,11 @@ public final class CloudError { */ private List<CloudError> details; + /** + * The inner error. + */ + private ObjectNode innerError; + /** * Initializes a new instance of CloudError. */ @@ -100,4 +107,11 @@ public CloudError withTarget(String target) { public List<CloudError> details() { return details; } + + /** + * @return the inner error + */ + public ObjectNode innerError() { + return innerError; + } } diff --git a/azure-client-runtime/src/main/java/com/microsoft/azure/serializer/CloudErrorDeserializer.java b/azure-client-runtime/src/main/java/com/microsoft/azure/serializer/CloudErrorDeserializer.java index a38cf86b41..6d2c03435e 100644 --- a/azure-client-runtime/src/main/java/com/microsoft/azure/serializer/CloudErrorDeserializer.java +++ b/azure-client-runtime/src/main/java/com/microsoft/azure/serializer/CloudErrorDeserializer.java @@ -59,7 +59,8 @@ public CloudError deserialize(JsonParser p, DeserializationContext ctxt) throws nodeContent = nodeContent.replaceFirst("(?i)\"code\"", "\"code\"") .replaceFirst("(?i)\"message\"", "\"message\"") .replaceFirst("(?i)\"target\"", "\"target\"") - .replaceFirst("(?i)\"details\"", "\"details\""); + .replaceFirst("(?i)\"details\"", "\"details\"") + .replaceFirst("(?i)\"innererror\"", "\"innerError\""); JsonParser parser = new JsonFactory().createParser(nodeContent); parser.setCodec(mapper); return parser.readValueAs(CloudError.class); diff --git a/azure-client-runtime/src/test/java/com/microsoft/azure/CloudErrorDeserializerTests.java b/azure-client-runtime/src/test/java/com/microsoft/azure/CloudErrorDeserializerTests.java new file mode 100644 index 0000000000..574a502879 --- /dev/null +++ b/azure-client-runtime/src/test/java/com/microsoft/azure/CloudErrorDeserializerTests.java @@ -0,0 +1,45 @@ +/** + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for + * license information. + */ + +package com.microsoft.azure; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.microsoft.azure.serializer.AzureJacksonAdapter; +import com.microsoft.rest.protocol.SerializerAdapter; +import org.junit.Assert; +import org.junit.Test; + +public class CloudErrorDeserializerTests { + @Test + public void errorDeserializedFully() throws Exception { + SerializerAdapter<ObjectMapper> serializerAdapter = new AzureJacksonAdapter(); + String bodyString = + "{" + + " \"error\": {" + + " \"code\": \"BadArgument\"," + + " \"message\": \"The provided database ‘foo’ has an invalid username.\"," + + " \"target\": \"query\"," + + " \"details\": [" + + " {" + + " \"code\": \"301\"," + + " \"target\": \"$search\"," + + " \"message\": \"$search query option not supported\"" + + " }" + + " ]," + + " \"innererror\": {" + + " \"customKey\": \"customValue\"" + + " }" + + " }" + + "}"; + CloudError cloudError = serializerAdapter.deserialize(bodyString, CloudError.class); + + Assert.assertEquals("BadArgument", cloudError.code()); + Assert.assertEquals("The provided database ‘foo’ has an invalid username.", cloudError.message()); + Assert.assertEquals("query", cloudError.target()); + Assert.assertEquals("301", cloudError.details().get(0).code()); + Assert.assertEquals("customValue", cloudError.innerError().get("customKey").asText()); + } +}