Skip to content

Commit

Permalink
add ExecutionReplyException, ProgramCreationReplyException
Browse files Browse the repository at this point in the history
  • Loading branch information
vobradovich committed Nov 29, 2024
1 parent 39d0eab commit a9f1067
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 42 deletions.
21 changes: 9 additions & 12 deletions net/src/Sails.Remoting/Core/RemotingViaNodeClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public RemotingViaNodeClient(
cancellationToken),
extractResult: static (queuedMessageData, replyMessage) =>
{
EnsureSuccessOrThrowsReplyException(replyMessage.Details.Value.Code, replyMessage.Payload.Bytes);
EnsureSuccessOrThrowReplyException(replyMessage.Details.Value.Code, replyMessage.Payload.Bytes);
return (
(ActorId)queuedMessageData.Value[2],
replyMessage.Payload.Value.Value.Select(@byte => @byte.Value).ToArray()
Expand Down Expand Up @@ -141,7 +141,7 @@ public async Task<RemotingReply<byte[]>> MessageAsync(
cancellationToken),
extractResult: static (_, replyMessage) =>
{
EnsureSuccessOrThrowsReplyException(replyMessage.Details.Value.Code, replyMessage.Payload.Bytes);
EnsureSuccessOrThrowReplyException(replyMessage.Details.Value.Code, replyMessage.Payload.Bytes);
return replyMessage.Payload.Value.Value.Select(@byte => @byte.Value).ToArray();
},
cancellationToken)
Expand Down Expand Up @@ -172,7 +172,7 @@ public async Task<byte[]> QueryAsync(
cancellationToken)
.ConfigureAwait(false);

EnsureSuccessOrThrowsReplyException(replyInfo.Code, replyInfo.EncodedPayload);
EnsureSuccessOrThrowReplyException(replyInfo.Code, replyInfo.EncodedPayload);

return replyInfo.EncodedPayload;
}
Expand All @@ -196,15 +196,14 @@ private static MessageQueuedEventData SelectMessageQueuedEventData(IEnumerable<B
.SingleOrDefault()
?? throw new Exception("TODO: Custom exception. Something terrible happened.");

private static void EnsureSuccessOrThrowsReplyException(EnumReplyCode replyCode, byte[] payload)
private static void EnsureSuccessOrThrowReplyException(EnumReplyCode replyCode, byte[] payload)
{
if (replyCode.Value == ReplyCode.Success)
{
return;
}
var errorString = ParseErrorString(payload);
var (reason, executionError, programCreationError) = ParseReplyCode(replyCode);
throw new ReplyException(errorString, reason, executionError, programCreationError);
ThrowReplyException(replyCode, errorString);
}

private static string ParseErrorString(byte[] payload)
Expand All @@ -222,11 +221,9 @@ private static string ParseErrorString(byte[] payload)
return errorStr;
}

private static (ErrorReplyReason, SimpleExecutionError?, SimpleProgramCreationError?) ParseReplyCode(EnumReplyCode replyCode)
private static void ThrowReplyException(EnumReplyCode replyCode, string message)
{
var reason = ErrorReplyReason.Unsupported;
SimpleExecutionError? executionError = null;
SimpleProgramCreationError? programCreationError = null;
if (replyCode.Value == ReplyCode.Error)
{
var enumReason = (EnumErrorReplyReason)replyCode.Value2;
Expand All @@ -235,14 +232,14 @@ private static (ErrorReplyReason, SimpleExecutionError?, SimpleProgramCreationEr
if (reason == ErrorReplyReason.Execution)
{
var error = (EnumSimpleExecutionError)enumReason.Value2;
executionError = error.Value;
throw new ExecutionReplyException(message, reason, error);
}
if (reason == ErrorReplyReason.FailedToCreateProgram)
{
var error = (EnumSimpleProgramCreationError)enumReason.Value2;
programCreationError = error.Value;
throw new ProgramCreationReplyException(message, reason, error);
}
}
return (reason, executionError, programCreationError);
throw new ReplyException(message, reason);
}
}
49 changes: 49 additions & 0 deletions net/src/Sails.Remoting/Exceptions/ExecutionReplyException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using Substrate.Gear.Api.Generated.Model.gear_core_errors.simple;

namespace Sails.Remoting.Exceptions;

public class ExecutionReplyException : ReplyException
{
public SimpleExecutionError ExecutionError { get; } = SimpleExecutionError.Unsupported;

protected ExecutionReplyException()
{
}

protected ExecutionReplyException(string message)
: base(message)
{
}

public ExecutionReplyException(string message, ErrorReplyReason reason, SimpleExecutionError executionError)
: base(message, reason)
{
this.ExecutionError = executionError;
}

public ExecutionReplyException(
string message,
ErrorReplyReason reason,
SimpleExecutionError executionError,
Exception innerException)
: base(message, reason, innerException)
{
this.ExecutionError = executionError;
}

protected ExecutionReplyException(string message, ErrorReplyReason reason)
: base(message, reason)
{
}

protected ExecutionReplyException(string message, ErrorReplyReason reason, Exception innerException)
: base(message, reason, innerException)
{
}

protected ExecutionReplyException(string message, Exception innerException)
: base(message, innerException)
{
}
}
48 changes: 48 additions & 0 deletions net/src/Sails.Remoting/Exceptions/ProgramCreationReplyException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using Substrate.Gear.Api.Generated.Model.gear_core_errors.simple;

namespace Sails.Remoting.Exceptions;

public class ProgramCreationReplyException : ReplyException
{
public SimpleProgramCreationError ProgramCreationError { get; }

protected ProgramCreationReplyException()
{
}

protected ProgramCreationReplyException(string message) : base(message)
{
}

public ProgramCreationReplyException(string message, ErrorReplyReason reason, SimpleProgramCreationError programCreationError)
: base(message, reason)
{
this.ProgramCreationError = programCreationError;
}

public ProgramCreationReplyException(
string message,
ErrorReplyReason reason,
SimpleProgramCreationError programCreationError,
Exception innerException)
: base(message, reason, innerException)
{
this.ProgramCreationError = programCreationError;
}


protected ProgramCreationReplyException(string message, ErrorReplyReason reason) : base(message, reason)
{
}

protected ProgramCreationReplyException(string message, ErrorReplyReason reason, Exception innerException)
: base(message, reason, innerException)
{
}

protected ProgramCreationReplyException(string message, Exception innerException)
: base(message, innerException)
{
}
}
39 changes: 10 additions & 29 deletions net/src/Sails.Remoting/Exceptions/ReplyException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,31 @@ namespace Sails.Remoting.Exceptions;

public class ReplyException : Exception
{
public ErrorReplyReason Reason { get; }
public ErrorReplyReason Reason { get; } = ErrorReplyReason.Unsupported;

public SimpleExecutionError? ExecutionError { get; }

public SimpleProgramCreationError? ProgramCreationError { get; }

public ReplyException()
protected ReplyException()
{
}

public ReplyException(string message) : base(message)
protected ReplyException(string message)
: base(message)
{
}

public ReplyException(string message, ErrorReplyReason reason) : base(message)
public ReplyException(string message, ErrorReplyReason reason)
: base(message)
{
this.Reason = reason;
}

public ReplyException(
string message,
ErrorReplyReason reason,
SimpleExecutionError? executionError,
SimpleProgramCreationError? programCreationError) : base(message)
{
this.Reason = reason;
this.ExecutionError = executionError;
this.ProgramCreationError = programCreationError;
}


public ReplyException(
string message,
ErrorReplyReason reason,
SimpleExecutionError? executionError,
SimpleProgramCreationError? programCreationError,
Exception innerException) : base(message, innerException)
public ReplyException(string message, ErrorReplyReason reason, Exception innerException)
: base(message, innerException)
{
this.Reason = reason;
this.ExecutionError = executionError;
this.ProgramCreationError = programCreationError;
}

public ReplyException(string message, Exception innerException) : base(message, innerException)
protected ReplyException(string message, Exception innerException)
: base(message, innerException)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public async Task Program_Activation_Throws_NotEnoughGas()
CancellationToken.None);

// throws on ReadAsync
var ex = await Assert.ThrowsAsync<ReplyException>(() => activationReply.ReadAsync(CancellationToken.None));
var ex = await Assert.ThrowsAsync<ExecutionReplyException>(() => activationReply.ReadAsync(CancellationToken.None));

// Assert
ex.Should().BeEquivalentTo(new
Expand Down

0 comments on commit a9f1067

Please sign in to comment.