Skip to content

Fix misleading "unknown device type" log when a plugin factory throws - #1453

Open
ngenovese11 with Copilot wants to merge 3 commits into
mainfrom
copilot/fix-misleading-log-message
Open

Fix misleading "unknown device type" log when a plugin factory throws#1453
ngenovese11 with Copilot wants to merge 3 commits into
mainfrom
copilot/fix-misleading-log-message

Conversation

Copilot AI commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

When a plugin's device factory method threw an exception during construction, DeviceFactory.GetDevice caught it, logged it, and returned null. The null result was then misinterpreted by callers as an unregistered device type, producing a misleading "ERROR: Cannot load unknown device type..." message that obscured the real failure cause.

Root cause

  • DeviceFactory.GetDevice wrapped the entire method body (type lookup + factory invocation) in a single try/catch, collapsing "type not found" and "factory threw" into the same null return path.
  • Callers (ControlSystem.LoadDevices, LoadRooms) can't distinguish the two cases from a null return, so they always assumed "unknown type."

Changes

  • DeviceFactory.GetDevice: no longer catches exceptions from the registered factory method's invocation. Type-not-found still returns null with an accurate warning; any exception from wrapper.FactoryMethod(localDc) now propagates to the caller.
  • Callers unchanged in behavior but now accurate: ControlSystem.LoadDevices/LoadRooms already had catch (Exception e) blocks that log the exception with its message and stack trace via Debug.LogMessage(e, ...) — this path now actually gets exercised for factory failures instead of being masked.
  • Removed unused local variables (key, name, type) left over from the refactor.

Effect

// Before: factory throws NullReferenceException while building the device
ERROR: Cannot load unknown device type 'my-plugin-device', key 'device1'.

// After: real exception surfaces with context
ERROR: Creating device device1. Skipping device.
System.NullReferenceException: Object reference not set to an instance of an object.
   at MyPlugin.Factory.BuildDevice(...)

Copilot AI changed the title [WIP] Fix misleading log message when device fails to load Fix misleading "unknown device type" log when a plugin factory throws Jul 28, 2026
Copilot AI requested a review from ngenovese11 July 28, 2026 17:34
@ngenovese11
ngenovese11 marked this pull request as ready for review July 28, 2026 17:35
@ngenovese11

Copy link
Copy Markdown
Contributor

@copilot we need to add an empty commit with a semantic label to trigger a new build

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a misleading “unknown device type” error by changing DeviceFactory.GetDevice so that exceptions thrown by a registered plugin factory are no longer swallowed and converted into a null result (which callers previously interpreted as “type not registered”).

Changes:

  • Refactors DeviceFactory.GetDevice to only return null for “type not found” and allow factory exceptions to propagate to callers.
  • Updates XML documentation to describe the new behavior.
  • Removes unused locals left from the refactor.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +203 to +207
var properties = localDc.Properties;

if (!FactoryMethods.TryGetValue(typeName, out var wrapper))
{
Debug.LogWarning("Device type '{typeName}' not found in DeviceFactory", typeName);
return null;
}
var typeName = localDc.Type.ToLower();

Debug.LogInformation("Loading '{type}' from {assemblyName}", typeName, wrapper.Type.Assembly.FullName);
if (properties is JObject jObject)
return null;
}

Debug.LogInformation("Loading '{type}' from {assemblyName}", typeName, wrapper.Type.Assembly.FullName);
Comment on lines 195 to +198
/// <returns>An instance of a device that implements <see cref="IKeyed"/>, or <see langword="null"/> if the device type is
/// not recognized or an error occurs during creation.</returns>
/// not recognized.</returns>
/// <exception cref="Exception">Thrown when the registered factory method for the device type throws an exception while creating the
/// device. Callers should catch and log this exception to report the actual cause of the failure.</exception>

Copilot AI commented Jul 28, 2026

Copy link
Copy Markdown
Contributor Author

@copilot we need to add an empty commit with a semantic label to trigger a new build

Added an empty chore: trigger build commit to kick off a new build.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]-Misleading log message when device fails to load

3 participants