Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'innererror' property bag to CloudError contract #338

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

package com.microsoft.azure;

import com.fasterxml.jackson.databind.node.ObjectNode;

import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -33,6 +35,11 @@ public final class CloudError {
*/
private List<CloudError> details;

/**
* The inner error.
*/
private ObjectNode innererror;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the format of the innerError? Will it be a HashMap? (judging on your test)

Also please camel case the variables and methods.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be an arbitrary JSON struct with an object at the top level. Inside could be multiple levels of nesting, arrays and numbers.


/**
* Initializes a new instance of CloudError.
*/
Expand Down Expand Up @@ -100,4 +107,11 @@ public CloudError withTarget(String target) {
public List<CloudError> details() {
return details;
}

/**
* @return the inner error
*/
public ObjectNode innererror() {
return innererror;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}