Skip to content

Commit

Permalink
Exception handler in middle layer catches exception thrown in HandleR…
Browse files Browse the repository at this point in the history
…equestAsync by sendRequest.
  • Loading branch information
allisterb committed Mar 16, 2024
1 parent 76a0922 commit b12ae6f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 29 deletions.
62 changes: 35 additions & 27 deletions src/Stratis.VS.StratisEVM/SolidityLanguageClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,49 +260,57 @@ public async Task HandleNotificationAsync(string methodName, JToken methodParam,

public async Task<JToken> HandleRequestAsync(string methodName, JToken methodParam, Func<JToken, Task<JToken>> sendRequest)
{
var resp = await sendRequest(methodParam);
Info("Request {req} {param}: {resp}", methodName, methodParam?.ToString() ?? "", resp?.ToString() ?? "(null)");
if (resp != null)
try
{
if (methodName == "textDocument/hover")
var resp = await sendRequest(methodParam);
Info("Request {req} {param}: {resp}", methodName, methodParam?.ToString() ?? "", resp?.ToString() ?? "(null)");
if (resp != null)
{
if (resp.Root != null && resp.Root["contents"] != null && resp.Root["contents"]["kind"] != null && resp.Root["contents"]["kind"].Value<string>() == "markdown")
if (methodName == "textDocument/hover")
{
Info("Replace hover markup contents with plaintext.");
resp.Root["contents"]["kind"] = JValue.CreateString("plaintext");
if (resp.Root["contents"]["value"] != null)
if (resp.Root != null && resp.Root["contents"] != null && resp.Root["contents"]["kind"] != null && resp.Root["contents"]["kind"].Value<string>() == "markdown")
{
resp.Root["contents"]["value"] = JValue.CreateString(resp.Root["contents"]["value"].Value<string>().Replace("### ", "").Replace("#", ""));
}
else
{
resp.Root["contents"]["value"] = JValue.CreateString("");
Info("Replace hover markup contents with plaintext.");
resp.Root["contents"]["kind"] = JValue.CreateString("plaintext");
if (resp.Root["contents"]["value"] != null)
{
resp.Root["contents"]["value"] = JValue.CreateString(resp.Root["contents"]["value"].Value<string>().Replace("### ", "").Replace("#", ""));
}
else
{
resp.Root["contents"]["value"] = JValue.CreateString("");
}
}
}
}
else if (methodName == "textDocument/completion")
{
if (resp.Root.Type == JTokenType.Array && resp.Root.HasValues)
else if (methodName == "textDocument/completion")
{
foreach (var f in resp.Root)
if (resp.Root.Type == JTokenType.Array && resp.Root.HasValues)
{
if (f != null && f["documentation"] != null && f["documentation"]["kind"] != null && f["documentation"]["value"] != null && f["documentation"]["kind"].Value<string>() == "markdown")
foreach (var f in resp.Root)
{
Info("Replace completion markup contents with plaintext.");
f["documentation"]["kind"] = JValue.CreateString("plaintext");
f["documentation"]["value"] = JValue.CreateString(f["documentation"]["value"].Value<string>().Replace("### ", "").Replace("#", ""));
if (f != null && f["documentation"] != null && f["documentation"]["kind"] != null && f["documentation"]["value"] != null && f["documentation"]["kind"].Value<string>() == "markdown")
{
Info("Replace completion markup contents with plaintext.");
f["documentation"]["kind"] = JValue.CreateString("plaintext");
f["documentation"]["value"] = JValue.CreateString(f["documentation"]["value"].Value<string>().Replace("### ", "").Replace("#", ""));
}
}
}

}

return resp;
}
else
{
Info("resp is null");
return JObject.FromObject(new { contents = new { kind = "plaintext", value = "" } });
}
return resp;
}
else
catch (Exception ex)
{
Info("resp is null");
Error(ex, "Exception thrown handling request {m}.", methodName);
return JObject.FromObject(new { contents = new { kind = "plaintext", value = "" } });
}
}
}
}
#endregion
Expand Down
5 changes: 3 additions & 2 deletions tests/solidity/test2/ReceivesEth.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ pragma solidity ^0.8.23;

contract ReceivesEth {
uint public amount = 1 ether;

uint public amount2 = 2 ether;

receive() external payable {
require(msg.value == amount, "Wrong amount");
require(msg.value == amount2, "Wrong amount");
}
}

0 comments on commit b12ae6f

Please sign in to comment.