@@ -378,30 +378,49 @@ private void ProcessReceivedMessage(byte[]? data)
378
378
case "success" :
379
379
if ( id is null ) throw new JsonException ( "The remote end responded with 'success' message type, but missed required 'id' property." ) ;
380
380
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
+
385
392
break ;
386
393
387
394
case "event" :
388
395
if ( method is null ) throw new JsonException ( "The remote end responded with 'event' message type, but missed required 'method' property." ) ;
389
396
390
- var eventType = _eventTypesMap [ method ] ;
397
+ if ( _eventTypesMap . TryGetValue ( method , out var eventType ) )
398
+ {
399
+ var eventArgs = ( EventArgs ) JsonSerializer . Deserialize ( ref paramsReader , eventType , _jsonSerializerContext ) ! ;
391
400
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
+ }
393
408
394
- var messageEvent = new MessageEvent ( method , eventArgs ) ;
395
- _pendingEvents . Add ( messageEvent ) ;
396
409
break ;
397
410
398
411
case "error" :
399
412
if ( id is null ) throw new JsonException ( "The remote end responded with 'error' message type, but missed required 'id' property." ) ;
400
413
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
+
405
424
break ;
406
425
}
407
426
}
0 commit comments