Skip to content

Commit

Permalink
[net]chore(exceptions): improve error reporting in Substrate.Gear.Cli…
Browse files Browse the repository at this point in the history
…ent (#731)
  • Loading branch information
DennisInSky authored Dec 12, 2024
1 parent b45e034 commit d59acc3
Show file tree
Hide file tree
Showing 21 changed files with 68 additions and 74 deletions.
13 changes: 12 additions & 1 deletion net/src/Sails.Net/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
global using CodeChangedEventData = Substrate.NetApi.Model.Types.Base.BaseTuple<
global using System;
global using System.Collections;
global using System.Collections.Generic;
global using System.Diagnostics.CodeAnalysis;
global using System.Linq;
global using System.Numerics;
global using System.Runtime.CompilerServices;
global using System.Threading;
global using System.Threading.Channels;
global using System.Threading.Tasks;
global using EnsureThat;
global using CodeChangedEventData = Substrate.NetApi.Model.Types.Base.BaseTuple<
Substrate.Gear.Api.Generated.Model.gprimitives.CodeId,
Substrate.Gear.Api.Generated.Model.gear_common.@event.EnumCodeChangeKind>;
global using CodeId = Substrate.Gear.Api.Generated.Model.gprimitives.CodeId;
Expand Down
12 changes: 2 additions & 10 deletions net/src/Substrate.Gear.Client/BlocksStream.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using EnsureThat;
using Substrate.Gear.Api.Generated;
using Substrate.Gear.Api.Generated;
using Substrate.NetApi;
using Substrate.NetApi.Model.Rpc;

Expand Down Expand Up @@ -71,7 +63,7 @@ public IAsyncEnumerable<Header> ReadAllHeadersAsync(CancellationToken cancellati
{
return Interlocked.CompareExchange(ref this.isReadInProgress, 1, 0) == 0
? ReadAllImpl(cancellationToken)
: throw new InvalidOperationException("TODO: Custom exception. Only one read operation is allowed at a time.");
: throw new InvalidOperationException("Only one read operation is allowed at a time.");

async IAsyncEnumerable<Header> ReadAllImpl([EnumeratorCancellation] CancellationToken cancellationToken)
{
Expand Down
6 changes: 1 addition & 5 deletions net/src/Substrate.Gear.Client/BlocksStreamExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using EnsureThat;
using Substrate.Gear.Api.Generated.Model.vara_runtime;
using Substrate.Gear.Api.Generated.Model.vara_runtime;
using Substrate.Gear.Client.NetApi.Model.Rpc;
using Substrate.Gear.Client.NetApi.Model.Types.Base;
using Substrate.NetApi.Model.Types.Base;
Expand Down
18 changes: 18 additions & 0 deletions net/src/Substrate.Gear.Client/Exceptions/GearException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Substrate.Gear.Client.Exceptions;

public class GearException : Exception
{
public GearException()
{
}

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

public GearException(string message, Exception innerException)
: base(message, innerException)
{
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;

namespace Substrate.Gear.Client.Extensions;
namespace Substrate.Gear.Client.Extensions;

public static class ReadOnlyMemoryExtensions
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Linq;
using EnsureThat;
using Substrate.Gear.Api.Generated.Model.gprimitives;
using Substrate.Gear.Api.Generated.Model.gprimitives;
using Substrate.NetApi;

namespace Substrate.Gear.Client.GearApi.Model.gprimitives;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Collections.Generic;
using EnsureThat;
using Substrate.Gear.Client.NetApi.Model.Types.Primitive;
using Substrate.Gear.Client.NetApi.Model.Types.Primitive;
using Substrate.NetApi;

namespace Substrate.Gear.Client.GearApi.Model.gprimitives;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Linq;
using EnsureThat;
using Substrate.NetApi;
using Substrate.NetApi;

namespace Substrate.Gear.Client.GearApi.Model.gprimitives;

Expand Down
13 changes: 12 additions & 1 deletion net/src/Substrate.Gear.Client/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
global using CodeChangedEventData = Substrate.NetApi.Model.Types.Base.BaseTuple<
global using System;
global using System.Collections;
global using System.Collections.Generic;
global using System.Diagnostics.CodeAnalysis;
global using System.Linq;
global using System.Numerics;
global using System.Runtime.CompilerServices;
global using System.Threading;
global using System.Threading.Channels;
global using System.Threading.Tasks;
global using EnsureThat;
global using CodeChangedEventData = Substrate.NetApi.Model.Types.Base.BaseTuple<
Substrate.Gear.Api.Generated.Model.gprimitives.CodeId,
Substrate.Gear.Api.Generated.Model.gear_common.@event.EnumCodeChangeKind>;
global using CodeId = Substrate.Gear.Api.Generated.Model.gprimitives.CodeId;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using EnsureThat;
using EnsureThat;
using Substrate.Gear.Client.Exceptions;
using Substrate.Gear.Client.NetApi.Model.Extrinsics;
using Substrate.Gear.Client.NetApi.Model.Types.Base;
using Substrate.NetApi.Model.Rpc;
Expand All @@ -22,7 +22,8 @@ public static uint GetExtrinsicIdxByHash(this Block block, Hash extrinsicHash)
EnsureArg.IsNotNull(extrinsicHash, nameof(extrinsicHash));

return block.FindExtrinsicIdxByHash(extrinsicHash)
?? throw new Exception("TODO: Custom exception.");
?? throw new GearException(
$"Block {block.Header.Number} doesn't contain extrinsic with hash {extrinsicHash}.");
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Linq;
using EnsureThat;
using Substrate.Gear.Client.NetApi.Model.Types.Base;
using Substrate.Gear.Client.NetApi.Model.Types.Base;
using Substrate.NetApi;
using Substrate.NetApi.Model.Rpc;
using Substrate.NetApi.Model.Types.Base;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json;
using Substrate.NetApi;
using Substrate.NetApi.Model.Types;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using EnsureThat;
using Substrate.NetApi.Model.Types;
using Substrate.NetApi.Model.Types;
using Substrate.NetApi.Model.Types.Base;

namespace Substrate.Gear.Client.NetApi.Model.Types.Base;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#nullable disable
using System;

using Substrate.NetApi.Model.Types.Base;

namespace Substrate.Gear.Client.NetApi.Model.Types.Base;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
#nullable disable

using System;
using System.Collections.Generic;
using System.Linq;

namespace Substrate.NetApi.Model.Types.Base;

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Linq;
using EnsureThat;
using Substrate.NetApi.Model.Types.Base;
using Substrate.NetApi.Model.Types.Base;

namespace Substrate.Gear.Client.NetApi.Model.Types.Base;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using Substrate.Gear.Client.NetApi.Model.Types.Primitive;
using Substrate.Gear.Client.NetApi.Model.Types.Primitive;
using Substrate.NetApi.Model.Types.Base;
using Substrate.NetApi.Model.Types.Primitive;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;

namespace Substrate.Gear.Client.NetApi.Model.Types.Base;
namespace Substrate.Gear.Client.NetApi.Model.Types.Base;

internal static class SpanExtensions
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#nullable disable
using System;

using Substrate.Gear.Api.Generated.Types.Base;
using Substrate.NetApi.Model.Types.Base;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Collections.Generic;
using EnsureThat;
using Substrate.NetApi.Model.Types.Primitive;
using Substrate.NetApi.Model.Types.Primitive;

namespace Substrate.Gear.Client.NetApi.Model.Types.Primitive;

Expand Down
15 changes: 6 additions & 9 deletions net/src/Substrate.Gear.Client/SubstrateClientExtExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Threading;
using System.Threading.Tasks;
using EnsureThat;
using Newtonsoft.Json;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Substrate.Gear.Api.Generated;
using Substrate.Gear.Api.Generated.Model.frame_system;
Expand All @@ -14,6 +7,7 @@
using Substrate.Gear.Api.Generated.Model.sp_runtime;
using Substrate.Gear.Api.Generated.Model.vara_runtime;
using Substrate.Gear.Api.Generated.Storage;
using Substrate.Gear.Client.Exceptions;
using Substrate.Gear.Client.GearApi.Model.gprimitives;
using Substrate.Gear.Client.NetApi.Model.Extrinsics;
using Substrate.Gear.Client.NetApi.Model.Rpc;
Expand Down Expand Up @@ -90,7 +84,10 @@ public static async Task<ExtrinsicInfo> ExecuteExtrinsicAsync(
case ExtrinsicState.Dropped:
case ExtrinsicState.Invalid:
default:
taskCompletionSource.SetException(new Exception("TODO: Custom exception."));
taskCompletionSource.SetException(
new GearException(
$"Execution of the '{method.ModuleName}:{method.CallName}' extrinsic ended up "
+ $"with unexpected state {extrinsicStatus.ExtrinsicState}."));
break;
}
},
Expand Down

0 comments on commit d59acc3

Please sign in to comment.