Skip to content

Commit

Permalink
[Core] Modify exceptions (#6355)
Browse files Browse the repository at this point in the history
* [Core] Modify exceptions

Some methods should throw InvalidOperationException.
This patch modifies that.

Signed-off-by: Hwankyu Jhun <[email protected]>

* [Core] Add a missing exception to ChannelReceiver.Receive()

Signed-off-by: Hwankyu Jhun <[email protected]>

* [Core] Fix ambiguous error messages

Signed-off-by: Hwankyu Jhun <[email protected]>

* [Core] Fix wrong description & implementation

Signed-off-by: Hwankyu Jhun <[email protected]>

* [Core] Modify ambiguous log message

Signed-off-by: Hwankyu Jhun <[email protected]>

* [Core] Convert error to C# exception

Signed-off-by: Hwankyu Jhun <[email protected]>

* [Core] Fix wrong log message

Signed-off-by: Hwankyu Jhun <[email protected]>

* [Core] Fix wrong error handling

Signed-off-by: Hwankyu Jhun <[email protected]>

---------

Signed-off-by: Hwankyu Jhun <[email protected]>
  • Loading branch information
hjhun authored Sep 25, 2024
1 parent f931ade commit f2a3101
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 14 deletions.
11 changes: 10 additions & 1 deletion src/Tizen.Core/Tizen.Core/ChannelReceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ internal ChannelReceiver(IntPtr handle)
/// Receives the channel object from the sender asynchronously.
/// </summary>
/// <returns>The received channel object.</returns>
/// <exception cref="OutOfMemoryException">Thrown when out of memory.</exception>
/// <exception cref="InvalidOperationException">Thrown when failed because of an invalid operation.</exception>
/// <example>
/// <code>
///
Expand All @@ -84,7 +86,14 @@ public async Task<ChannelObject> 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);
}
Expand Down
18 changes: 16 additions & 2 deletions src/Tizen.Core/Tizen.Core/ChannelSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ internal ChannelSender(IntPtr handle)
/// </summary>
/// <param name="channelObject">The channel object instance.</param>
/// <exception cref="ArgumentNullException">Thrown when the argument is null.</exception>
/// <exception cref="ArgumentException">Thrown when the argument is invalid.</exception>
/// <exception cref="InvalidOperationException">Thrown when failed because of an invalid operation.</exception>
/// <remarks>
/// It's important to call the Dispose() method on the passed channel object to release resources.
/// </remarks>
Expand All @@ -68,16 +70,24 @@ 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;
}

/// <summary>
/// Creates and returns a copy of the channel sender object.
/// </summary>
/// <returns>A newly created channel sender instance.</returns>
/// <exception cref="ArgumentException">Thrown when the argument is invalid.</exception>
/// <exception cref="InvalidOperationException">Thrown when failed because of an invalid operation.</exception>
/// <exception cref="OutOfMemoryException">Thrown when out of memory.</exception>
/// <example>
/// <code>
Expand All @@ -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);
Expand Down
69 changes: 58 additions & 11 deletions src/Tizen.Core/Tizen.Core/Task.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ internal Task(IntPtr handle)
/// </summary>
/// <param name="action">The action callback to post.</param>
/// <exception cref="ArgumentNullException">Thrown when the action argument is null.</exception>
/// <exception cref="ArgumentException">Thrown when failed because of the instance is invalid.</exception>
/// <exception cref="InvalidOperationException">Thrown when failed because of an invalid operation.</exception>
/// <exception cref="OutOfMemoryException">Thrown when out of memory.</exception>
/// <remarks>
/// The action callback will be executed by the main loop of the task.
Expand Down Expand Up @@ -127,15 +127,22 @@ 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");
}
}

/// <summary>
/// Posts a task to be executed later.
/// </summary>
/// <param name="task">The task to post.</param>
/// <exception cref="ArgumentNullException">Thrown when the task argument is null.</exception>
/// <exception cref="ArgumentException">Thrown when failed because of the instance is invalid.</exception>
/// <exception cref="InvalidOperationException">Thrown when failed because of an invalid operation.</exception>
/// <exception cref="OutOfMemoryException">Thrown when out of memory.</exception>
/// <remarks>
/// The task will be stored in the internal map using its unique identifier.
Expand Down Expand Up @@ -178,6 +185,10 @@ public void Post(Func<System.Threading.Tasks.Task> 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");
}
}
Expand All @@ -189,7 +200,7 @@ public void Post(Func<System.Threading.Tasks.Task> task)
/// <param name="callback">The recurring timer callback function which returns whether or not to continue triggering the timer.</param>
/// <returns>The registered timer ID to be used with <see cref="RemoveTimer"/>.</returns>
/// <exception cref="ArgumentNullException">Thrown when the callback argument is null.</exception>
/// <exception cref="ArgumentException">Thrown when failed because of the instance is invalid.</exception>
/// <exception cref="InvalidOperationException">Thrown when failed because of an invalid operation.</exception>
/// <exception cref="OutOfMemoryException">Thrown when out of memory.</exception>
/// <remarks>
/// 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.
Expand Down Expand Up @@ -227,7 +238,7 @@ public int AddTimer(uint interval, Func<bool> 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;
Expand Down Expand Up @@ -275,6 +286,7 @@ public void RemoveTimer(int id)
/// <param name="receiver">The channel receiver instance.</param>
/// <exception cref="ArgumentNullException">Thrown when the argument is null.</exception>
/// <exception cref="ArgumentException">Thrown when the argument is invalid.</exception>
/// <exception cref="InvalidOperationException">Thrown when failed because of an invalid operation.</exception>
/// <exception cref="OutOfMemoryException">Thrown when out of memory.</exception>
/// <example>
/// <code>
Expand All @@ -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;
Expand All @@ -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");
}

Expand Down Expand Up @@ -376,6 +397,7 @@ public void RemoveChannelReceiver(ChannelReceiver receiver)
/// <param name="coreEvent">The event instance.</param>
/// <exception cref="ArgumentNullException">Thrown when the argument is null.</exception>
/// <exception cref="ArgumentException">Thrown when the argument is invalid.</exception>
/// <exception cref="InvalidOperationException">Thrown when failed because of an invalid operation.</exception>
/// <exception cref="OutOfMemoryException">Thrown when out of memory.</exception>
/// <remarks>
/// 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.
Expand Down Expand Up @@ -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");
Expand All @@ -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");
}

Expand Down Expand Up @@ -486,6 +517,7 @@ public void RemoveEvent(Event coreEvent)
/// <param name="eventObject">The event object instance.</param>
/// <exception cref="ArgumentNullException">Thrown when the argument is null.</exception>
/// <exception cref="ArgumentException">Thrown when the argument is invalid.</exception>
/// <exception cref="InvalidOperationException">Thrown when failed because of an invalid operation.</exception>
/// <example>
/// <code>
///
Expand All @@ -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;
}

Expand Down Expand Up @@ -632,7 +673,7 @@ public bool Running {
/// <summary>
/// Runs the main loop of the task.
/// </summary>
/// <exception cref="ArgumentException">Thrown when the unmanaged handle is invalid.</exception>
/// <exception cref="InvalidOperationException">Thrown when failed because of an invalid operation.</exception>
/// <example>
/// <code>
///
Expand All @@ -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");
}
}

/// <summary>
/// Quits the main loop of the task.
/// </summary>
/// <exception cref="ArgumentException">Thrown when the unmanaged handle is invalid.</exception>
/// <exception cref="InvalidOperationException">Thrown when failed because of an invalid operation.</exception>
/// <remarks>
/// This function can be called from any thread.
/// It requests the task to finish the current iteration of its loop and stop running.
Expand All @@ -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");
}
}

/// <summary>
Expand Down

0 comments on commit f2a3101

Please sign in to comment.