-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(dotnet): broken error name deserialization (#3855)
Fixes a bug where the `name` field of an `ErrorResponse` would not deserialize into our enum of known names correctly. Initially it was thought that if the `name` field was non-null, it would only be one of our known values, but the kernel is in fact not setting this field explicitly in all cases of "user land" errors. This results in the `name` field being `"Error"`in the default case and would cause a JSON deserialization error. Implements a custom deserializer for the `ErrorName` to explicitly return null if the string is not defined or unknown. Additionally makes the `name` field nullable. Fixes aws/aws-cdk#22369 Co-authored-by: 🧑🏻💻 Romain Marcadier <rmuller@amazon.com> Co-authored-by: Romain Marcadier <rmuller@amazon.fr>
- Loading branch information
1 parent
880dbcc
commit be008d4
Showing
28 changed files
with
906 additions
and
40 deletions.
There are no files selected for viewing
This file contains 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
Empty file.
This file contains 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 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 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 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
73 changes: 73 additions & 0 deletions
73
packages/@jsii/dotnet-runtime/src/Amazon.JSII.Runtime/JsonModel/Api/NamedError.cs
This file contains 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,73 @@ | ||
using Amazon.JSII.Runtime; | ||
using Newtonsoft.Json; | ||
using System; | ||
|
||
namespace Amazon.JSII.JsonModel.Api | ||
{ | ||
internal sealed class NamedError | ||
{ | ||
internal NamedError(Exception exception): this(exception.ToString(), exception is JsiiError ? ErrorName.JsiiError : (ErrorName?)null) {} | ||
|
||
internal NamedError(string message, ErrorName? name) | ||
{ | ||
Message = message; | ||
Name = name; | ||
} | ||
|
||
public string Message { get; } | ||
public ErrorName? Name { get;} | ||
} | ||
|
||
[JsonConverter(typeof(ErrorNameConverter))] | ||
public enum ErrorName | ||
{ | ||
JsiiError, | ||
RuntimeException, | ||
} | ||
|
||
internal sealed class ErrorNameConverter : JsonConverter | ||
{ | ||
private const string ErrorNameFault = "@jsii/kernel.Fault"; | ||
private const string ErrorNameRuntime = "@jsii/kernel.RuntimeError"; | ||
|
||
public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer) | ||
{ | ||
string? enumString = (string?)reader.Value; | ||
ErrorName? errorResponseName = null; | ||
switch (enumString) { | ||
case ErrorNameFault: | ||
{ | ||
errorResponseName = ErrorName.JsiiError; | ||
break; | ||
} | ||
case ErrorNameRuntime: | ||
{ | ||
errorResponseName = ErrorName.RuntimeException; | ||
break; | ||
} | ||
} | ||
|
||
return errorResponseName; | ||
} | ||
|
||
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer) | ||
{ | ||
switch (value) | ||
{ | ||
case ErrorName.JsiiError: | ||
writer.WriteValue(ErrorNameFault); | ||
break; | ||
case ErrorName.RuntimeException: | ||
writer.WriteValue(ErrorNameRuntime); | ||
break; | ||
default: writer.WriteNull(); | ||
break; | ||
} | ||
} | ||
|
||
public override bool CanConvert(Type objectType) | ||
{ | ||
return objectType == typeof(string); | ||
} | ||
} | ||
} |
This file contains 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 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 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 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 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 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 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.