Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 0.9.2 #17

Merged
merged 1 commit into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,31 @@ All notable changes to this package will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [0.9.2] - 2024-10-25

**Fixed**
- Changed the try/catch exceptions to Debug.LogException with a Debug.LogError, to properly show the issues in the Unity console

## [0.9.1] - 2023-09-04

**Fixed**
- Added missing documentation to describe the purpose of the *InnerStateData* and *StateFactoryData* classes.

## [0.9.0] - 2023-09-02

- Introduced unique identifiers for *IWaitActivity* instances to differentiate between different activities
- Enhanced the *TaskWaitState* class with a new event queuing mechanism to handle events after task completion
- Improved the *SplitState* class to allow pausing/resuming of inner state transitions

**Changed**
- Updated exception handling across various classes, replacing *MissingMemberException* with *InvalidOperationException* for more appropriate error representation

## [0.8.0] - 2023-08-03

**Changed**
- Change the main class name from *Statemachine* to *Statechart* Change namespace to avoid conflicts to StatechartMachine
- Changed both *ISplitState* and *INestedState* to receive a single parameterized *NestedStateData* to make calls less verbose and ready for future scalability

## [0.7.0] - 2021-05-04

- Improved State documentations
Expand Down
28 changes: 24 additions & 4 deletions Runtime/Internal/StateInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,14 @@ private void TriggerEnter(IStateInternal state)
}
catch (Exception e)
{
throw new Exception($"Exception in the state {state.Name}, OnEnter() actions.\n" + CreationStackTrace, e);
var finalMessage = "";
#if UNITY_EDITOR || DEBUG
finalMessage = $"\nStackTrace log of '{state.Name}' state creation bellow.\n{CreationStackTrace}";
#endif

Debug.LogError($"Exception in the state '{state.Name}', OnExit() actions.\n" +
$"-->> Check the exception log after this one for more details <<-- {finalMessage}");
Debug.LogException(e);
Comment on lines +167 to +174

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

The try/catch block has been modified to log the exception using Debug.LogError and Debug.LogException. The code also includes a conditional message for logging exceptions in the Unity Editor or Debug mode. This change improves error visibility in the Unity console.

However, there are a few suggestions to consider:

  1. Line 168: The variable finalMessage is initialized as an empty string and then assigned a value based on the condition UNITY_EDITOR || DEBUG. Instead of initializing it as an empty string, you can directly assign the value inside the condition. This will eliminate the need for an extra line of code.

  2. Line 170: The comment mentions "StackTrace log of '{state.Name}' state creation below," but the code actually logs the CreationStackTrace instead of the state name. Consider updating the comment to accurately reflect the logged information.

  3. Line 172: The error message includes the text "-->> Check the exception log after this one for more details <<--". It's unclear what "exception log" refers to. Consider providing more specific instructions or clarifying the intended behavior.

  4. Line 173: The error message includes the state name, but it's not clear why the state name is relevant in the context of the exception. Consider removing the state name from the error message unless it provides valuable information for debugging.

  5. Line 174: The Debug.LogException(e) statement logs the exception object. It's important to note that logging the entire exception object may expose sensitive information in a production environment. Ensure that the exception object does not contain any sensitive data before logging it.

}
}

Expand All @@ -180,7 +187,14 @@ private void TriggerExit()
}
catch(Exception e)
{
throw new Exception($"Exception in the state '{Name}', OnExit() actions.\n" + CreationStackTrace, e);
var finalMessage = "";
#if UNITY_EDITOR || DEBUG
finalMessage = $"\nStackTrace log of '{Name}' state creation bellow.\n{CreationStackTrace}";
#endif

Debug.LogError($"Exception in the state '{Name}', OnExit() actions.\n" +
$"-->> Check the exception log after this one for more details <<-- {finalMessage}");
Debug.LogException(e);
Comment on lines +190 to +197

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

Similar changes have been made to another catch block in the same file. The suggestions mentioned above apply to this section as well.

}
}

Expand All @@ -199,8 +213,14 @@ private void TriggerTransition(ITransitionInternal transition, string eventName)
}
catch (Exception e)
{
throw new Exception($"Exception in the transition '{Name}' -> '{transition.TargetState?.Name}'," +
$" TriggerTransition() actions.\n{transition.CreationStackTrace}", e);
var finalMessage = "";
#if UNITY_EDITOR || DEBUG
finalMessage = $"\nStackTrace log of this transition creation bellow.\n{transition.CreationStackTrace}";
#endif

Debug.LogError($"Exception in the transition '{Name}' -> '{transition.TargetState?.Name}'" +
$"-->> Check the exception log after this one for more details <<-- {finalMessage}");
Debug.LogException(e);
Comment on lines +216 to +223

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

Another catch block has been modified to log the exception using Debug.LogError and Debug.LogException. The code includes a conditional message for logging exceptions in the Unity Editor or Debug mode. The suggestions mentioned above also apply to this section.

Overall, the changes improve error visibility in the Unity console. However, it's important to review the error messages and ensure they provide clear and relevant information for debugging purposes. Additionally, be cautious about logging sensitive information in the exception object.

}
}
}
Expand Down
11 changes: 9 additions & 2 deletions Runtime/Internal/TaskWaitState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,15 @@ private async Task InnerTaskAwait(string eventName)
}
catch (Exception e)
{
throw new Exception($"Exception in the state '{Name}', when calling the task wait action " +
$"'{_taskAwaitAction.Target}.{_taskAwaitAction.Method.Name}()'.\n" + CreationStackTrace, e);
var finalMessage = "";
#if UNITY_EDITOR || DEBUG
finalMessage = $"\nStackTrace log of '{Name}' state creation bellow.\n{CreationStackTrace}";
#endif

Debug.LogError($"Exception in the state '{Name}', when calling the task wait action" +
$"'{_taskAwaitAction.Target}.{_taskAwaitAction.Method.Name}()'.\n" +
$"-->> Check the exception log after this one for more details <<-- {finalMessage}");
Debug.LogException(e);
Comment on lines +146 to +154

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

The exception handling in this code has been modified. Instead of throwing a new exception with a detailed message and stack trace, the code now logs an error message using Debug.LogError and logs the exception itself using Debug.LogException. Additionally, a conditional message is added to include the creation stack trace when running in the Unity Editor or Debug mode.

While the change improves error visibility in the Unity console, it removes the ability to catch and handle the exception at a higher level. This may limit the flexibility of error handling and make it harder to debug issues that occur during the task wait action.

Consider the following suggestions:

  1. If catching and handling the exception at a higher level is necessary, you can revert the changes and keep the original code that throws a new exception with a detailed message and stack trace. This allows for more flexibility in error handling.

  2. If logging the exception and its stack trace is sufficient for debugging purposes, you can keep the current implementation. However, it would be beneficial to include the exception type in the log message to provide more context about the error. For example:

Debug.LogError($"Exception of type {e.GetType()} occurred in the state '{Name}', when calling the task wait action " +
    $"'{_taskAwaitAction.Target}.{_taskAwaitAction.Method.Name}()'.\n" +
    $"-->> Check the exception log after this one for more details <<-- {finalMessage}");
  1. If you want to strike a balance between logging the exception and allowing for higher-level error handling, you can modify the code to rethrow the exception after logging it. This way, the exception will still propagate up the call stack, but you'll have a record of it in the log. Here's an example:
Debug.LogError($"Exception in the state '{Name}', when calling the task wait action " +
    $"'{_taskAwaitAction.Target}.{_taskAwaitAction.Method.Name}()'.\n" +
    $"-->> Check the exception log after this one for more details <<-- {finalMessage}");
Debug.LogException(e);
throw;

Choose the approach that best suits your error handling and debugging requirements.

}
}
}
Expand Down
23 changes: 15 additions & 8 deletions Runtime/Internal/WaitState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public override void Enter()
_waitingActivity = new WaitActivity(OnActivityComplete);
_triggered = false;

for(int i = 0; i < _onEnter.Count; i++)
for (int i = 0; i < _onEnter.Count; i++)
{
_onEnter[i]?.Invoke();
}
Expand All @@ -48,7 +48,7 @@ public override void Enter()
/// <inheritdoc />
public override void Exit()
{
for(int i = 0; i < _onExit.Count; i++)
for (int i = 0; i < _onExit.Count; i++)
{
_onExit[i]?.Invoke();
}
Expand Down Expand Up @@ -132,7 +132,7 @@ public ITransition WaitingFor(Action<IWaitActivity> waitAction)

private void OnActivityComplete(uint id)
{
if(id == _waitingActivity.Id)
if (id == _waitingActivity.Id)
{
_stateFactory.Data.StateChartMoveNextCall(null);
}
Expand All @@ -143,7 +143,7 @@ protected override ITransitionInternal OnTrigger(IStatechartEvent statechartEven
{
if (statechartEvent != null && _events.TryGetValue(statechartEvent, out var transition))
{
if(transition.TargetState != null)
if (transition.TargetState != null)
{
_waitingActivity.ForceComplete();
}
Expand All @@ -167,14 +167,21 @@ private void InnerWait(string eventName)
if (IsStateLogsEnabled)
{
Debug.Log($"'{eventName}' event triggers the wait method '{_waitAction.Method.Name}'" +
$"from the object {_waitAction.Target} in the state {Name}");
$"from the object {_waitAction.Target} in the state {Name}");
}
_waitAction(_waitingActivity);
}
catch(Exception e)
catch (Exception e)
{
throw new Exception($"Exception in the state '{Name}', when calling the wait action {_waitAction.Method.Name}" +
$"from the object {_waitAction.Target}.\n" + CreationStackTrace, e);
var finalMessage = "";
#if UNITY_EDITOR || DEBUG
finalMessage = $"\nStackTrace log of '{Name}' state creation bellow.\n{CreationStackTrace}";
#endif

Debug.LogError($"Exception in the state '{Name}', when calling the wait action {_waitAction.Method.Name}" +
$" from the object {_waitAction.Target}.\n" +
$"-->> Check the exception log after this one for more details <<-- {finalMessage}");
Debug.LogException(e);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"name": "com.gamelovers.statechart",
"displayName": "Statechart",
"author": "Miguel Tomas",
"version": "0.9.1",
"unity": "2021.4",
"version": "0.9.2",
"unity": "2022.4",
"license": "MIT",
"description": "This package allows the use of Statecharts (Hierarchichal State Machine) within an Unity project.\n\nThe primary feature of Statecharts is that states can be organized in a hierarchy.\nA Statecharts is a state machine where each state in the state machine may define its own subordinate state machines, called substates.\nThose states can again define substates.\n\nFor more information: https://statecharts.github.io/what-is-a-statechart.html",
"type": "library",
Expand Down
Loading