-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Expose error details in AMQ APIs #22114
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
Merged
pakrym
merged 8 commits into
Azure:main
from
pakrym:pakrym/Expose-error-details-in-AMQ-APIs
Jun 24, 2021
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e2029e0
Expose error details in AMQ APIs
pakrym d91f486
snippet
pakrym 6e61761
AzureError
pakrym 37a6ce1
one more
pakrym c9d6b3f
Update sdk/monitor/Azure.Monitor.Query/src/Models/LogsBatchQueryResul…
pakrym 8afaa35
fix test
pakrym ff82d8c
...
pakrym 26ad318
renamesis
pakrym File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization; | ||
|
|
||
| namespace Azure.Core | ||
| { | ||
| /// <summary> | ||
| /// Represents an error returned by an Azure Service. | ||
| /// </summary> | ||
| [JsonConverter(typeof(Converter))] | ||
| public sealed class ResponseError | ||
| { | ||
| internal ResponseError(string? code, string? message, ResponseInnerError? innerError, string? target, IReadOnlyList<ResponseError>? details) | ||
| { | ||
| Code = code; | ||
| Message = message; | ||
| InnerError = innerError; | ||
| Target = target; | ||
| Details = details ?? Array.Empty<ResponseError>(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the error code. | ||
| /// </summary> | ||
| public string? Code { get; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the error message. | ||
| /// </summary> | ||
| public string? Message { get; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the inner error. | ||
| /// </summary> | ||
| public ResponseInnerError? InnerError { get; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the error target. | ||
| /// </summary> | ||
| public string? Target { get; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the list of related errors. | ||
| /// </summary> | ||
| public IReadOnlyList<ResponseError> Details { get; } | ||
|
|
||
| private class Converter : JsonConverter<ResponseError?> | ||
| { | ||
| public override ResponseError? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
| { | ||
| using var document = JsonDocument.ParseValue(ref reader); | ||
| var element = document.RootElement; | ||
| return Read(element); | ||
| } | ||
|
|
||
| private static ResponseError? Read(JsonElement element) | ||
| { | ||
| if (element.ValueKind == JsonValueKind.Null) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| string? code = null; | ||
| if (element.TryGetProperty("code", out var property)) | ||
| { | ||
| code = property.GetString(); | ||
| } | ||
|
|
||
| string? message = null; | ||
| if (element.TryGetProperty("message", out property)) | ||
| { | ||
| message = property.GetString(); | ||
| } | ||
|
|
||
| string? target = null; | ||
| if (element.TryGetProperty("target", out property)) | ||
| { | ||
| target = property.GetString(); | ||
| } | ||
|
|
||
| ResponseInnerError? innererror = null; | ||
| if (element.TryGetProperty("innererror", out property)) | ||
| { | ||
| innererror = ResponseInnerError.Converter.Read(property); | ||
| } | ||
|
|
||
| List<ResponseError>? details = null; | ||
| if (element.TryGetProperty("details", out property) && | ||
| property.ValueKind == JsonValueKind.Array) | ||
| { | ||
| foreach (var item in property.EnumerateArray()) | ||
| { | ||
| var detail = Read(item); | ||
| if (detail != null) | ||
| { | ||
| details ??= new(); | ||
| details.Add(detail); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return new ResponseError(code, message, innererror, target, details); | ||
| } | ||
|
|
||
| public override void Write(Utf8JsonWriter writer, ResponseError? value, JsonSerializerOptions options) | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
| } | ||
| } | ||
| } |
81 changes: 81 additions & 0 deletions
81
sdk/core/Azure.Core.Experimental/src/ResponseInnerError.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization; | ||
|
|
||
| namespace Azure.Core | ||
| { | ||
| /// <summary> | ||
| /// Represents an inner error. | ||
| /// </summary> | ||
| [JsonConverter(typeof(Converter))] | ||
| public sealed class ResponseInnerError | ||
| { | ||
| internal ResponseInnerError(string? code, string? message, ResponseInnerError? innerError) | ||
| { | ||
| Code = code; | ||
| Message = message; | ||
| InnerError = innerError; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the error code. | ||
| /// </summary> | ||
| public string? Code { get; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the error message. | ||
| /// </summary> | ||
| public string? Message { get; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the inner error. | ||
| /// </summary> | ||
| public ResponseInnerError? InnerError { get; } | ||
|
|
||
| internal class Converter : JsonConverter<ResponseInnerError?> | ||
| { | ||
| public override ResponseInnerError? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
| { | ||
| using var document = JsonDocument.ParseValue(ref reader); | ||
| var element = document.RootElement; | ||
| return Read(element); | ||
| } | ||
|
|
||
| internal static ResponseInnerError? Read(JsonElement element) | ||
| { | ||
| if (element.ValueKind == JsonValueKind.Null) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| string? code = null; | ||
| if (element.TryGetProperty("code", out var property)) | ||
| { | ||
| code = property.GetString(); | ||
| } | ||
|
|
||
| string? message = null; | ||
| if (element.TryGetProperty("message", out property)) | ||
| { | ||
| message = property.GetString(); | ||
| } | ||
|
|
||
| ResponseInnerError? innererror = null; | ||
| if (element.TryGetProperty("innererror", out property)) | ||
| { | ||
| innererror = Read(property); | ||
| } | ||
|
|
||
| return new ResponseInnerError(code, message, innererror); | ||
| } | ||
|
|
||
| public override void Write(Utf8JsonWriter writer, ResponseInnerError? value, JsonSerializerOptions options) | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
| } | ||
| } | ||
| } |
85 changes: 85 additions & 0 deletions
85
sdk/core/Azure.Core.Experimental/tests/ResponseErrorTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Text.Json; | ||
| using NUnit.Framework; | ||
|
|
||
| namespace Azure.Core.Tests | ||
| { | ||
| public class ResponseErrorTests | ||
| { | ||
| [Test] | ||
| public void CanDeserializeNull() | ||
| { | ||
| Assert.Null(JsonSerializer.Deserialize<ResponseError>("null")); | ||
| } | ||
|
|
||
| [Test] | ||
| public void CanDeserializeSimple() | ||
| { | ||
| var error = JsonSerializer.Deserialize<ResponseError>( | ||
| "{" + | ||
| "\"code\":\"BadError\"," + | ||
| "\"message\":\"Something wasn't awesome\"," + | ||
| "\"target\":\"Error target\"," + | ||
| "\"innererror\":" + | ||
| "{" + | ||
| "\"code\":\"MoreDetailedBadError\"," + | ||
| "\"message\":\"Inner message\"" + | ||
| "}}"); | ||
|
|
||
| Assert.AreEqual("BadError", error.Code); | ||
| Assert.AreEqual("Something wasn't awesome", error.Message); | ||
| Assert.AreEqual("Error target", error.Target); | ||
|
|
||
| Assert.AreEqual("MoreDetailedBadError", error.InnerError.Code); | ||
| Assert.AreEqual("Inner message", error.InnerError.Message); | ||
| Assert.Null(error.InnerError.InnerError); | ||
| } | ||
|
|
||
| [Test] | ||
| public void CanDeserializeComplex() | ||
| { | ||
| var error = JsonSerializer.Deserialize<ResponseError>( | ||
| "{" + | ||
| "\"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\"" + | ||
| "}"+ | ||
| "}}"); | ||
|
|
||
| Assert.AreEqual("BadError", error.Code); | ||
| Assert.AreEqual("Something wasn't awesome", error.Message); | ||
| Assert.AreEqual("Error target", error.Target); | ||
|
|
||
| Assert.AreEqual("MoreDetailedBadError", error.InnerError.Code); | ||
| Assert.AreEqual("Inner message", error.InnerError.Message); | ||
|
|
||
| Assert.AreEqual("InnerMoreDetailedBadError", error.InnerError.InnerError.Code); | ||
| Assert.AreEqual("Inner Inner message", error.InnerError.InnerError.Message); | ||
|
|
||
| Assert.AreEqual("Code 1", error.Details[0].Code); | ||
| Assert.AreEqual("Message 1", error.Details[0].Message); | ||
|
|
||
| Assert.AreEqual("Code 2", error.Details[1].Code); | ||
| Assert.AreEqual("Message 2", error.Details[1].Message); | ||
|
|
||
| Assert.AreEqual(2, error.Details.Count); | ||
|
|
||
| Assert.Null(error.InnerError.InnerError.InnerError); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's a bit strange that details of X is a collection of X.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And it's even stranger that InnerError is a different type... but https://github.com/microsoft/api-guidelines/blob/vNext/Guidelines.md#7102-error-condition-responses