From 88277c18085c79217996b9968676e6d2cddfd5b7 Mon Sep 17 00:00:00 2001 From: Yury-Fridlyand Date: Wed, 6 Mar 2024 16:31:48 -0800 Subject: [PATCH] Polish the code. Signed-off-by: Yury-Fridlyand --- csharp/lib/AsyncClient.cs | 20 ++++++++++++++------ csharp/lib/Errors.cs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 6 deletions(-) create mode 100644 csharp/lib/Errors.cs diff --git a/csharp/lib/AsyncClient.cs b/csharp/lib/AsyncClient.cs index 4c3a71cd97..569af1a803 100644 --- a/csharp/lib/AsyncClient.cs +++ b/csharp/lib/AsyncClient.cs @@ -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(); @@ -119,9 +115,21 @@ private void FailureCallback(ulong index, ErrorType error_type, IntPtr ptr) internal enum ErrorType : uint { + /// + /// Represented by for user + /// Unspecified = 0, + /// + /// Represented by for user + /// ExecAbort = 1, + /// + /// Represented by for user + /// Timeout = 2, + /// + /// Represented by for user + /// Disconnect = 3, } diff --git a/csharp/lib/Errors.cs b/csharp/lib/Errors.cs new file mode 100644 index 0000000000..42fdd8723f --- /dev/null +++ b/csharp/lib/Errors.cs @@ -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), + }; +}