Skip to content

Commit f5383a5

Browse files
committed
chore: Make errors more expressive
1 parent b9b24ed commit f5383a5

2 files changed

Lines changed: 21 additions & 5 deletions

File tree

Buttplug/Client/ButtplugClientDeviceFeature.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// Licensed under the BSD 3-Clause license. See LICENSE file in the project root for full license information.
55
// </copyright>
66

7+
using System.Linq;
78
using System.Threading;
89
using System.Threading.Tasks;
910
using Buttplug.Core;
@@ -96,7 +97,13 @@ public async Task RunOutputAsync(DeviceOutputCommand command, CancellationToken
9697
{
9798
if (!HasOutput(command.OutputType))
9899
{
99-
throw new ButtplugDeviceException($"Feature {FeatureIndex} on device {Device.Name} does not support output type {command.OutputType}");
100+
var supportedOutputs = FeatureDefinition.Output?.Keys ?? Enumerable.Empty<string>();
101+
var supportedList = supportedOutputs.Any()
102+
? string.Join(", ", supportedOutputs)
103+
: "none";
104+
throw new ButtplugDeviceException(
105+
$"Feature {FeatureIndex} ({FeatureDescriptor}) on device '{Device.Name}' does not support output type '{command.OutputType}'. " +
106+
$"Supported outputs: {supportedList}.");
100107
}
101108

102109
// Get the output definition to determine step range
@@ -115,7 +122,9 @@ public async Task RunOutputAsync(DeviceOutputCommand command, CancellationToken
115122
{
116123
if (!command.Duration.HasValue)
117124
{
118-
throw new ButtplugDeviceException("PositionWithDuration requires a duration value");
125+
throw new ButtplugDeviceException(
126+
$"PositionWithDuration command for feature {FeatureIndex} ({FeatureDescriptor}) on device '{Device.Name}' requires a duration value. " +
127+
"Use DeviceOutput.PositionWithDuration.Percent(position, durationMs) or DeviceOutput.PositionWithDuration.Steps(steps, durationMs).");
119128
}
120129
cmd = OutputCmd.CreatePositionWithDuration(Device.Index, FeatureIndex, actualValue, command.Duration.Value);
121130
}
@@ -138,7 +147,13 @@ public async Task<InputReading> RunInputAsync(DeviceInputCommand command, Cancel
138147
{
139148
if (!HasInput(command.InputType))
140149
{
141-
throw new ButtplugDeviceException($"Feature {FeatureIndex} on device {Device.Name} does not support input type {command.InputType}");
150+
var supportedInputs = FeatureDefinition.Input?.Keys ?? Enumerable.Empty<string>();
151+
var supportedList = supportedInputs.Any()
152+
? string.Join(", ", supportedInputs)
153+
: "none";
154+
throw new ButtplugDeviceException(
155+
$"Feature {FeatureIndex} ({FeatureDescriptor}) on device '{Device.Name}' does not support input type '{command.InputType}'. " +
156+
$"Supported inputs: {supportedList}.");
142157
}
143158

144159
var cmd = new InputCmd(Device.Index, FeatureIndex, command.InputType, command.CommandType);

Buttplug/Core/ButtplugJsonMessageParser.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public class ButtplugJsonMessageParser
3737
/// <param name="logManager">Log manager, passed from the parser owner.</param>
3838
public ButtplugJsonMessageParser()
3939
{
40-
_serializer = new JsonSerializer { MissingMemberHandling = MissingMemberHandling.Error };
40+
// Use Ignore to allow forward compatibility with new fields from newer servers
41+
_serializer = new JsonSerializer { MissingMemberHandling = MissingMemberHandling.Ignore };
4142
_messageTypes = new Dictionary<string, Type>();
4243
foreach (var messageType in ButtplugUtils.GetAllMessageTypes())
4344
{
@@ -233,4 +234,4 @@ private JObject ButtplugMessageToJObject(ButtplugMessage msg)
233234
return new JObject(new JProperty(msg.Name, JObject.FromObject(msg)));
234235
}
235236
}
236-
}
237+
}

0 commit comments

Comments
 (0)