Skip to content

Return a .serverCancelled error code if the server cancels a request #674

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 1 commit into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/LanguageServerProtocol/Connection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ extension LocalConnection: Connection {
public func send<Request>(_ request: Request, queue: DispatchQueue, reply: @escaping (LSPResult<Request.Response>) -> Void) -> RequestID where Request: RequestType {
let id = nextRequestID()
guard let handler = handler else {
queue.async { reply(.failure(.cancelled)) }
queue.async { reply(.failure(.serverCancelled)) }
return id
}

Expand Down
4 changes: 3 additions & 1 deletion Sources/LanguageServerProtocol/Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ public struct ResponseError: Error, Codable, Hashable {
extension ResponseError {
// MARK: Convencience properties for common errors.

public static var cancelled: ResponseError = ResponseError(code: .cancelled, message: "request cancelled")
public static var cancelled: ResponseError = ResponseError(code: .cancelled, message: "request cancelled by client")

public static var serverCancelled: ResponseError = ResponseError(code: .serverCancelled, message: "request cancelled by server")

public static func workspaceNotOpen(_ uri: DocumentURI) -> ResponseError {
return ResponseError(code: .workspaceNotOpen, message: "No workspace containing '\(uri)' found")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ extension JSONRPCConnection: Connection {
let id = nextRequestID()

guard readyToSend() else {
reply(.failure(.cancelled))
reply(.failure(.serverCancelled))
return id
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/SourceKitLSP/Swift/CodeCompletion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ extension SwiftLanguageServer {
if req.params.context?.triggerKind == .triggerFromIncompleteCompletions {
guard let currentSession = currentCompletionSession else {
log("triggerFromIncompleteCompletions with no existing completion session", level: .warning)
return req.reply(.failure(.cancelled))
return req.reply(.failure(.serverCancelled))
}
guard currentSession.uri == snapshot.document.uri, currentSession.utf8StartOffset == offset else {
log("triggerFromIncompleteCompletions with incompatible completion session; expected \(currentSession.uri)@\(currentSession.utf8StartOffset), but got \(snapshot.document.uri)@\(offset)", level: .warning)
return req.reply(.failure(.cancelled))
return req.reply(.failure(.serverCancelled))
}
session = currentSession
} else {
Expand Down
4 changes: 2 additions & 2 deletions Sources/SourceKitLSP/Swift/CodeCompletionSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class CodeCompletionSession {
switch self.state {
case .closed, .opening(_):
// Don't try again.
completion(.failure(.cancelled))
completion(.failure(.serverCancelled))
case .open:
self._update(filterText: filterText, position: position, in: snapshot, options: options, completion: completion)
}
Expand Down Expand Up @@ -134,7 +134,7 @@ class CodeCompletionSession {
return completion(.failure(ResponseError(result.failure!)))
}
if case .closed = self.state {
return completion(.failure(.cancelled))
return completion(.failure(.serverCancelled))
}

self.state = .open
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ extension ResponseError {
public init(_ value: SKDError) {
switch value {
case .requestCancelled:
self = .cancelled
self = .serverCancelled
case .requestFailed(let desc):
self = .unknown("sourcekitd request failed: \(desc)")
case .requestInvalid(let desc):
Expand Down
2 changes: 1 addition & 1 deletion Sources/SourceKitLSP/Swift/SwiftLanguageServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ extension SwiftLanguageServer {
let position = req.params.position
cursorInfo(uri, position..<position) { result in
guard let cursorInfo: CursorInfo = result.success ?? nil else {
if let error = result.failure, error != .responseError(.cancelled) {
if let error = result.failure, error != .responseError(.serverCancelled) {
log("cursor info failed \(uri):\(position): \(error)", level: .warning)
}
return req.reply(nil)
Expand Down
4 changes: 2 additions & 2 deletions Tests/LanguageServerProtocolJSONRPCTests/CodingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ final class CodingTests: XCTestCase {
{
"error" : {
"code" : -32800,
"message" : "request cancelled"
"message" : "request cancelled by client"
},
"id" : 2,
"jsonrpc" : "2.0"
Expand All @@ -132,7 +132,7 @@ final class CodingTests: XCTestCase {
{
"error" : {
"code" : -32800,
"message" : "request cancelled"
"message" : "request cancelled by client"
},
"id" : null,
"jsonrpc" : "2.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ class ConnectionTests: XCTestCase {

client.send(EchoNotification(string: "hi"))
_ = client.send(EchoRequest(string: "yo")) { result in
XCTAssertEqual(result, .failure(ResponseError.cancelled))
XCTAssertEqual(result, .failure(ResponseError.serverCancelled))
expectation.fulfill()
}

Expand Down