diff --git a/rust-bindings/src/lib.rs b/rust-bindings/src/lib.rs index 45ee29f0..016c0ace 100644 --- a/rust-bindings/src/lib.rs +++ b/rust-bindings/src/lib.rs @@ -209,6 +209,8 @@ pub enum FFIError { } impl FFIError { + /// The enumeration starts a 1 since return value 0 indicating a successfull + /// FFI call. fn to_int(&self) -> u16 { match self { FFIError::JsonError(_) => 1, diff --git a/src/Exceptions/InteropBindingException.cs b/src/Exceptions/InteropBindingException.cs deleted file mode 100644 index 310d287b..00000000 --- a/src/Exceptions/InteropBindingException.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System.Text; -using Concordium.Sdk.Interop; - -namespace Concordium.Sdk.Exceptions; - -/// -/// Thrown when a interop call failed with possible error as message. -/// -public sealed class InteropBindingException : Exception -{ - private const string EmptyErrorMessage = "Empty error message returned"; - /// - /// Type of error - /// - public Result Result { get; } - - internal static InteropBindingException Create(Result result, byte[]? message) => - message != null ? new InteropBindingException(result, Encoding.UTF8.GetString(message)) : Empty(result); - - private InteropBindingException(Result result, string message) : base(message) => this.Result = result; - - private static InteropBindingException Empty(Result result) => new(result, EmptyErrorMessage); -} diff --git a/src/Exceptions/SchemaJsonException.cs b/src/Exceptions/SchemaJsonException.cs new file mode 100644 index 00000000..5c51cf71 --- /dev/null +++ b/src/Exceptions/SchemaJsonException.cs @@ -0,0 +1,23 @@ +using System.Text; +using Concordium.Sdk.Interop; + +namespace Concordium.Sdk.Exceptions; + +/// +/// Thrown when a interop call failed with possible error as message. +/// +public sealed class SchemaJsonException : Exception +{ + private const string EmptyErrorMessage = "Empty error message returned"; + /// + /// Type of error + /// + public SchemaJsonResult SchemaJsonResult { get; } + + internal static SchemaJsonException Create(SchemaJsonResult schemaJsonResult, byte[]? message) => + message != null ? new SchemaJsonException(schemaJsonResult, Encoding.UTF8.GetString(message)) : Empty(schemaJsonResult); + + private SchemaJsonException(SchemaJsonResult schemaJsonResult, string message) : base(message) => this.SchemaJsonResult = schemaJsonResult; + + private static SchemaJsonException Empty(SchemaJsonResult schemaJsonResult) => new(schemaJsonResult, EmptyErrorMessage); +} diff --git a/src/Interop/InteropBinding.cs b/src/Interop/InteropBinding.cs index b0a3a810..54f02d39 100644 --- a/src/Interop/InteropBinding.cs +++ b/src/Interop/InteropBinding.cs @@ -15,14 +15,14 @@ internal static class InteropBinding private const string DllName = "rust_bindings"; [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "schema_display")] - private static extern Result SchemaDisplay( + private static extern SchemaJsonResult SchemaDisplay( [MarshalAs(UnmanagedType.LPArray)] byte[] schema, int schema_size, FfiByteOption schema_version, [MarshalAs(UnmanagedType.FunctionPtr)] SetResultCallback callback); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "get_receive_contract_parameter")] - private static extern Result GetReceiveContractParameter( + private static extern SchemaJsonResult GetReceiveContractParameter( [MarshalAs(UnmanagedType.LPArray)] byte[] schema, int schema_size, FfiByteOption schema_version, [MarshalAs(UnmanagedType.LPUTF8Str)] string contract_name, @@ -32,7 +32,7 @@ private static extern Result GetReceiveContractParameter( [MarshalAs(UnmanagedType.FunctionPtr)] SetResultCallback callback); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "get_event_contract")] - private static extern Result GetEventContract( + private static extern SchemaJsonResult GetEventContract( [MarshalAs(UnmanagedType.LPArray)] byte[] schema, int schema_size, FfiByteOption schema_version, @@ -68,7 +68,7 @@ internal static Utf8Json SchemaDisplay(VersionedModuleSchema schema) return new Utf8Json(result); } - var interopException = InteropBindingException.Create(schemaDisplay, result); + var interopException = SchemaJsonException.Create(schemaDisplay, result); throw interopException; } @@ -102,7 +102,7 @@ internal static Utf8Json GetReceiveContractParameter(VersionedModuleSchema schem return new Utf8Json(result); } - var interopException = InteropBindingException.Create(receiveContractParameter, result); + var interopException = SchemaJsonException.Create(receiveContractParameter, result); throw interopException; } @@ -132,7 +132,7 @@ internal static Utf8Json GetEventContract(VersionedModuleSchema schema, Contract return new Utf8Json(result); } - var interopException = InteropBindingException.Create(schemaDisplay, result); + var interopException = SchemaJsonException.Create(schemaDisplay, result); throw interopException; } diff --git a/src/Interop/Result.cs b/src/Interop/SchemaJsonResult.cs similarity index 95% rename from src/Interop/Result.cs rename to src/Interop/SchemaJsonResult.cs index f5f9d8a8..1a399b6d 100644 --- a/src/Interop/Result.cs +++ b/src/Interop/SchemaJsonResult.cs @@ -3,7 +3,7 @@ namespace Concordium.Sdk.Interop; /// /// Result type which on errors hold error type information. /// -public enum Result +public enum SchemaJsonResult { /// /// No error @@ -87,5 +87,5 @@ public enum Result internal static class ErrorExtensions { - internal static bool IsError(this Result result) => result != Result.NoError; + internal static bool IsError(this SchemaJsonResult schemaJsonResult) => schemaJsonResult != SchemaJsonResult.NoError; } diff --git a/src/Types/ContractEvent.cs b/src/Types/ContractEvent.cs index d75002c2..dfdac385 100644 --- a/src/Types/ContractEvent.cs +++ b/src/Types/ContractEvent.cs @@ -19,7 +19,7 @@ public sealed record ContractEvent(byte[] Bytes) /// Module schema in hexadecimal. /// Contract name. /// deserialized as json uft8 encoded. - /// Thrown when event wasn't able to be deserialized from schema. + /// Thrown when event wasn't able to be deserialized from schema. public Utf8Json GetDeserializeEvent( VersionedModuleSchema schema, ContractIdentifier contractName diff --git a/src/Types/ContractInitializedEvent.cs b/src/Types/ContractInitializedEvent.cs index c9531c49..8bce0a6b 100644 --- a/src/Types/ContractInitializedEvent.cs +++ b/src/Types/ContractInitializedEvent.cs @@ -45,7 +45,7 @@ internal static ContractInitializedEvent From(Grpc.V2.ContractInitializedEvent i /// /// Module schema in hexadecimal. /// List of deserialized json uft8 encoded events. Possible null if this was returned from deserialization. - /// Thrown if an event wasn't able to be deserialized from schema. + /// Thrown if an event wasn't able to be deserialized from schema. public IList GetDeserializedEvents(VersionedModuleSchema schema) { var deserialized = new List(this.Events.Count); diff --git a/src/Types/ContractTraceElement.cs b/src/Types/ContractTraceElement.cs index f99df036..730d2c5d 100644 --- a/src/Types/ContractTraceElement.cs +++ b/src/Types/ContractTraceElement.cs @@ -79,7 +79,7 @@ public sealed record Updated( /// /// Versioned module schema. /// deserialized as json uft8 encoded. - /// Thrown when message wasn't able to be deserialized form schema. + /// Thrown when message wasn't able to be deserialized form schema. public Utf8Json GetDeserializeMessage(VersionedModuleSchema schema) => GetDeserializeMessage(schema, this.ReceiveName.GetContractName(), this.ReceiveName.GetEntrypoint(), this.Message); @@ -91,7 +91,7 @@ public Utf8Json GetDeserializeMessage(VersionedModuleSchema schema) => /// Entrypoint on contract. /// Message to entrypoint. /// deserialized as json uft8 encoded. - /// Thrown when message wasn't able to be deserialized from schema. + /// Thrown when message wasn't able to be deserialized from schema. public static Utf8Json GetDeserializeMessage( VersionedModuleSchema schema, ContractIdentifier contractIdentifier, @@ -105,7 +105,7 @@ Parameter message /// /// Module schema. /// List of deserialized json uft8 encoded events. Possible null if this was returned from deserialization. - /// Thrown if an event wasn't able to be deserialized from schema. + /// Thrown if an event wasn't able to be deserialized from schema. public IList GetDeserializedEvents(VersionedModuleSchema schema) { var deserialized = new List(this.Events.Count); @@ -141,7 +141,7 @@ public sealed record Interrupted(ContractAddress Address, IList E /// Module schema. /// Contract name. /// List of deserialized json uft8 encoded events. Possible null if this was returned from deserialization. - /// Thrown if an event wasn't able to be deserialized from schema. + /// Thrown if an event wasn't able to be deserialized from schema. public IList GetDeserializedEvents(VersionedModuleSchema schema, ContractIdentifier contractName) { var deserialized = new List(this.Events.Count); diff --git a/src/Types/RejectReason.cs b/src/Types/RejectReason.cs index f709dbd8..837bc7d2 100644 --- a/src/Types/RejectReason.cs +++ b/src/Types/RejectReason.cs @@ -194,7 +194,7 @@ public sealed record RejectedReceive(int RejectReason, ContractAddress ContractA /// /// Versioned module schema. /// deserialized as json uft8 encoded. - /// Thrown when message wasn't able to be deserialized using the schema. + /// Thrown when message wasn't able to be deserialized using the schema. public Utf8Json GetDeserializeMessage(VersionedModuleSchema schema) => Updated.GetDeserializeMessage(schema, this.ReceiveName.GetContractName(), this.ReceiveName.GetEntrypoint(), this.Parameter); } diff --git a/src/Types/VersionedModuleSchema.cs b/src/Types/VersionedModuleSchema.cs index 40a4d18b..767cd8cf 100644 --- a/src/Types/VersionedModuleSchema.cs +++ b/src/Types/VersionedModuleSchema.cs @@ -21,6 +21,6 @@ public sealed record VersionedModuleSchema(byte[] Schema, ModuleSchemaVersion Ve /// Deserialize schema. /// /// Schema as json uft8 encoded. - /// Thrown when schema wasn't able to be deserialized. + /// Thrown when schema wasn't able to be deserialized. public Utf8Json GetDeserializedSchema() => InteropBinding.SchemaDisplay(this); }; diff --git a/tests/UnitTests/Interop/InteropBindingTests.cs b/tests/UnitTests/Interop/InteropBindingTests.cs index d96a3d17..492b719e 100644 --- a/tests/UnitTests/Interop/InteropBindingTests.cs +++ b/tests/UnitTests/Interop/InteropBindingTests.cs @@ -56,9 +56,9 @@ public async Task GivenBadSchema_WhenSchemaDisplay_ThenThrowExceptionWithParseEr var action = () => InteropBinding.SchemaDisplay(versionedModuleSchema); // Assert - action.Should().Throw() + action.Should().Throw() .Where(e => - e.Result == Result.VersionedSchemaErrorParseError && + e.SchemaJsonResult == SchemaJsonResult.VersionedSchemaErrorParseError && e.Message.Equals("Parse error", StringComparison.Ordinal)); } @@ -101,9 +101,9 @@ public async Task GivenBadReceiveParam_WhenDisplayReceiveParam_ThenThrowExceptio var action = () => InteropBinding.GetReceiveContractParameter(versionedModuleSchema, contractIdentifier, entryPoint, parameter); // Assert - action.Should().Throw() + action.Should().Throw() .Where(e => - e.Result == Result.JsonError && + e.SchemaJsonResult == SchemaJsonResult.JsonError && e.Message.StartsWith("Failed to deserialize AccountAddress due to: Could not parse AccountAddress", StringComparison.InvariantCulture)); } @@ -123,9 +123,9 @@ public async Task GivenBadContractIdentifier_WhenDisplayReceiveParam_ThenThrowEx var action = () => InteropBinding.GetReceiveContractParameter(versionedModuleSchema, contractIdentifier, entryPoint, parameter); // Assert - action.Should().Throw() + action.Should().Throw() .Where(e => - e.Result == Result.VersionedSchemaErrorNoContractInModule && + e.SchemaJsonResult == SchemaJsonResult.VersionedSchemaErrorNoContractInModule && e.Message.Equals("Unable to find contract schema in module schema", StringComparison.Ordinal)); } @@ -145,9 +145,9 @@ public async Task GivenBadEntrypoint_WhenDisplayReceiveParam_ThenThrowException( var action = () => InteropBinding.GetReceiveContractParameter(versionedModuleSchema, contractIdentifier, entryPoint, parameter); // Assert - action.Should().Throw() + action.Should().Throw() .Where(e => - e.Result == Result.VersionedSchemaErrorNoReceiveInContract && + e.SchemaJsonResult == SchemaJsonResult.VersionedSchemaErrorNoReceiveInContract && e.Message.Equals("Receive function schema not found in contract schema", StringComparison.Ordinal)); } @@ -167,9 +167,9 @@ public async Task GivenBadSchema_WhenDisplayReceiveParam_ThenThrowException() var action = () => InteropBinding.GetReceiveContractParameter(versionedModuleSchema, contractIdentifier, entryPoint, parameter); // Assert - action.Should().Throw() + action.Should().Throw() .Where(e => - e.Result == Result.VersionedSchemaErrorMissingSchemaVersion && + e.SchemaJsonResult == SchemaJsonResult.VersionedSchemaErrorMissingSchemaVersion && e.Message.Equals("Missing Schema Version", StringComparison.Ordinal)); } @@ -208,9 +208,9 @@ public async Task GivenBadSchema_WhenDisplayEvent_ThenThrowException() var action = () => InteropBinding.GetEventContract(versionedModuleSchema, contractIdentifier, contractEvent); // Assert - action.Should().Throw() + action.Should().Throw() .Where(e => - e.Result == Result.VersionedSchemaErrorMissingSchemaVersion && + e.SchemaJsonResult == SchemaJsonResult.VersionedSchemaErrorMissingSchemaVersion && e.Message.Equals("Missing Schema Version", StringComparison.Ordinal)); } @@ -229,9 +229,9 @@ public async Task GivenBadContractIdentifier_WhenDisplayEvent_ThenThrowException var action = () => InteropBinding.GetEventContract(versionedModuleSchema, contractIdentifier, contractEvent); // Assert - action.Should().Throw() + action.Should().Throw() .Where(e => - e.Result == Result.VersionedSchemaErrorNoContractInModule && + e.SchemaJsonResult == SchemaJsonResult.VersionedSchemaErrorNoContractInModule && e.Message.Equals("Unable to find contract schema in module schema", StringComparison.Ordinal)); } @@ -250,9 +250,9 @@ public async Task GivenBadContractEvent_WhenDisplayEvent_ThenThrowException() var action = () => InteropBinding.GetEventContract(versionedModuleSchema, contractIdentifier, contractEvent); // Assert - action.Should().Throw() + action.Should().Throw() .Where(e => - e.Result == Result.JsonError && + e.SchemaJsonResult == SchemaJsonResult.JsonError && e.Message.StartsWith("Failed to deserialize AccountAddress due to: Could not parse")); }