Skip to content

Synchronously send error response on message decoding failure #334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 16, 2020
Merged

Synchronously send error response on message decoding failure #334

merged 8 commits into from
Oct 16, 2020

Conversation

flowtoolz
Copy link
Contributor

  • Replaces two logs since those don't result in response messages anymore
  • Adds a func for sending messages synchronously, to get the two messages out before fatalError

@flowtoolz
Copy link
Contributor Author

@swift-ci please test

1 similar comment
@MaxDesiatov
Copy link
Contributor

@swift-ci please test

@@ -229,17 +233,19 @@ public final class JSONRPCConnection {
continue MESSAGE_LOOP
}
case .unknown:
log("error decoding message: \(error.message)", level: .error)
send(.errorResponse(ResponseError(error), id: nil),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't test this behaviour directly due to the fatalError, but it might be good to add a test for the JSONRPCConnection receiving an error with nil id. For example, if you manually send an errorResponse(..., id: nil) make sure it doesn't crash the receiving connection. This would fit in Tests/LanguageServerProtocolJSONRPCTests/ConnectionTests.swift

Copy link
Contributor Author

@flowtoolz flowtoolz Oct 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this one I better ask you before going down the wrong rabbit hole ... I don't see an easy way to test that a JSONRPCMessage or a concrete error response actually reaches the receiver. I'd want to write somethin like this:

func testSendErrorResponseWithNilID() {
    let client = connection.client

    let expectation = self.expectation(description: "received message")
    client.handleNextMessage { (message: JSONRPCMessage) in
      expectation.fulfill()
    }
  
    connection.serverConnection._send(.errorResponse(.cancelled, id: nil))

    waitForExpectations(timeout: 10)
  }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see we don't have a good way to test this right now. Let's skip this.

@benlangmuir
Copy link
Contributor

@swift-ci please test

Copy link
Contributor

@benlangmuir benlangmuir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Please regenerate the tests for Linux (swift test --generate-linuxmain), and I think it will be ready to go :-)

@flowtoolz
Copy link
Contributor Author

Done. Thanks for the learning opportunity 😊

@benlangmuir
Copy link
Contributor

@swift-ci please test

@benlangmuir
Copy link
Contributor

16:01:54 Test Case '-[LanguageServerProtocolJSONRPCTests.CodingTests testMessageDecodingError]' started.
16:01:54 
/Users/buildnode/jenkins/workspace/swift-sourcekit-lsp-PR-macOS/branch-main/sourcekit-lsp/Tests/LanguageServerProtocolJSONRPCTests/CodingTests.swift:161: error: -[LanguageServerProtocolJSONRPCTests.CodingTests testMessageDecodingError] : failed - expected error not seen
16:01:54 Test Case '-[LanguageServerProtocolJSONRPCTests.CodingTests testMessageDecodingError]' failed (1.289 seconds).

@flowtoolz
Copy link
Contributor Author

flowtoolz commented Oct 16, 2020

Oh, I guess this is now a decodable error response message:

{"jsonrpc":"2.0","error":{"code":-32000,"message":""}}

So decoding that produces no error in CodingTests.swift:161.

If the test was only intended to detect the missing ID, I guess I should remove the test?

@benlangmuir
Copy link
Contributor

Sounds good to me

@benlangmuir
Copy link
Contributor

@swift-ci please test

@flowtoolz
Copy link
Contributor Author

done. this time, regenerating the Linux tests had no effect, just so you know 🤷🏼‍♂️

@benlangmuir benlangmuir merged commit 7a36a51 into swiftlang:main Oct 16, 2020
aaditya-chandrasekhar pushed a commit to val-verde/sourcekit-lsp that referenced this pull request Nov 14, 2020
…ang#334)

* Replaces two logs since those don't result in response messages anymore
* Adds a func for sending messages synchronously, to get the two messages out before fatalError
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants