Skip to content

Commit

Permalink
chore(net): add custom exceptions (#718)
Browse files Browse the repository at this point in the history
  • Loading branch information
vobradovich authored Dec 9, 2024
1 parent b2a6cb1 commit a80cd16
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 11 deletions.
10 changes: 5 additions & 5 deletions net/src/Sails.Remoting/Core/RemotingViaNodeClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ public RemotingViaNodeClient(
createProgram,
DefaultExtrinsicTtlInBlocks,
selectResultOnSuccess: SelectMessageQueuedEventData,
selectResultOnError: static (_) =>
throw new Exception("TODO: Custom exception. Unable to create program."),
selectResultOnError: static (error) =>
throw new ExtrinsicDispatchException("Unable to create program", error),
cancellationToken),
extractResult: static (queuedMessageData, replyMessage) =>
{
Expand Down Expand Up @@ -135,8 +135,8 @@ public async Task<RemotingReply<byte[]>> MessageAsync(
sendMessage,
DefaultExtrinsicTtlInBlocks,
selectResultOnSuccess: SelectMessageQueuedEventData,
selectResultOnError: static (_) =>
throw new Exception("TODO: Custom exception. Unable to send message."),
selectResultOnError: static (error) =>
throw new ExtrinsicDispatchException("Unable to send message", error),
cancellationToken),
extractResult: static (_, replyMessage) =>
{
Expand Down Expand Up @@ -194,7 +194,7 @@ private static MessageQueuedEventData SelectMessageQueuedEventData(IEnumerable<B
GearEvent.MessageQueued,
(MessageQueuedEventData data) => data)
.SingleOrDefault()
?? throw new Exception("TODO: Custom exception. Something terrible happened.");
?? throw new ExtrinsicDispatchException("Something terrible happened - MessageQueued event not found");

private static void EnsureSuccessOrThrowReplyException(EnumReplyCode replyCode, byte[] payload)
{
Expand Down
2 changes: 1 addition & 1 deletion net/src/Sails.Remoting/DelegatingReply.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Sails.Remoting;

public sealed class DelegatingReply<TResult, T>(RemotingReply<TResult> innerReply, Func<TResult, T> map) : IReply<T>
internal sealed class DelegatingReply<TResult, T>(RemotingReply<TResult> innerReply, Func<TResult, T> map) : IReply<T>
where T : IType, new()
{
/// <inheritdoc />
Expand Down
36 changes: 36 additions & 0 deletions net/src/Sails.Remoting/Exceptions/ExtrinsicDispatchException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using Substrate.Gear.Api.Generated.Model.frame_support.dispatch;
using Substrate.Gear.Api.Generated.Model.sp_runtime;

namespace Sails.Remoting.Exceptions;

public class ExtrinsicDispatchException : SailsException
{
public EnumDispatchError? DispatchError { get; }
public DispatchInfo? DispatchInfo { get; }

public ExtrinsicDispatchException(string message, ExtrinsicFailedEventData? eventData) : base(message)
{
this.DispatchError = eventData?.Value[0] as EnumDispatchError;
this.DispatchInfo = eventData?.Value[1] as DispatchInfo;
}

public ExtrinsicDispatchException(string message, EnumDispatchError? dispatchError, DispatchInfo? dispatchInfo)
: base(message)
{
this.DispatchError = dispatchError;
this.DispatchInfo = dispatchInfo;
}

public ExtrinsicDispatchException() : base()
{
}

public ExtrinsicDispatchException(string message) : base(message)
{
}

public ExtrinsicDispatchException(string message, Exception innerException) : base(message, innerException)
{
}
}
2 changes: 1 addition & 1 deletion net/src/Sails.Remoting/Exceptions/ReplyException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Sails.Remoting.Exceptions;

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

Expand Down
18 changes: 18 additions & 0 deletions net/src/Sails.Remoting/Exceptions/SailsException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;

namespace Sails.Remoting.Exceptions;

public class SailsException : Exception
{
public SailsException() : base()
{
}

public SailsException(string message) : base(message)
{
}

public SailsException(string message, Exception innerException) : base(message, innerException)
{
}
}
3 changes: 3 additions & 0 deletions net/src/Sails.Remoting/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
global using EnumGearEvent = Substrate.Gear.Api.Generated.Model.pallet_gear.pallet.EnumEvent;
global using ExtrinsicFailedEventData = Substrate.NetApi.Model.Types.Base.BaseTuple<
Substrate.Gear.Api.Generated.Model.sp_runtime.EnumDispatchError,
Substrate.Gear.Api.Generated.Model.frame_support.dispatch.DispatchInfo>;
global using GasUnit = Substrate.NetApi.Model.Types.Primitive.U64;
global using GearEvent = Substrate.Gear.Api.Generated.Model.pallet_gear.pallet.Event;
global using MessageQueuedEventData = Substrate.NetApi.Model.Types.Base.BaseTuple<
Expand Down
7 changes: 3 additions & 4 deletions net/src/Sails.Remoting/RemotingAction.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using EnsureThat;
using Sails.Remoting.Abstractions;
using Sails.Remoting.Abstractions.Core;
using Sails.Remoting.Exceptions;
using Substrate.Gear.Api.Generated.Model.gprimitives;
using Substrate.NetApi.Model.Types;
using Substrate.NetApi.Model.Types.Primitive;
Expand Down Expand Up @@ -128,8 +128,7 @@ private static void EnsureRoute(byte[] bytes, ref int p, params string[] routes)
str.Decode(bytes, ref p);
if (str != route)
{
// TODO: custom invalid route exception
throw new ArgumentException();
throw new SailsException("Reply route mismatches");
}
}
}
Expand Down

0 comments on commit a80cd16

Please sign in to comment.