Fix uigadget api14#7654
Conversation
- Wrap UIGadgetInfo.CreateUIGadgetInfo with try/catch and log packageId on exception - Add try/catch in UIGadgetManager static constructor to prevent TypeInitializationException - Add try/catch in foreach loop to continue loading other packages on failure - Add try/catch in UIGadgetManager.Refresh() with packageId in error logs - Add try/catch in NUIGadgetManager static constructor, LoadGadgetInfos, and Refresh Signed-off-by: Changgyu Choi <changyu.choi@samsung.com>
- Use 'as' cast instead of direct cast for CoreApplication.Current with null check - Remove redundant outer try/catch in foreach loops (CreateUIGadgetInfo already handles exceptions internally) - Move PackageInfoDestroy to finally block for proper native handle cleanup - Remove redundant null check after 'new NUIGadgetInfo' operator Co-Authored-By: Cline SR Signed-off-by: Changgyu Choi <changyu.choi@samsung.com>
There was a problem hiding this comment.
Code Review
This pull request enhances the robustness of the UIGadget and NUIGadget managers by implementing comprehensive error handling and improved logging. Key updates include wrapping static constructors and resource-intensive methods in try-catch blocks, ensuring native package handles are consistently released in UIGadgetInfo via finally blocks, and providing more context in error logs. Feedback suggests further improving reliability by broadening exception filters in UIGadgetManager to prevent TypeInitializationException and adding explicit logging for null application instances to aid in troubleshooting.
| catch (Exception e) when (e is ArgumentNullException || e is OverflowException) | ||
| { | ||
| Log.Error("Exception occurs while adding gadget info. packageId: " + pkg + ", " + e.Message); | ||
| } |
There was a problem hiding this comment.
The exception filter here is quite restrictive. To ensure the robustness intended by this pull request and prevent TypeInitializationException, it is safer to catch all exceptions during gadget info registration, consistent with the implementation in NUIGadgetManager.cs. Additionally, logging the full exception object e instead of just e.Message provides the stack trace, which is essential for debugging.
catch (Exception e)
{
Log.Error("Exception occurs while adding gadget info. packageId: " + pkg + ", " + e);
}| var app = CoreApplication.Current as CoreApplication; | ||
| if (app != null) | ||
| { | ||
| app.AppControlReceived += (s, e) => HandleAppControl(e); | ||
| app.LowMemory += (s, e) => HandleLowMemoryEvent(e); | ||
| app.LowBattery += (s, e) => HandleLowBatteryEvent(e); | ||
| app.LocaleChanged += (s, e) => HandleLocaleChangedEvent(e); | ||
| app.RegionFormatChanged += (s, e) => HandleRegionFormatChangedEvent(e); | ||
| app.DeviceOrientationChanged += (s, e) => HandleDeviceOrientationChangedEvent(e); | ||
| } |
There was a problem hiding this comment.
If CoreApplication.Current is null or cannot be cast to CoreApplication, the manager will silently skip registering critical system event handlers (AppControl, LowMemory, etc.). This could lead to difficult-to-debug issues where the application fails to respond to system events. It is recommended to log an error if app is null to aid in troubleshooting.
var app = CoreApplication.Current as CoreApplication;
if (app != null)
{
app.AppControlReceived += (s, e) => HandleAppControl(e);
app.LowMemory += (s, e) => HandleLowMemoryEvent(e);
app.LowBattery += (s, e) => HandleLowBatteryEvent(e);
app.LocaleChanged += (s, e) => HandleLocaleChangedEvent(e);
app.RegionFormatChanged += (s, e) => HandleRegionFormatChangedEvent(e);
app.DeviceOrientationChanged += (s, e) => HandleDeviceOrientationChangedEvent(e);
}
else
{
Log.Error("Failed to get CoreApplication instance. Event handlers will not be registered.");
}| catch (Exception e) when (e is ArgumentNullException || e is OverflowException) | ||
| { | ||
| Log.Error("Exception occurs. " + e.Message); | ||
| Log.Error("Exception occurs while adding gadget info. packageId: " + pkg + ", " + e.Message); | ||
| } |
There was a problem hiding this comment.
For consistency and improved robustness, consider catching all exceptions here instead of using a specific filter. Logging the full exception object e is also preferred as it includes the stack trace for better diagnostics.
catch (Exception e)
{
Log.Error("Exception occurs while adding gadget info. packageId: " + pkg + ", " + e);
}|
🤖 [AI Review] Reviewed — no findings. Scope checked:
No 🔴 critical issues, no 🟡 suggestions to flag. Automated review — final merge decision rests with human reviewers. |
Description of Change
API Changes