-
Notifications
You must be signed in to change notification settings - Fork 870
Implement CborErrorResponseUnmarshaller with proper error type mapping #3947
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -21,6 +21,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||||
using System.Collections.Generic; | ||||||||||||||||||||||||||||||||||||||||||||||||
using System.Formats.Cbor; | ||||||||||||||||||||||||||||||||||||||||||||||||
using System.IO; | ||||||||||||||||||||||||||||||||||||||||||||||||
using System.Net; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
namespace Amazon.Extensions.CborProtocol.Internal.Transform | ||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -37,8 +38,75 @@ public class CborErrorResponseUnmarshaller : ICborUnmarshaller<ErrorResponse, Cb | |||||||||||||||||||||||||||||||||||||||||||||||
/// <returns>An <c>ErrorResponse</c> object.</returns> | ||||||||||||||||||||||||||||||||||||||||||||||||
public ErrorResponse Unmarshall(CborUnmarshallerContext context) | ||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||
// Placeholder until CBOR exception implementation. | ||||||||||||||||||||||||||||||||||||||||||||||||
return null; | ||||||||||||||||||||||||||||||||||||||||||||||||
var errorType = ErrorType.Unknown; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
if (context.ResponseData.StatusCode == HttpStatusCode.BadRequest) | ||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||
errorType = ErrorType.Sender; | ||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
else if (context.ResponseData.StatusCode == HttpStatusCode.InternalServerError) | ||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||
errorType = ErrorType.Receiver; | ||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
var response = new ErrorResponse | ||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||
Type = errorType, | ||||||||||||||||||||||||||||||||||||||||||||||||
StatusCode = context.ResponseData.StatusCode, | ||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
var reader = context.Reader; | ||||||||||||||||||||||||||||||||||||||||||||||||
reader.ReadStartMap(); | ||||||||||||||||||||||||||||||||||||||||||||||||
while (reader.PeekState() != CborReaderState.EndMap) | ||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||
string propertyName = reader.ReadTextString().ToLowerInvariant(); | ||||||||||||||||||||||||||||||||||||||||||||||||
switch (propertyName) | ||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||
case "__type": | ||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||
context.AddPathSegment("__type"); | ||||||||||||||||||||||||||||||||||||||||||||||||
var unmarshaller = CborStringUnmarshaller.Instance; | ||||||||||||||||||||||||||||||||||||||||||||||||
var type = unmarshaller.Unmarshall(context); | ||||||||||||||||||||||||||||||||||||||||||||||||
response.Code = SanitizeErrorType(type); | ||||||||||||||||||||||||||||||||||||||||||||||||
context.PopPathSegment(); | ||||||||||||||||||||||||||||||||||||||||||||||||
break; | ||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
case "message": | ||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||
context.AddPathSegment("message"); | ||||||||||||||||||||||||||||||||||||||||||||||||
var unmarshaller = CborStringUnmarshaller.Instance; | ||||||||||||||||||||||||||||||||||||||||||||||||
response.Message = unmarshaller.Unmarshall(context); | ||||||||||||||||||||||||||||||||||||||||||||||||
context.PopPathSegment(); | ||||||||||||||||||||||||||||||||||||||||||||||||
break; | ||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
default: | ||||||||||||||||||||||||||||||||||||||||||||||||
reader.SkipValue(); | ||||||||||||||||||||||||||||||||||||||||||||||||
break; | ||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
if (context.ResponseData.IsHeaderPresent(HeaderKeys.RequestIdHeader)) | ||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||
response.RequestId = context.ResponseData.GetHeaderValue(HeaderKeys.RequestIdHeader); | ||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
return response; | ||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
muhammad-othman marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||
/// <summary> | ||||||||||||||||||||||||||||||||||||||||||||||||
/// Extracts the error type from a Smithy shape identifier string. | ||||||||||||||||||||||||||||||||||||||||||||||||
/// The input is expected to be in the format "namespace#ErrorType[:additionalInfo]". | ||||||||||||||||||||||||||||||||||||||||||||||||
/// Returns the error type portion (e.g., "ErrorType"). | ||||||||||||||||||||||||||||||||||||||||||||||||
/// </summary> | ||||||||||||||||||||||||||||||||||||||||||||||||
private string SanitizeErrorType(string type) | ||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||
int start = type.IndexOf('#'); | ||||||||||||||||||||||||||||||||||||||||||||||||
start = start == -1 ? 0 : start + 1; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
int end = type.IndexOf(':', start); | ||||||||||||||||||||||||||||||||||||||||||||||||
end = end == -1 ? type.Length : end; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+102
to
+108
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potential ArgumentOutOfRangeException if 'type' parameter is null or if the calculated substring indices are invalid. The method should validate the input parameter and handle edge cases where start >= end.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't want to include these checks since this means we received a malformed response from the service. |
||||||||||||||||||||||||||||||||||||||||||||||||
return type.Substring(start, end - start).Trim(); | ||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
private static CborErrorResponseUnmarshaller instance; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
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.
The CBOR map reading logic doesn't call reader.ReadEndMap() after the while loop, which could leave the reader in an inconsistent state. Consider adding reader.ReadEndMap() after the while loop.
Copilot uses AI. Check for mistakes.
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.
I skipped that
ReadEndMap()
since in the next step we re-read the response anyway to generate the typed exception and reset the reader anyway and didn’t want to add an unnecessary step.