From: hjhun <36876573+hjhun@users.noreply.github.com> Date: Wed, 25 Sep 2024 07:23:53 +0000 (+0900) Subject: [Core] Modify exceptions (#6355) X-Git-Tag: submit/tizen/20240925.121854~1^2~8 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=f2a3101466043f0c75c2a97e571788ac83d53452;p=platform%2Fcore%2Fcsapi%2Ftizenfx.git [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 --- diff --git a/src/Tizen.Core/Tizen.Core/ChannelReceiver.cs b/src/Tizen.Core/Tizen.Core/ChannelReceiver.cs index c81699541..c1c62d435 100644 --- a/src/Tizen.Core/Tizen.Core/ChannelReceiver.cs +++ b/src/Tizen.Core/Tizen.Core/ChannelReceiver.cs @@ -66,6 +66,8 @@ namespace Tizen.Core /// 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 @@ namespace Tizen.Core 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 45be966a8..ae170bac4 100644 --- a/src/Tizen.Core/Tizen.Core/ChannelSender.cs +++ b/src/Tizen.Core/Tizen.Core/ChannelSender.cs @@ -44,6 +44,8 @@ namespace Tizen.Core /// /// 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 @@ namespace Tizen.Core 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 @@ namespace Tizen.Core /// 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 @@ namespace Tizen.Core 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 a0bbf61fb..4ca4cc81d 100644 --- a/src/Tizen.Core/Tizen.Core/Task.cs +++ b/src/Tizen.Core/Tizen.Core/Task.cs @@ -93,7 +93,7 @@ namespace Tizen.Core /// /// 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 @@ namespace Tizen.Core } _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 @@ namespace Tizen.Core /// /// 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 @@ namespace Tizen.Core 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 @@ namespace Tizen.Core /// 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 @@ namespace Tizen.Core 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 @@ namespace Tizen.Core /// 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 @@ namespace Tizen.Core 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 @@ namespace Tizen.Core 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 @@ namespace Tizen.Core /// 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 @@ namespace Tizen.Core 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 @@ namespace Tizen.Core 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 @@ namespace Tizen.Core /// 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 @@ namespace Tizen.Core 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 @@ namespace Tizen.Core /// /// 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 @@ namespace Tizen.Core 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 @@ namespace Tizen.Core 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"); + } } ///