Skip to content

Commit 9869b91

Browse files
Fixed error message not being formatted correctly
1 parent 545fffb commit 9869b91

30 files changed

+163
-78
lines changed

src/JsonRpc/Client/Response.cs

+14-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)
@@ -16,10 +17,20 @@ public Response(object id, object result)
1617
Result = result;
1718
}
1819

20+
public Response(object id, RpcError result)
21+
{
22+
Id = id;
23+
Result = result;
24+
}
25+
1926
public string ProtocolVersion { get; set; } = "2.0";
2027

2128
public object Id { get; set; }
2229

30+
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
2331
public object Result { get; set; }
32+
33+
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
34+
public RpcError Error { get; set; }
2435
}
25-
}
36+
}

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/InputHandler.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
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;
89
using OmniSharp.Extensions.JsonRpc.Server.Messages;
910

1011
namespace OmniSharp.Extensions.JsonRpc
@@ -153,7 +154,7 @@ private void HandleRequest(string request)
153154
}
154155
else
155156
{
156-
tcs.SetException(new Exception(response.Error));
157+
tcs.SetException(new Exception(JsonConvert.SerializeObject(response.Error)));
157158
}
158159
}
159160

src/JsonRpc/Reciever.cs

+3-2
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;
@@ -68,7 +68,8 @@ protected virtual Renor GetRenor(JToken @object)
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 RpcError(requestId, new ErrorMessage<object>(-1337, "Unknown error response", errorResponse));
7273
}
7374

7475
var method = request["method"]?.Value<string>();
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
using Newtonsoft.Json;
1+
using Newtonsoft.Json;
22
using Newtonsoft.Json.Serialization;
33
using OmniSharp.Extensions.JsonRpc.Server.Messages;
44

55
namespace OmniSharp.Extensions.JsonRpc
66
{
77

8-
[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
9-
public class Error<T>
8+
[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy)), JsonConverter(typeof(RpcErrorConverter))]
9+
public class RpcError<T>
1010
{
11-
public Error(object id, ErrorMessage<T> message) : this(id, message, "2.0")
11+
public RpcError(object id, ErrorMessage<T> message) : this(id, message, "2.0")
1212
{
1313
}
1414

1515
[JsonConstructor]
16-
public Error(object id, ErrorMessage<T> message, string protocolVersion)
16+
public RpcError(object id, ErrorMessage<T> message, string protocolVersion)
1717
{
1818
Id = id;
19-
Message = message;
19+
Error = message;
2020
ProtocolVersion = protocolVersion;
2121
}
2222

@@ -26,18 +26,18 @@ public Error(object id, ErrorMessage<T> message, string protocolVersion)
2626
public object Id { get; }
2727

2828
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
29-
public ErrorMessage<T> Message { get; }
29+
public ErrorMessage<T> Error { get; }
3030
}
3131

32-
public class Error : Error<object>
32+
public class RpcError : RpcError<object>
3333
{
34-
public Error(object id, ErrorMessage<object> message) : this(id, message, "2.0")
34+
public RpcError(object id, ErrorMessage<object> message) : this(id, message, "2.0")
3535
{
3636
}
3737

3838
[JsonConstructor]
39-
public Error(object id, ErrorMessage<object> message, string protocolVersion) : base(id, message, protocolVersion)
39+
public RpcError(object id, ErrorMessage<object> message, string protocolVersion) : base(id, message, protocolVersion)
4040
{
4141
}
4242
}
43-
}
43+
}

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+
}

src/JsonRpc/Server/Messages/ErrorMessage.cs

+16-2
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,32 @@
33

44
namespace OmniSharp.Extensions.JsonRpc.Server.Messages
55
{
6+
public interface IErrorMessage
7+
{
8+
int Code { get; }
9+
10+
string Message { get; }
11+
12+
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
13+
object Data { get; }
14+
}
15+
616
public class ErrorMessage: ErrorMessage<object>
717
{
818
public ErrorMessage(int code, string message) : base(code, message, null)
919
{
1020
}
1121

22+
[JsonConstructor]
1223
public ErrorMessage(int code, string message, object data) : base(code, message, data)
1324
{
1425
}
1526
}
1627

1728
[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
18-
public class ErrorMessage<T>
29+
public class ErrorMessage<T> : IErrorMessage
1930
{
31+
[JsonConstructor]
2032
public ErrorMessage(int code, string message, T data)
2133
{
2234
Code = code;
@@ -30,5 +42,7 @@ public ErrorMessage(int code, string message, T data)
3042

3143
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
3244
public T Data { get; }
45+
46+
object IErrorMessage.Data => Data;
3347
}
34-
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
namespace OmniSharp.Extensions.JsonRpc.Server.Messages
22
{
3-
public class InternalError : Error
3+
public class InternalError : RpcError
44
{
55
public InternalError() : this(null) { }
66
public InternalError(object id) : base(id, new ErrorMessage(-32602, "Internal Error")) { }
77
}
8-
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
namespace OmniSharp.Extensions.JsonRpc.Server.Messages
22
{
3-
public class InvalidParams : Error
3+
public class InvalidParams : RpcError
44
{
55
public InvalidParams() : this(null) { }
66
public InvalidParams(object id) : base(id, new ErrorMessage(-32602, "Invalid params")) { }
77
}
8-
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
namespace OmniSharp.Extensions.JsonRpc.Server.Messages
22
{
3-
public class InvalidRequest : Error
3+
public class InvalidRequest : RpcError
44
{
55

66
public InvalidRequest() : base(null, new ErrorMessage(-32600, $"Invalid Request")) { }
77
public InvalidRequest(object id) : base(id, new ErrorMessage(-32600, $"Invalid Request")) { }
88
public InvalidRequest(string message) : base(null, new ErrorMessage(-32600, $"Invalid Request - {message}")) { }
99
public InvalidRequest(object id, string message) : base(id, new ErrorMessage(-32600, $"Invalid Request - {message}")) { }
1010
}
11-
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace OmniSharp.Extensions.JsonRpc.Server.Messages
22
{
3-
public class MethodNotFound : Error
3+
public class MethodNotFound : RpcError
44
{
55
public MethodNotFound(object id) : base(id, new ErrorMessage(-32601, "Method not found")) { }
66
}
7-
}
7+
}
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
namespace OmniSharp.Extensions.JsonRpc.Server.Messages
22
{
3-
public class ParseError : Error
3+
public class ParseError : RpcError
44
{
55
public ParseError() : this(null) { }
66
public ParseError(object id) : base(id, new ErrorMessage(-32700, "Parse Error")) { }
77
}
8-
}
8+
}

src/JsonRpc/Server/Renor.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace OmniSharp.Extensions.JsonRpc.Server
1+
namespace OmniSharp.Extensions.JsonRpc.Server
22
{
33
/// <summary>
44
/// Request, Error, Notification or Response
@@ -22,7 +22,7 @@ internal Renor(Request request)
2222
Response = null;
2323
}
2424

25-
internal Renor(Error errorMessage)
25+
internal Renor(RpcError errorMessage)
2626
{
2727
Notification = null;
2828
Request = null;
@@ -45,7 +45,7 @@ internal Renor(Response response)
4545
public Request Request { get; }
4646

4747
public bool IsError => Error != null;
48-
public Error Error { get; }
48+
public RpcError Error { get; }
4949

5050
public bool IsResponse => Response != null;
5151
public Response Response { get; }
@@ -60,7 +60,7 @@ public static implicit operator Renor(Request request)
6060
return new Renor(request);
6161
}
6262

63-
public static implicit operator Renor(Error error)
63+
public static implicit operator Renor(RpcError error)
6464
{
6565
return new Renor(error);
6666
}
@@ -70,4 +70,4 @@ public static implicit operator Renor(Response response)
7070
return new Renor(response);
7171
}
7272
}
73-
}
73+
}

src/JsonRpc/Server/Response.cs

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using Newtonsoft.Json.Linq;
1+
using Newtonsoft.Json;
2+
using Newtonsoft.Json.Linq;
3+
using OmniSharp.Extensions.JsonRpc.Server.Messages;
24

35
namespace OmniSharp.Extensions.JsonRpc.Server
46
{
@@ -10,7 +12,7 @@ public Response(object id, JToken result)
1012
Result = result;
1113
}
1214

13-
public Response(object id, string error)
15+
public Response(object id, IErrorMessage error)
1416
{
1517
Id = id;
1618
Error = error;
@@ -20,8 +22,10 @@ public Response(object id, string error)
2022

2123
public object Id { get; set; }
2224

25+
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
2326
public JToken Result { get; set; }
2427

25-
public string Error { get; set; }
28+
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
29+
public IErrorMessage Error { get; set; }
2630
}
27-
}
31+
}

0 commit comments

Comments
 (0)