From f2a3101466043f0c75c2a97e571788ac83d53452 Mon Sep 17 00:00:00 2001 From: hjhun <36876573+hjhun@users.noreply.github.com> Date: Wed, 25 Sep 2024 16:23:53 +0900 Subject: [PATCH] [Core] Modify exceptions (#6355) * [Core] Modify exceptions Some methods should throw InvalidOperationException. This patch modifies that. Signed-off-by: Hwankyu Jhun * [Core] Add a missing exception to ChannelReceiver.Receive() Signed-off-by: Hwankyu Jhun * [Core] Fix ambiguous error messages Signed-off-by: Hwankyu Jhun * [Core] Fix wrong description & implementation Signed-off-by: Hwankyu Jhun * [Core] Modify ambiguous log message Signed-off-by: Hwankyu Jhun * [Core] Convert error to C# exception Signed-off-by: Hwankyu Jhun * [Core] Fix wrong log message Signed-off-by: Hwankyu Jhun * [Core] Fix wrong error handling Signed-off-by: Hwankyu Jhun --------- Signed-off-by: Hwankyu Jhun --- src/Tizen.Core/Tizen.Core/ChannelReceiver.cs | 11 +++- src/Tizen.Core/Tizen.Core/ChannelSender.cs | 18 ++++- src/Tizen.Core/Tizen.Core/Task.cs | 69 ++++++++++++++++---- 3 files changed, 84 insertions(+), 14 deletions(-) diff --git a/src/Tizen.Core/Tizen.Core/ChannelReceiver.cs b/src/Tizen.Core/Tizen.Core/ChannelReceiver.cs index c8169954131..c1c62d435be 100644 --- a/src/Tizen.Core/Tizen.Core/ChannelReceiver.cs +++ b/src/Tizen.Core/Tizen.Core/ChannelReceiver.cs @@ -66,6 +66,8 @@ internal ChannelReceiver(IntPtr handle) /// Receives the channel object from the sender asynchronously. /// /// The received channel object. + /// Thrown when out of memory. + /// Thrown when failed because of an invalid operation. /// /// /// @@ -84,7 +86,14 @@ public async Task Receive() return await System.Threading.Tasks.Task.Run(() => { Interop.LibTizenCore.ErrorCode error = Interop.LibTizenCore.TizenCoreChannel.ReceiverReceive(Handle, out IntPtr channelObject); - TCoreErrorFactory.CheckAndThrownException(error, "Failed to receive channel object"); + if (error != Interop.LibTizenCore.ErrorCode.None) + { + if (error == Interop.LibTizenCore.ErrorCode.InvalidParameter) + { + error = Interop.LibTizenCore.ErrorCode.InvalidContext; + } + TCoreErrorFactory.CheckAndThrownException(error, "Failed to receive channel object"); + } return new ChannelObject(channelObject); }).ConfigureAwait(false); } diff --git a/src/Tizen.Core/Tizen.Core/ChannelSender.cs b/src/Tizen.Core/Tizen.Core/ChannelSender.cs index 45be966a802..ae170bac46d 100644 --- a/src/Tizen.Core/Tizen.Core/ChannelSender.cs +++ b/src/Tizen.Core/Tizen.Core/ChannelSender.cs @@ -44,6 +44,8 @@ internal ChannelSender(IntPtr handle) /// /// The channel object instance. /// Thrown when the argument is null. + /// Thrown when the argument is invalid. + /// Thrown when failed because of an invalid operation. /// /// It's important to call the Dispose() method on the passed channel object to release resources. /// @@ -68,8 +70,16 @@ public void Send(ChannelObject channelObject) throw new ArgumentNullException(nameof(channelObject)); } + if (channelObject.Handle == IntPtr.Zero) + { + throw new ArgumentException("Invalid argument"); + } + Interop.LibTizenCore.ErrorCode error = Interop.LibTizenCore.TizenCoreChannel.SenderSend(Handle, channelObject.Handle); - TCoreErrorFactory.CheckAndThrownException(error, "Failed to send channel object"); + if (error != Interop.LibTizenCore.ErrorCode.None) + { + throw new InvalidOperationException("Failed to send channel object"); + } channelObject.IsUsed = true; } @@ -77,7 +87,7 @@ public void Send(ChannelObject channelObject) /// Creates and returns a copy of the channel sender object. /// /// A newly created channel sender instance. - /// Thrown when the argument is invalid. + /// Thrown when failed because of an invalid operation. /// Thrown when out of memory. /// /// @@ -92,6 +102,10 @@ public void Send(ChannelObject channelObject) public ChannelSender Clone() { Interop.LibTizenCore.ErrorCode error = Interop.LibTizenCore.TizenCoreChannel.SenderClone(Handle, out IntPtr clonedHandle); + if (error == Interop.LibTizenCore.ErrorCode.InvalidParameter) + { + error = Interop.LibTizenCore.ErrorCode.InvalidContext; + } TCoreErrorFactory.CheckAndThrownException(error, "Failed to clone channel sender"); return new ChannelSender(clonedHandle); diff --git a/src/Tizen.Core/Tizen.Core/Task.cs b/src/Tizen.Core/Tizen.Core/Task.cs index a0bbf61fb74..4ca4cc81dd4 100644 --- a/src/Tizen.Core/Tizen.Core/Task.cs +++ b/src/Tizen.Core/Tizen.Core/Task.cs @@ -93,7 +93,7 @@ internal Task(IntPtr handle) /// /// The action callback to post. /// Thrown when the action argument is null. - /// Thrown when failed because of the instance is invalid. + /// Thrown when failed because of an invalid operation. /// Thrown when out of memory. /// /// The action callback will be executed by the main loop of the task. @@ -127,7 +127,14 @@ public void Post(Action action) } _actionkMap[id] = action; Interop.LibTizenCore.ErrorCode error = Interop.LibTizenCore.TizenCore.AddIdleJob(_handle, NativeActionCallback, (IntPtr)id, out IntPtr handle); - TCoreErrorFactory.CheckAndThrownException(error, "Failed to add idle job"); + if (error != Interop.LibTizenCore.ErrorCode.None) + { + if (error == Interop.LibTizenCore.ErrorCode.InvalidParameter) + { + error = Interop.LibTizenCore.ErrorCode.InvalidContext; + } + TCoreErrorFactory.CheckAndThrownException(error, "Failed to add idle job"); + } } /// @@ -135,7 +142,7 @@ public void Post(Action action) /// /// The task to post. /// Thrown when the task argument is null. - /// Thrown when failed because of the instance is invalid. + /// Thrown when failed because of an invalid operation. /// Thrown when out of memory. /// /// The task will be stored in the internal map using its unique identifier. @@ -178,6 +185,10 @@ public void Post(Func task) if (error != Interop.LibTizenCore.ErrorCode.None) { _taskMap.TryRemove(id, out var _); + if (error == Interop.LibTizenCore.ErrorCode.InvalidParameter) + { + error = Interop.LibTizenCore.ErrorCode.InvalidContext; + } TCoreErrorFactory.CheckAndThrownException(error, "Failed to add idle job"); } } @@ -189,7 +200,7 @@ public void Post(Func task) /// The recurring timer callback function which returns whether or not to continue triggering the timer. /// The registered timer ID to be used with . /// Thrown when the callback argument is null. - /// Thrown when failed because of the instance is invalid. + /// Thrown when failed because of an invalid operation. /// Thrown when out of memory. /// /// The callback function will be called every time the specified interval elapses. It should return true to keep the timer running, otherwise the timer will be stopped. @@ -227,7 +238,7 @@ public int AddTimer(uint interval, Func callback) if (error != Interop.LibTizenCore.ErrorCode.None) { _timerMap.TryRemove(id, out var _); - TCoreErrorFactory.CheckAndThrownException(error, "Failed to add a timer"); + throw new InvalidOperationException("Failed to add timer"); } timerSource.Handle = handle; @@ -275,6 +286,7 @@ public void RemoveTimer(int id) /// The channel receiver instance. /// Thrown when the argument is null. /// Thrown when the argument is invalid. + /// Thrown when failed because of an invalid operation. /// Thrown when out of memory. /// /// @@ -296,7 +308,12 @@ public void AddChannelReceiver(ChannelReceiver receiver) if (receiver.Handle == IntPtr.Zero) { - throw new ArgumentException("The receiver is already added"); + if (receiver.Source != IntPtr.Zero) + { + throw new ArgumentException("The receiver is already added"); + } + + throw new ArgumentException("Invalid argument"); } int id; @@ -313,6 +330,10 @@ public void AddChannelReceiver(ChannelReceiver receiver) if (error != Interop.LibTizenCore.ErrorCode.None) { _channelMap.TryRemove(id, out var _); + if (error == Interop.LibTizenCore.ErrorCode.InvalidParameter) + { + error = Interop.LibTizenCore.ErrorCode.InvalidContext; + } TCoreErrorFactory.CheckAndThrownException(error, "Failed to add a channel to the task"); } @@ -376,6 +397,7 @@ public void RemoveChannelReceiver(ChannelReceiver receiver) /// The event instance. /// Thrown when the argument is null. /// Thrown when the argument is invalid. + /// Thrown when failed because of an invalid operation. /// Thrown when out of memory. /// /// This method allows you to associate an event with a specific task. By adding an event to a task's main loop, other threads can utilize this event to communicate with the task. @@ -403,6 +425,11 @@ public void AddEvent(Event coreEvent) throw new ArgumentNullException(nameof(coreEvent)); } + if (coreEvent.Handle == IntPtr.Zero) + { + throw new ArgumentException("Invalid argument"); + } + if (coreEvent.Source != IntPtr.Zero) { throw new ArgumentException("The event is already added"); @@ -425,6 +452,10 @@ public void AddEvent(Event coreEvent) if (error != Interop.LibTizenCore.ErrorCode.None) { _eventMap.TryRemove(id, out var _); + if (error == Interop.LibTizenCore.ErrorCode.InvalidParameter) + { + error = Interop.LibTizenCore.ErrorCode.InvalidContext; + } TCoreErrorFactory.CheckAndThrownException(error, "Failed to add an event to the task"); } @@ -486,6 +517,7 @@ public void RemoveEvent(Event coreEvent) /// The event object instance. /// Thrown when the argument is null. /// Thrown when the argument is invalid. + /// Thrown when failed because of an invalid operation. /// /// /// @@ -507,8 +539,17 @@ public void EmitEvent(EventObject eventObject) throw new ArgumentNullException(nameof(eventObject)); } + if (eventObject.Handle == IntPtr.Zero) + { + throw new ArgumentException("Invalid argument"); + } + Interop.LibTizenCore.ErrorCode error = Interop.LibTizenCore.TizenCore.EmitEvent(_handle, eventObject.Handle); - TCoreErrorFactory.CheckAndThrownException(error, "Failed to emit event"); + if (error != Interop.LibTizenCore.ErrorCode.None) + { + throw new InvalidOperationException("Failed to emit event"); + } + eventObject.Handle = IntPtr.Zero; } @@ -632,7 +673,7 @@ public bool Running { /// /// Runs the main loop of the task. /// - /// Thrown when the unmanaged handle is invalid. + /// Thrown when failed because of an invalid operation. /// /// /// @@ -645,13 +686,16 @@ public bool Running { public void Run() { Interop.LibTizenCore.ErrorCode error = Interop.LibTizenCore.TizenCore.TaskRun(_handle); - TCoreErrorFactory.CheckAndThrownException(error, "Failed to run task"); + if (error != Interop.LibTizenCore.ErrorCode.None) + { + throw new InvalidOperationException("Failed to run task"); + } } /// /// Quits the main loop of the task. /// - /// Thrown when the unmanaged handle is invalid. + /// Thrown when failed because of an invalid operation. /// /// This function can be called from any thread. /// It requests the task to finish the current iteration of its loop and stop running. @@ -674,7 +718,10 @@ public void Run() public void Quit() { Interop.LibTizenCore.ErrorCode error = Interop.LibTizenCore.TizenCore.TaskQuit(_handle); - TCoreErrorFactory.CheckAndThrownException(error, "Failed to quit task"); + if (error != Interop.LibTizenCore.ErrorCode.None) + { + throw new InvalidOperationException("Failed to quit task"); + } } ///