Skip to content

Commit 19f9cab

Browse files
authored
Apply ResponseError feedback and move DateTimeRange to Azure.Monitor.Query (Azure#23646)
1 parent 1ce06be commit 19f9cab

26 files changed

+255
-186
lines changed

sdk/core/Azure.Core.Experimental/api/Azure.Core.Experimental.netstandard2.0.cs

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -48,26 +48,6 @@ namespace Azure.Core
4848
public static bool operator !=(Azure.Core.ContentType left, Azure.Core.ContentType right) { throw null; }
4949
public override string ToString() { throw null; }
5050
}
51-
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
52-
public readonly partial struct DateTimeRange : System.IEquatable<Azure.Core.DateTimeRange>
53-
{
54-
public DateTimeRange(System.DateTimeOffset start, System.DateTimeOffset end) { throw null; }
55-
public DateTimeRange(System.DateTimeOffset start, System.TimeSpan duration) { throw null; }
56-
public DateTimeRange(System.TimeSpan duration) { throw null; }
57-
public DateTimeRange(System.TimeSpan duration, System.DateTimeOffset end) { throw null; }
58-
public static Azure.Core.DateTimeRange All { get { throw null; } }
59-
public System.TimeSpan Duration { get { throw null; } }
60-
public System.DateTimeOffset? End { get { throw null; } }
61-
public System.DateTimeOffset? Start { get { throw null; } }
62-
public bool Equals(Azure.Core.DateTimeRange other) { throw null; }
63-
public override bool Equals(object obj) { throw null; }
64-
public override int GetHashCode() { throw null; }
65-
public static bool operator ==(Azure.Core.DateTimeRange left, Azure.Core.DateTimeRange right) { throw null; }
66-
public static implicit operator Azure.Core.DateTimeRange (System.TimeSpan timeSpan) { throw null; }
67-
public static bool operator !=(Azure.Core.DateTimeRange left, Azure.Core.DateTimeRange right) { throw null; }
68-
public static Azure.Core.DateTimeRange Parse(string value) { throw null; }
69-
public override string ToString() { throw null; }
70-
}
7151
[System.Diagnostics.DebuggerDisplayAttribute("Content: {_body}")]
7252
public partial class DynamicContent : Azure.Core.RequestContent
7353
{
@@ -203,22 +183,4 @@ public partial class ProtocolClientOptions : Azure.Core.ClientOptions
203183
{
204184
public ProtocolClientOptions() { }
205185
}
206-
public sealed partial class ResponseError
207-
{
208-
public ResponseError(string? code, string? message, Azure.Core.ResponseInnerError? innerError, string? target, System.Collections.Generic.IReadOnlyList<Azure.Core.ResponseError>? details) { }
209-
public string? Code { get { throw null; } }
210-
public System.Collections.Generic.IReadOnlyList<Azure.Core.ResponseError> Details { get { throw null; } }
211-
public Azure.Core.ResponseInnerError? InnerError { get { throw null; } }
212-
public string? Message { get { throw null; } }
213-
public string? Target { get { throw null; } }
214-
public override string ToString() { throw null; }
215-
}
216-
public sealed partial class ResponseInnerError
217-
{
218-
internal ResponseInnerError() { }
219-
public string? Code { get { throw null; } }
220-
public Azure.Core.ResponseInnerError? InnerError { get { throw null; } }
221-
public string? Message { get { throw null; } }
222-
public override string ToString() { throw null; }
223-
}
224186
}

sdk/core/Azure.Core/api/Azure.Core.net461.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,13 @@ protected Response() { }
208208
protected internal abstract bool TryGetHeader(string name, out string? value);
209209
protected internal abstract bool TryGetHeaderValues(string name, out System.Collections.Generic.IEnumerable<string>? values);
210210
}
211+
public sealed partial class ResponseError
212+
{
213+
public ResponseError(string? code, string? message) { }
214+
public string? Code { get { throw null; } }
215+
public string? Message { get { throw null; } }
216+
public override string ToString() { throw null; }
217+
}
211218
public abstract partial class Response<T>
212219
{
213220
protected Response() { }

sdk/core/Azure.Core/api/Azure.Core.net5.0.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,13 @@ protected Response() { }
208208
protected internal abstract bool TryGetHeader(string name, [System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] out string? value);
209209
protected internal abstract bool TryGetHeaderValues(string name, [System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] out System.Collections.Generic.IEnumerable<string>? values);
210210
}
211+
public sealed partial class ResponseError
212+
{
213+
public ResponseError(string? code, string? message) { }
214+
public string? Code { get { throw null; } }
215+
public string? Message { get { throw null; } }
216+
public override string ToString() { throw null; }
217+
}
211218
public abstract partial class Response<T>
212219
{
213220
protected Response() { }

sdk/core/Azure.Core/api/Azure.Core.netcoreapp2.1.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,13 @@ protected Response() { }
208208
protected internal abstract bool TryGetHeader(string name, out string? value);
209209
protected internal abstract bool TryGetHeaderValues(string name, out System.Collections.Generic.IEnumerable<string>? values);
210210
}
211+
public sealed partial class ResponseError
212+
{
213+
public ResponseError(string? code, string? message) { }
214+
public string? Code { get { throw null; } }
215+
public string? Message { get { throw null; } }
216+
public override string ToString() { throw null; }
217+
}
211218
public abstract partial class Response<T>
212219
{
213220
protected Response() { }

sdk/core/Azure.Core/api/Azure.Core.netstandard2.0.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,13 @@ protected Response() { }
208208
protected internal abstract bool TryGetHeader(string name, out string? value);
209209
protected internal abstract bool TryGetHeaderValues(string name, out System.Collections.Generic.IEnumerable<string>? values);
210210
}
211+
public sealed partial class ResponseError
212+
{
213+
public ResponseError(string? code, string? message) { }
214+
public string? Code { get { throw null; } }
215+
public string? Message { get { throw null; } }
216+
public override string ToString() { throw null; }
217+
}
211218
public abstract partial class Response<T>
212219
{
213220
protected Response() { }

sdk/core/Azure.Core.Experimental/src/ResponseError.cs renamed to sdk/core/Azure.Core/src/ResponseError.cs

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,37 @@
88
using System.Text.Json;
99
using System.Text.Json.Serialization;
1010

11-
namespace Azure.Core
11+
namespace Azure
1212
{
1313
/// <summary>
1414
/// Represents an error returned by an Azure Service.
1515
/// </summary>
1616
[JsonConverter(typeof(Converter))]
1717
public sealed class ResponseError
1818
{
19+
private readonly JsonElement _element;
20+
21+
/// <summary>
22+
/// Initializes a new instance of <see cref="ResponseError"/>.
23+
/// </summary>
24+
/// <param name="code">The error code.</param>
25+
/// <param name="message">The error message.</param>
26+
public ResponseError(string? code, string? message) : this(code, message, null, default)
27+
{
28+
}
29+
1930
/// <summary>
2031
/// Initializes a new instance of <see cref="ResponseError"/>.
2132
/// </summary>
2233
/// <param name="code">The error code.</param>
2334
/// <param name="message">The error message.</param>
35+
/// <param name="element">The raw JSON element the error was parsed from.</param>
2436
/// <param name="innerError">The inner error.</param>
2537
/// <param name="target">The error target.</param>
2638
/// <param name="details">The error details.</param>
27-
public ResponseError(string? code, string? message, ResponseInnerError? innerError, string? target, IReadOnlyList<ResponseError>? details)
39+
internal ResponseError(string? code, string? message, string? target, JsonElement element, ResponseInnerError? innerError = null, IReadOnlyList<ResponseError>? details = null)
2840
{
41+
_element = element;
2942
Code = code;
3043
Message = message;
3144
InnerError = innerError;
@@ -46,17 +59,17 @@ public ResponseError(string? code, string? message, ResponseInnerError? innerErr
4659
/// <summary>
4760
/// Gets the inner error.
4861
/// </summary>
49-
public ResponseInnerError? InnerError { get; }
62+
internal ResponseInnerError? InnerError { get; }
5063

5164
/// <summary>
5265
/// Gets the error target.
5366
/// </summary>
54-
public string? Target { get; }
67+
internal string? Target { get; }
5568

5669
/// <summary>
5770
/// Gets the list of related errors.
5871
/// </summary>
59-
public IReadOnlyList<ResponseError> Details { get; }
72+
internal IReadOnlyList<ResponseError> Details { get; }
6073

6174
private class Converter : JsonConverter<ResponseError?>
6275
{
@@ -113,7 +126,7 @@ private class Converter : JsonConverter<ResponseError?>
113126
}
114127
}
115128

116-
return new ResponseError(code, message, innererror, target, details);
129+
return new ResponseError(code, message, target, element.Clone(), innererror, details);
117130
}
118131

119132
public override void Write(Utf8JsonWriter writer, ResponseError? value, JsonSerializerOptions options)
@@ -127,29 +140,49 @@ public override string ToString()
127140
{
128141
var builder = new StringBuilder();
129142

143+
Append(builder, includeRaw: true);
144+
145+
return builder.ToString();
146+
}
147+
148+
internal void Append(StringBuilder builder, bool includeRaw)
149+
{
130150
builder.AppendFormat(CultureInfo.InvariantCulture, "{0}: {1}{2}", Code, Message, Environment.NewLine);
131151

132152
if (Target != null)
133153
{
134154
builder.AppendFormat(CultureInfo.InvariantCulture, "Target: {0}{1}", Target, Environment.NewLine);
135155
}
136156

137-
if (InnerError != null)
157+
var innerError = InnerError;
158+
159+
if (innerError != null)
138160
{
139-
builder.AppendLine("Inner Error:");
140-
builder.Append(InnerError);
161+
builder.AppendLine();
162+
builder.AppendLine("Inner Errors:");
163+
while (innerError != null)
164+
{
165+
builder.AppendLine(innerError.Code);
166+
innerError = innerError.InnerError;
167+
}
141168
}
142169

143170
if (Details.Count > 0)
144171
{
172+
builder.AppendLine();
145173
builder.AppendLine("Details:");
146174
foreach (var detail in Details)
147175
{
148-
builder.Append(detail);
176+
detail.Append(builder, includeRaw: false);
149177
}
150178
}
151179

152-
return builder.ToString();
180+
if (includeRaw && _element.ValueKind != JsonValueKind.Undefined)
181+
{
182+
builder.AppendLine();
183+
builder.AppendLine("Raw:");
184+
builder.Append(_element.GetRawText());
185+
}
153186
}
154187
}
155188
}

sdk/core/Azure.Core.Experimental/src/ResponseInnerError.cs renamed to sdk/core/Azure.Core/src/ResponseInnerError.cs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,20 @@
77
using System.Text.Json;
88
using System.Text.Json.Serialization;
99

10-
namespace Azure.Core
10+
namespace Azure
1111
{
1212
/// <summary>
1313
/// Represents an inner error.
1414
/// </summary>
1515
[JsonConverter(typeof(Converter))]
16-
public sealed class ResponseInnerError
16+
internal sealed class ResponseInnerError
1717
{
18-
internal ResponseInnerError(string? code, string? message, ResponseInnerError? innerError)
18+
private readonly JsonElement _innerErrorElement;
19+
20+
internal ResponseInnerError(string? code, ResponseInnerError? innerError, JsonElement innerErrorElement)
1921
{
22+
_innerErrorElement = innerErrorElement;
2023
Code = code;
21-
Message = message;
2224
InnerError = innerError;
2325
}
2426

@@ -27,11 +29,6 @@ internal ResponseInnerError(string? code, string? message, ResponseInnerError? i
2729
/// </summary>
2830
public string? Code { get; }
2931

30-
/// <summary>
31-
/// Gets the error message.
32-
/// </summary>
33-
public string? Message { get; }
34-
3532
/// <summary>
3633
/// Gets the inner error.
3734
/// </summary>
@@ -59,19 +56,13 @@ internal class Converter : JsonConverter<ResponseInnerError?>
5956
code = property.GetString();
6057
}
6158

62-
string? message = null;
63-
if (element.TryGetProperty("message", out property))
64-
{
65-
message = property.GetString();
66-
}
67-
6859
ResponseInnerError? innererror = null;
6960
if (element.TryGetProperty("innererror", out property))
7061
{
7162
innererror = Read(property);
7263
}
7364

74-
return new ResponseInnerError(code, message, innererror);
65+
return new ResponseInnerError(code, innererror, element.Clone());
7566
}
7667

7768
public override void Write(Utf8JsonWriter writer, ResponseInnerError? value, JsonSerializerOptions options)
@@ -85,14 +76,19 @@ public override string ToString()
8576
{
8677
var builder = new StringBuilder();
8778

88-
builder.AppendFormat(CultureInfo.InvariantCulture, "{0}: {1}{2}", Code, Message, Environment.NewLine);
79+
Append(builder);
80+
81+
return builder.ToString();
82+
}
83+
84+
internal void Append(StringBuilder builder)
85+
{
86+
builder.AppendFormat(CultureInfo.InvariantCulture, "{0}: {1}", Code, Environment.NewLine);
8987
if (InnerError != null)
9088
{
9189
builder.AppendLine("Inner Error:");
9290
builder.Append(InnerError);
9391
}
94-
95-
return builder.ToString();
9692
}
9793
}
9894
}

sdk/core/Azure.Core.Experimental/tests/ResponseErrorTests.cs renamed to sdk/core/Azure.Core/tests/ResponseErrorTests.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,16 @@ public void CanDeserializeSimple()
3434
Assert.AreEqual("Error target", error.Target);
3535

3636
Assert.AreEqual("MoreDetailedBadError", error.InnerError.Code);
37-
Assert.AreEqual("Inner message", error.InnerError.Message);
37+
3838
Assert.Null(error.InnerError.InnerError);
3939
Assert.AreEqual("BadError: Something wasn't awesome" + Environment.NewLine +
4040
"Target: Error target" + Environment.NewLine +
41-
"Inner Error:" + Environment.NewLine +
42-
"MoreDetailedBadError: Inner message" + Environment.NewLine,
41+
Environment.NewLine +
42+
"Inner Errors:" + Environment.NewLine +
43+
"MoreDetailedBadError" + Environment.NewLine +
44+
Environment.NewLine +
45+
"Raw:" + Environment.NewLine +
46+
"{\"code\":\"BadError\",\"message\":\"Something wasn't awesome\",\"target\":\"Error target\",\"innererror\":{\"code\":\"MoreDetailedBadError\",\"message\":\"Inner message\"}}",
4347
error.ToString());
4448
}
4549

@@ -72,10 +76,8 @@ public void CanDeserializeComplex()
7276
Assert.AreEqual("Error target", error.Target);
7377

7478
Assert.AreEqual("MoreDetailedBadError", error.InnerError.Code);
75-
Assert.AreEqual("Inner message", error.InnerError.Message);
7679

7780
Assert.AreEqual("InnerMoreDetailedBadError", error.InnerError.InnerError.Code);
78-
Assert.AreEqual("Inner Inner message", error.InnerError.InnerError.Message);
7981

8082
Assert.AreEqual("Code 1", error.Details[0].Code);
8183
Assert.AreEqual("Message 1", error.Details[0].Message);
@@ -89,13 +91,17 @@ public void CanDeserializeComplex()
8991

9092
Assert.AreEqual("BadError: Something wasn't awesome" + Environment.NewLine +
9193
"Target: Error target" + Environment.NewLine +
92-
"Inner Error:" + Environment.NewLine +
93-
"MoreDetailedBadError: Inner message" + Environment.NewLine +
94-
"Inner Error:" + Environment.NewLine +
95-
"InnerMoreDetailedBadError: Inner Inner message" + Environment.NewLine +
94+
Environment.NewLine +
95+
"Inner Errors:" + Environment.NewLine +
96+
"MoreDetailedBadError" + Environment.NewLine +
97+
"InnerMoreDetailedBadError" + Environment.NewLine +
98+
Environment.NewLine +
9699
"Details:" + Environment.NewLine +
97100
"Code 1: Message 1" + Environment.NewLine +
98-
"Code 2: Message 2" + Environment.NewLine, error.ToString());
101+
"Code 2: Message 2" + Environment.NewLine +
102+
Environment.NewLine +
103+
"Raw:" + Environment.NewLine +
104+
"{\"code\":\"BadError\",\"message\":\"Something wasn't awesome\",\"target\":\"Error target\",\"details\": [{\"code\":\"Code 1\",\"message\":\"Message 1\"},{\"code\":\"Code 2\",\"message\":\"Message 2\"},null],\"innererror\":{\"code\":\"MoreDetailedBadError\",\"message\":\"Inner message\",\"innererror\":{\"code\":\"InnerMoreDetailedBadError\",\"message\":\"Inner Inner message\"}}}", error.ToString());
99105
}
100106
}
101107
}

0 commit comments

Comments
 (0)