Skip to content

Commit c740712

Browse files
committed
[dotnet] [bidi] Actively use TryGetValue while processing incoming messages
1 parent cb1c3f4 commit c740712

File tree

1 file changed

+31
-12
lines changed
  • dotnet/src/webdriver/BiDi/Communication

1 file changed

+31
-12
lines changed

dotnet/src/webdriver/BiDi/Communication/Broker.cs

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -378,30 +378,49 @@ private void ProcessReceivedMessage(byte[]? data)
378378
case "success":
379379
if (id is null) throw new JsonException("The remote end responded with 'success' message type, but missed required 'id' property.");
380380

381-
var successCommand = _pendingCommands[id.Value];
382-
var messageSuccess = JsonSerializer.Deserialize(ref resultReader, successCommand.ResultType, _jsonSerializerContext)!;
383-
successCommand.TaskCompletionSource.SetResult((EmptyResult)messageSuccess);
384-
_pendingCommands.TryRemove(id.Value, out _);
381+
if (_pendingCommands.TryGetValue(id.Value, out var successCommand))
382+
{
383+
var messageSuccess = JsonSerializer.Deserialize(ref resultReader, successCommand.ResultType, _jsonSerializerContext)!;
384+
successCommand.TaskCompletionSource.SetResult((EmptyResult)messageSuccess);
385+
_pendingCommands.TryRemove(id.Value, out _);
386+
}
387+
else
388+
{
389+
throw new BiDiException($"The remote end responded with 'success' message type, but no pending command with id {id} was found.");
390+
}
391+
385392
break;
386393

387394
case "event":
388395
if (method is null) throw new JsonException("The remote end responded with 'event' message type, but missed required 'method' property.");
389396

390-
var eventType = _eventTypesMap[method];
397+
if (_eventTypesMap.TryGetValue(method, out var eventType))
398+
{
399+
var eventArgs = (EventArgs)JsonSerializer.Deserialize(ref paramsReader, eventType, _jsonSerializerContext)!;
391400

392-
var eventArgs = (EventArgs)JsonSerializer.Deserialize(ref paramsReader, eventType, _jsonSerializerContext)!;
401+
var messageEvent = new MessageEvent(method, eventArgs);
402+
_pendingEvents.Add(messageEvent);
403+
}
404+
else
405+
{
406+
throw new BiDiException($"The remote end responded with 'event' message type, but no event type mapping for method '{method}' was found.");
407+
}
393408

394-
var messageEvent = new MessageEvent(method, eventArgs);
395-
_pendingEvents.Add(messageEvent);
396409
break;
397410

398411
case "error":
399412
if (id is null) throw new JsonException("The remote end responded with 'error' message type, but missed required 'id' property.");
400413

401-
var messageError = new MessageError(id.Value) { Error = error, Message = message };
402-
var errorCommand = _pendingCommands[messageError.Id];
403-
errorCommand.TaskCompletionSource.SetException(new BiDiException($"{messageError.Error}: {messageError.Message}"));
404-
_pendingCommands.TryRemove(messageError.Id, out _);
414+
if (_pendingCommands.TryGetValue(id.Value, out var errorCommand))
415+
{
416+
errorCommand.TaskCompletionSource.SetException(new BiDiException($"{error}: {message}"));
417+
_pendingCommands.TryRemove(id.Value, out _);
418+
}
419+
else
420+
{
421+
throw new BiDiException($"The remote end responded with 'error' message type, but no pending command with id {id} was found.");
422+
}
423+
405424
break;
406425
}
407426
}

0 commit comments

Comments
 (0)