Skip to content

Commit

Permalink
Polish the code.
Browse files Browse the repository at this point in the history
Signed-off-by: Yury-Fridlyand <yury.fridlyand@improving.com>
  • Loading branch information
Yury-Fridlyand committed Mar 7, 2024
1 parent 70cc9d9 commit 88277c1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
20 changes: 14 additions & 6 deletions csharp/lib/AsyncClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,8 @@ private void FailureCallback(ulong index, ErrorType error_type, IntPtr ptr)
{
var error = ptr == IntPtr.Zero ? null : Marshal.PtrToStringAnsi(ptr);
// Work needs to be offloaded from the calling thread, because otherwise we might starve the client's thread pool.
_ = Task.Run(() =>
{
Console.Error.WriteLine($"FailureCallback : {error_type} : {error}");
var message = messageContainer.GetMessage((int)index);
message.SetException(new Exception($"{error_type} : {error}"));
});
_ = Task.Run(() => messageContainer.GetMessage((int)index)
.SetException(Errors.MakeException(error_type, error)));
}

~AsyncClient() => Dispose();
Expand Down Expand Up @@ -119,9 +115,21 @@ private void FailureCallback(ulong index, ErrorType error_type, IntPtr ptr)

internal enum ErrorType : uint
{
/// <summary>
/// Represented by <see cref="Errors.UnspecifiedException"/> for user
/// </summary>
Unspecified = 0,
/// <summary>
/// Represented by <see cref="Errors.ExecutionAbortedException"/> for user
/// </summary>
ExecAbort = 1,
/// <summary>
/// Represented by <see cref="TimeoutException"/> for user
/// </summary>
Timeout = 2,
/// <summary>
/// Represented by <see cref="Errors.DisconnectedException"/> for user
/// </summary>
Disconnect = 3,
}

Expand Down
33 changes: 33 additions & 0 deletions csharp/lib/Errors.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright GLIDE-for-Redis Project Contributors - SPDX Identifier: Apache-2.0
*/

using static Glide.AsyncClient;

namespace Glide;

public abstract class Errors
{
public sealed class UnspecifiedException : Exception
{
internal UnspecifiedException(string? message) : base(message) { }
}

public sealed class ExecutionAbortedException : Exception
{
internal ExecutionAbortedException(string? message) : base(message) { }
}

public sealed class DisconnectedException : Exception
{
internal DisconnectedException(string? message) : base(message) { }
}

internal static Exception MakeException(ErrorType type, string? message) => type switch
{
ErrorType.ExecAbort => new ExecutionAbortedException(message),
ErrorType.Disconnect => new DisconnectedException(message),
ErrorType.Timeout => new TimeoutException(message),
_ => new UnspecifiedException(message),
};
}

0 comments on commit 88277c1

Please sign in to comment.