Skip to content

Commit 3418b51

Browse files
Merge pull request #38 from OmniSharp/fix/error
Breaking: Error object was misshaped, textdocumentsync double registered.
2 parents 545fffb + f1c675a commit 3418b51

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+422
-208
lines changed

src/JsonRpc/Client/Response.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
using Newtonsoft.Json;
1+
using Newtonsoft.Json;
22
using Newtonsoft.Json.Serialization;
33

44
namespace OmniSharp.Extensions.JsonRpc.Client
55
{
66
[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
77
public class Response
88
{
9-
public Response(object id) : this(id, null)
9+
public Response(object id)
1010
{
11+
Id = id;
1112
}
1213

1314
public Response(object id, object result)
@@ -22,4 +23,4 @@ public Response(object id, object result)
2223

2324
public object Result { get; set; }
2425
}
25-
}
26+
}

src/JsonRpc/Error.cs

-43
This file was deleted.

src/JsonRpc/ErrorResponse.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
using OmniSharp.Extensions.JsonRpc.Client;
1+
using OmniSharp.Extensions.JsonRpc.Client;
22

33
namespace OmniSharp.Extensions.JsonRpc
44
{
55
public struct ErrorResponse
66
{
7-
public ErrorResponse(Error error)
7+
public ErrorResponse(RpcError error)
88
{
99
Response = null;
1010
Error = error;
@@ -20,17 +20,17 @@ public ErrorResponse(Response response)
2020
public Response Response { get; }
2121

2222
public bool IsError => Error != null;
23-
public Error Error { get; }
23+
public RpcError Error { get; }
2424
public object Value => IsResponse ? (object)Response : IsError ? Error : null;
2525

2626
public static implicit operator ErrorResponse(Response response)
2727
{
2828
return new ErrorResponse(response);
2929
}
3030

31-
public static implicit operator ErrorResponse(Error error)
31+
public static implicit operator ErrorResponse(RpcError error)
3232
{
3333
return new ErrorResponse(error);
3434
}
3535
}
36-
}
36+
}

src/JsonRpc/HandlerCollection.cs

+2-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections;
33
using System.Collections.Generic;
44
using System.Linq;
@@ -66,16 +66,6 @@ public IDisposable Add(IJsonRpcHandler handler)
6666
return h;
6767
}
6868

69-
public IHandlerInstance Get(IJsonRpcHandler handler)
70-
{
71-
return _handlers.Find(instance => instance.Handler == handler);
72-
}
73-
74-
public IHandlerInstance Get(string method)
75-
{
76-
return _handlers.Find(instance => instance.Method == method);
77-
}
78-
7969
private static readonly Type[] HandlerTypes = { typeof(INotificationHandler), typeof(INotificationHandler<>), typeof(IRequestHandler<>), typeof(IRequestHandler<,>), };
8070

8171
private string GetMethodName(Type type)
@@ -115,4 +105,4 @@ private Type GetHandlerInterface(Type type)
115105
.First(IsValidInterface);
116106
}
117107
}
118-
}
108+
}

src/JsonRpc/InputHandler.cs

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
using System;
1+
using System;
22
using System.IO;
33
using System.Linq;
44
using System.Threading;
55
using System.Threading.Tasks;
66
using Newtonsoft.Json.Linq;
77
using Microsoft.Extensions.Logging;
8+
using Newtonsoft.Json;
9+
using OmniSharp.Extensions.JsonRpc.Server;
810
using OmniSharp.Extensions.JsonRpc.Server.Messages;
911

1012
namespace OmniSharp.Extensions.JsonRpc
@@ -147,13 +149,13 @@ private void HandleRequest(string request)
147149
var tcs = _responseRouter.GetRequest(id);
148150
if (tcs is null) continue;
149151

150-
if (response.Error is null)
152+
if (response is ServerResponse serverResponse)
151153
{
152-
tcs.SetResult(response.Result);
154+
tcs.SetResult(serverResponse.Result);
153155
}
154-
else
156+
else if (response is ServerError serverError)
155157
{
156-
tcs.SetException(new Exception(response.Error));
158+
tcs.SetException(new Exception(JsonConvert.SerializeObject(serverError.Error)));
157159
}
158160
}
159161

@@ -167,8 +169,7 @@ private void HandleRequest(string request)
167169
_scheduler.Add(
168170
type,
169171
item.Request.Method,
170-
async () =>
171-
{
172+
async () => {
172173
try
173174
{
174175
var result = await _requestRouter.RouteRequest(item.Request);
@@ -189,8 +190,7 @@ private void HandleRequest(string request)
189190
_scheduler.Add(
190191
type,
191192
item.Notification.Method,
192-
() =>
193-
{
193+
() => {
194194
try
195195
{
196196
_requestRouter.RouteNotification(item.Notification);

src/JsonRpc/Reciever.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Collections.Generic;
1+
using System.Collections.Generic;
22
using System.Linq;
33
using Newtonsoft.Json.Linq;
44
using OmniSharp.Extensions.JsonRpc.Server;
@@ -63,12 +63,13 @@ protected virtual Renor GetRenor(JToken @object)
6363

6464
if (hasRequestId && request.TryGetValue("result", out var response))
6565
{
66-
return new Response(requestId, response);
66+
return new ServerResponse(requestId, response);
6767
}
6868

6969
if (hasRequestId && request.TryGetValue("error", out var errorResponse))
7070
{
71-
return new Response(requestId, errorResponse.ToString());
71+
// TODO: this doesn't seem right.
72+
return new ServerError(requestId, errorResponse);
7273
}
7374

7475
var method = request["method"]?.Value<string>();

src/JsonRpc/RequestRouter.cs

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using System;
2+
using System.Linq;
23
using System.Reflection;
34
using System.Threading;
45
using System.Threading.Tasks;
@@ -23,7 +24,7 @@ public IDisposable Add(IJsonRpcHandler handler)
2324

2425
public async void RouteNotification(Notification notification)
2526
{
26-
var handler = _collection.Get(notification.Method);
27+
var handler = _collection.FirstOrDefault(x => x.Method == notification.Method);
2728

2829
Task result;
2930
if (handler.Params is null)
@@ -46,16 +47,14 @@ public Task<ErrorResponse> RouteRequest(Request request)
4647

4748
protected virtual async Task<ErrorResponse> RouteRequest(Request request, CancellationToken token)
4849
{
49-
var handler = _collection.Get(request.Method);
50-
51-
var method = _collection.Get(request.Method);
52-
if (method is null)
50+
var handler = _collection.FirstOrDefault(x => x.Method == request.Method);
51+
if (request.Method is null)
5352
{
54-
return new MethodNotFound(request.Id);
53+
return new MethodNotFound(request.Id, request.Method);
5554
}
5655

5756
Task result;
58-
if (method.Params is null)
57+
if (handler.Params is null)
5958
{
6059
result = ReflectionRequestHandlers.HandleRequest(handler, token);
6160
}
@@ -64,7 +63,7 @@ protected virtual async Task<ErrorResponse> RouteRequest(Request request, Cancel
6463
object @params;
6564
try
6665
{
67-
@params = request.Params.ToObject(method.Params);
66+
@params = request.Params.ToObject(handler.Params);
6867
}
6968
catch
7069
{
@@ -89,4 +88,4 @@ protected virtual async Task<ErrorResponse> RouteRequest(Request request, Cancel
8988
return new Client.Response(request.Id, responseValue);
9089
}
9190
}
92-
}
91+
}

src/JsonRpc/RpcError.cs

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using Newtonsoft.Json;
2+
using Newtonsoft.Json.Serialization;
3+
using OmniSharp.Extensions.JsonRpc.Server.Messages;
4+
5+
namespace OmniSharp.Extensions.JsonRpc
6+
{
7+
8+
[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy)), JsonConverter(typeof(RpcErrorConverter))]
9+
public class RpcError<T>
10+
{
11+
public RpcError(object id, ErrorMessage<T> message) : this(id, message, "2.0")
12+
{
13+
}
14+
15+
[JsonConstructor]
16+
public RpcError(object id, ErrorMessage<T> message, string protocolVersion)
17+
{
18+
Id = id;
19+
Error = message;
20+
ProtocolVersion = protocolVersion;
21+
}
22+
23+
public string ProtocolVersion { get; set; }
24+
25+
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
26+
public object Id { get; }
27+
28+
public ErrorMessage<T> Error { get; }
29+
}
30+
31+
public class RpcError : RpcError<object>
32+
{
33+
public RpcError(object id, ErrorMessage<object> message) : this(id, message, "2.0")
34+
{
35+
}
36+
37+
[JsonConstructor]
38+
public RpcError(object id, ErrorMessage<object> message, string protocolVersion) : base(id, message, protocolVersion)
39+
{
40+
}
41+
}
42+
}

src/JsonRpc/RpcErrorConverter.cs

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using System.Reflection;
3+
using Newtonsoft.Json;
4+
using Newtonsoft.Json.Linq;
5+
using OmniSharp.Extensions.JsonRpc.Server.Messages;
6+
7+
namespace OmniSharp.Extensions.JsonRpc
8+
{
9+
public class RpcErrorConverter : JsonConverter
10+
{
11+
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
12+
{
13+
throw new NotImplementedException();
14+
}
15+
16+
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
17+
{
18+
var obj = JObject.Load(reader);
19+
20+
var messageDataType = objectType == typeof(RpcError)
21+
? typeof(object)
22+
: objectType.GetTypeInfo().GetGenericArguments()[0];
23+
24+
object requestId = null;
25+
if (obj.TryGetValue("id", out var id))
26+
{
27+
var idString = id.Type == JTokenType.String ? (string)id : null;
28+
var idLong = id.Type == JTokenType.Integer ? (long?)id : null;
29+
requestId = idString ?? (idLong.HasValue ? (object)idLong.Value : null);
30+
}
31+
32+
object data = null;
33+
if (obj.TryGetValue("error", out var dataToken))
34+
{
35+
var errorMessageType = typeof(ErrorMessage<>).MakeGenericType(messageDataType);
36+
data = dataToken.ToObject(errorMessageType);
37+
}
38+
39+
return Activator.CreateInstance(objectType, requestId, data, obj["protocolVersion"].ToString());
40+
}
41+
42+
public override bool CanConvert(Type objectType)
43+
{
44+
return objectType == typeof(RpcError) ||
45+
(objectType.GetTypeInfo().IsGenericType && objectType.GetTypeInfo().GetGenericTypeDefinition() == typeof(RpcError<>));
46+
}
47+
48+
public override bool CanWrite { get; } = false;
49+
public override bool CanRead { get; } = true;
50+
}
51+
}

0 commit comments

Comments
 (0)