@@ -74,82 +74,104 @@ private ILspHandlerDescriptor FindDescriptor(string method, JToken @params)
7474 return _routeMatchers . SelectMany ( strat => strat . FindHandler ( paramsValue , lspHandlerDescriptors ) ) . FirstOrDefault ( ) ?? descriptor ;
7575 }
7676
77- public async Task RouteNotification ( IHandlerDescriptor handler , Notification notification )
77+ public async Task RouteNotification ( IHandlerDescriptor descriptor , Notification notification )
7878 {
79- try
79+ using ( _logger . TimeDebug ( "Routing Notification {Method}" , notification . Method ) )
8080 {
81- Task result ;
82- if ( handler . Params is null )
81+ using ( _logger . BeginScope ( new KeyValuePair < string , string > [ ] {
82+ new KeyValuePair < string , string > ( "Method" , notification . Method ) ,
83+ new KeyValuePair < string , string > ( "Params" , notification . Params ? . ToString ( ) )
84+ } ) )
8385 {
84- result = ReflectionRequestHandlers . HandleNotification ( handler ) ;
86+ try
87+ {
88+ if ( descriptor . Params is null )
89+ {
90+ await ReflectionRequestHandlers . HandleNotification ( descriptor ) ;
91+ }
92+ else
93+ {
94+ _logger . LogDebug ( "Converting params for Notification {Method} to {Type}" , notification . Method , descriptor . Params . FullName ) ;
95+ var @params = notification . Params . ToObject ( descriptor . Params , _serializer . JsonSerializer ) ;
96+ await ReflectionRequestHandlers . HandleNotification ( descriptor , @params ) ;
97+ }
98+ }
99+ catch ( Exception e )
100+ {
101+ _logger . LogCritical ( Events . UnhandledRequest , e , "Failed to handle request {Method}" , notification . Method ) ;
102+ }
85103 }
86- else
87- {
88- var @params = notification . Params . ToObject ( handler . Params , _serializer . JsonSerializer ) ;
89- result = ReflectionRequestHandlers . HandleNotification ( handler , @params ) ;
90- }
91-
92- await result ;
93- }
94- catch ( Exception e )
95- {
96- _logger . LogCritical ( Events . UnhandledRequest , e , "Failed to handle request {Method}" , notification . Method ) ;
97104 }
98105 }
99106
100107 public async Task < ErrorResponse > RouteRequest ( IHandlerDescriptor descriptor , Request request )
101108 {
102- var id = GetId ( request . Id ) ;
103- var cts = new CancellationTokenSource ( ) ;
104- _requests . TryAdd ( id , cts ) ;
105-
106- // TODO: Try / catch for Internal Error
107- try
109+ using ( _logger . TimeDebug ( "Routing Request ({Id}) {Method}" , request . Id , request . Method ) )
108110 {
109- if ( descriptor is null )
110- {
111- return new MethodNotFound ( request . Id , request . Method ) ;
112- }
113-
114- object @params ;
115- try
116- {
117- @params = request . Params ? . ToObject ( descriptor . Params , _serializer . JsonSerializer ) ;
118- }
119- catch ( Exception cannotDeserializeRequestParams )
120- {
121- _logger . LogError ( new EventId ( - 32602 ) , cannotDeserializeRequestParams , "Failed to deserialise request parameters." ) ;
122-
123- return new InvalidParams ( request . Id ) ;
124- }
125-
126- var result = ReflectionRequestHandlers . HandleRequest ( descriptor , @params , cts . Token ) . ConfigureAwait ( false ) ;
127- await result ;
128-
129- object responseValue = null ;
130- if ( result . GetType ( ) . GetTypeInfo ( ) . IsGenericType )
111+ using ( _logger . BeginScope ( new KeyValuePair < string , string > [ ] {
112+ new KeyValuePair < string , string > ( "Id" , request . Id ? . ToString ( ) ) ,
113+ new KeyValuePair < string , string > ( "Method" , request . Method ) ,
114+ new KeyValuePair < string , string > ( "Params" , request . Params ? . ToString ( ) )
115+ } ) )
131116 {
132- var property = typeof ( Task < > )
133- . MakeGenericType ( result . GetType ( ) . GetTypeInfo ( ) . GetGenericArguments ( ) [ 0 ] ) . GetTypeInfo ( )
134- . GetProperty ( nameof ( Task < object > . Result ) , BindingFlags . Public | BindingFlags . Instance ) ;
135-
136- responseValue = property . GetValue ( result ) ;
117+ var id = GetId ( request . Id ) ;
118+ var cts = new CancellationTokenSource ( ) ;
119+ _requests . TryAdd ( id , cts ) ;
120+
121+ // TODO: Try / catch for Internal Error
122+ try
123+ {
124+ if ( descriptor is null )
125+ {
126+ _logger . LogDebug ( "descriptor not found for Request ({Id}) {Method}" , request . Id , request . Method ) ;
127+ return new MethodNotFound ( request . Id , request . Method ) ;
128+ }
129+
130+ object @params ;
131+ try
132+ {
133+ _logger . LogDebug ( "Converting params for Request ({Id}) {Method} to {Type}" , request . Id , request . Method , descriptor . Params . FullName ) ;
134+ @params = request . Params ? . ToObject ( descriptor . Params , _serializer . JsonSerializer ) ;
135+ }
136+ catch ( Exception cannotDeserializeRequestParams )
137+ {
138+ _logger . LogError ( new EventId ( - 32602 ) , cannotDeserializeRequestParams , "Failed to deserialise request parameters." ) ;
139+ return new InvalidParams ( request . Id ) ;
140+ }
141+
142+ var result = ReflectionRequestHandlers . HandleRequest ( descriptor , @params , cts . Token ) ;
143+ await result ;
144+
145+ _logger . LogDebug ( "Result was {Type}" , result . GetType ( ) . FullName ) ;
146+
147+ object responseValue = null ;
148+ if ( result . GetType ( ) . GetTypeInfo ( ) . IsGenericType )
149+ {
150+ var property = typeof ( Task < > )
151+ . MakeGenericType ( result . GetType ( ) . GetTypeInfo ( ) . GetGenericArguments ( ) [ 0 ] ) . GetTypeInfo ( )
152+ . GetProperty ( nameof ( Task < object > . Result ) , BindingFlags . Public | BindingFlags . Instance ) ;
153+
154+ responseValue = property . GetValue ( result ) ;
155+ _logger . LogDebug ( "Response value was {Type}" , responseValue ? . GetType ( ) . FullName ) ;
156+ }
157+
158+ return new JsonRpc . Client . Response ( request . Id , responseValue ) ;
159+ }
160+ catch ( TaskCanceledException e )
161+ {
162+ _logger . LogDebug ( "Request {Id} was cancelled" , id ) ;
163+ return new RequestCancelled ( ) ;
164+ }
165+ catch ( Exception e )
166+ {
167+ _logger . LogCritical ( Events . UnhandledRequest , e , "Failed to handle notification {Method}" , request . Method ) ;
168+ return new InternalError ( id ) ;
169+ }
170+ finally
171+ {
172+ _requests . TryRemove ( id , out var _ ) ;
173+ }
137174 }
138-
139- return new JsonRpc . Client . Response ( request . Id , responseValue ) ;
140- }
141- catch ( TaskCanceledException )
142- {
143- return new RequestCancelled ( ) ;
144- }
145- catch ( Exception e )
146- {
147- _logger . LogCritical ( Events . UnhandledRequest , e , "Failed to handle notification {Method}" , request . Method ) ;
148- return new InternalError ( id ) ;
149- }
150- finally
151- {
152- _requests . TryRemove ( id , out var _ ) ;
153175 }
154176 }
155177
@@ -159,6 +181,10 @@ public void CancelRequest(object id)
159181 {
160182 cts . Cancel ( ) ;
161183 }
184+ else
185+ {
186+ _logger . LogDebug ( "Request {Id} was not found to cancel" , id ) ;
187+ }
162188 }
163189
164190 public IHandlerDescriptor GetDescriptor ( Notification notification )
0 commit comments