Rename Error to MCPError (#44)
* Rename Error to MCPError * Conform MCPError to Swift.Error * Use unqualified Error instead of Swift.Error * Use unqualified MCPError instead of MCP.MCPError * Rename file back to Error.swift * Introduce top-level MCP namespace with nested Error typealias to provide deprecation notice for existing usage of MCP.Error * Fix markup for MCP enum warning
This commit is contained in:
parent
a9bcbf9ad9
commit
09da2a1c01
5 changed files with 108 additions and 88 deletions
|
|
@ -90,8 +90,8 @@ struct ClientTests {
|
|||
do {
|
||||
try await client.connect(transport: transport)
|
||||
#expect(Bool(false), "Expected connection to fail")
|
||||
} catch let error as Error {
|
||||
if case Error.transportError = error {
|
||||
} catch let error as MCPError {
|
||||
if case MCPError.transportError = error {
|
||||
#expect(Bool(true))
|
||||
} else {
|
||||
#expect(Bool(false), "Expected transport error")
|
||||
|
|
@ -112,8 +112,8 @@ struct ClientTests {
|
|||
do {
|
||||
try await client.ping()
|
||||
#expect(Bool(false), "Expected ping to fail")
|
||||
} catch let error as Error {
|
||||
if case Error.transportError = error {
|
||||
} catch let error as MCPError {
|
||||
if case MCPError.transportError = error {
|
||||
#expect(Bool(true))
|
||||
} else {
|
||||
#expect(Bool(false), "Expected transport error")
|
||||
|
|
@ -132,12 +132,12 @@ struct ClientTests {
|
|||
try await client.connect(transport: transport)
|
||||
|
||||
// Create a task for listPrompts
|
||||
let promptsTask = Task<Void, Swift.Error> {
|
||||
let promptsTask = Task<Void, Error> {
|
||||
do {
|
||||
_ = try await client.listPrompts()
|
||||
#expect(Bool(false), "Expected listPrompts to fail in strict mode")
|
||||
} catch let error as Error {
|
||||
if case Error.methodNotFound = error {
|
||||
} catch let error as MCPError {
|
||||
if case MCPError.methodNotFound = error {
|
||||
#expect(Bool(true))
|
||||
} else {
|
||||
#expect(Bool(false), "Expected methodNotFound error, got \(error)")
|
||||
|
|
@ -185,7 +185,7 @@ struct ClientTests {
|
|||
// Create an error response with the same ID
|
||||
let errorResponse = Response<ListPrompts>(
|
||||
id: decodedRequest.id,
|
||||
error: Error.methodNotFound("Test: Prompts capability not available")
|
||||
error: MCPError.methodNotFound("Test: Prompts capability not available")
|
||||
)
|
||||
try await transport.queue(response: errorResponse)
|
||||
|
||||
|
|
@ -193,8 +193,8 @@ struct ClientTests {
|
|||
do {
|
||||
_ = try await client.listPrompts()
|
||||
#expect(Bool(false), "Expected listPrompts to fail in non-strict mode")
|
||||
} catch let error as Error {
|
||||
if case Error.methodNotFound = error {
|
||||
} catch let error as MCPError {
|
||||
if case MCPError.methodNotFound = error {
|
||||
#expect(Bool(true))
|
||||
} else {
|
||||
#expect(Bool(false), "Expected methodNotFound error, got \(error)")
|
||||
|
|
|
|||
|
|
@ -1,4 +1,8 @@
|
|||
import Foundation
|
||||
import class Foundation.JSONEncoder
|
||||
import class Foundation.JSONDecoder
|
||||
import struct Foundation.Data
|
||||
import struct Foundation.POSIXError
|
||||
|
||||
import Logging
|
||||
|
||||
@testable import MCP
|
||||
|
|
@ -26,7 +30,7 @@ actor MockTransport: Transport {
|
|||
private var dataToReceive: [Data] = []
|
||||
private(set) var receivedMessages: [String] = []
|
||||
|
||||
private var dataStreamContinuation: AsyncThrowingStream<Data, Swift.Error>.Continuation?
|
||||
private var dataStreamContinuation: AsyncThrowingStream<Data, Error>.Continuation?
|
||||
|
||||
var shouldFailConnect = false
|
||||
var shouldFailSend = false
|
||||
|
|
@ -37,7 +41,7 @@ actor MockTransport: Transport {
|
|||
|
||||
public func connect() async throws {
|
||||
if shouldFailConnect {
|
||||
throw Error.transportError(POSIXError(.ECONNREFUSED))
|
||||
throw MCPError.transportError(POSIXError(.ECONNREFUSED))
|
||||
}
|
||||
isConnected = true
|
||||
}
|
||||
|
|
@ -50,13 +54,13 @@ actor MockTransport: Transport {
|
|||
|
||||
public func send(_ message: Data) async throws {
|
||||
if shouldFailSend {
|
||||
throw Error.transportError(POSIXError(.EIO))
|
||||
throw MCPError.transportError(POSIXError(.EIO))
|
||||
}
|
||||
sentData.append(message)
|
||||
}
|
||||
|
||||
public func receive() -> AsyncThrowingStream<Data, Swift.Error> {
|
||||
return AsyncThrowingStream<Data, Swift.Error> { continuation in
|
||||
public func receive() -> AsyncThrowingStream<Data, Error> {
|
||||
return AsyncThrowingStream<Data, Error> { continuation in
|
||||
dataStreamContinuation = continuation
|
||||
for message in dataToReceive {
|
||||
continuation.yield(message)
|
||||
|
|
@ -76,7 +80,7 @@ actor MockTransport: Transport {
|
|||
shouldFailSend = shouldFail
|
||||
}
|
||||
|
||||
func queue<M: MCP.Method>(request: Request<M>) throws {
|
||||
func queue<M: Method>(request: Request<M>) throws {
|
||||
let data = try encoder.encode(request)
|
||||
if let continuation = dataStreamContinuation {
|
||||
continuation.yield(data)
|
||||
|
|
@ -85,12 +89,12 @@ actor MockTransport: Transport {
|
|||
}
|
||||
}
|
||||
|
||||
func queue<M: MCP.Method>(response: Response<M>) throws {
|
||||
func queue<M: Method>(response: Response<M>) throws {
|
||||
let data = try encoder.encode(response)
|
||||
dataToReceive.append(data)
|
||||
}
|
||||
|
||||
func queue<N: MCP.Notification>(notification: Message<N>) throws {
|
||||
func queue<N: Notification>(notification: Message<N>) throws {
|
||||
let data = try encoder.encode(notification)
|
||||
dataToReceive.append(data)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ struct ResponseTests {
|
|||
@Test("Error response initialization and encoding")
|
||||
func testErrorResponse() throws {
|
||||
let id: ID = "test-id"
|
||||
let error = MCP.Error.parseError(nil)
|
||||
let error = MCPError.parseError(nil)
|
||||
let response = Response<TestMethod>(id: id, error: error)
|
||||
|
||||
let encoder = JSONEncoder()
|
||||
|
|
@ -65,7 +65,7 @@ struct ResponseTests {
|
|||
@Test("Error response with detail")
|
||||
func testErrorResponseWithDetail() throws {
|
||||
let id: ID = "test-id"
|
||||
let error = MCP.Error.parseError("Invalid syntax")
|
||||
let error = MCPError.parseError("Invalid syntax")
|
||||
let response = Response<TestMethod>(id: id, error: error)
|
||||
|
||||
let encoder = JSONEncoder()
|
||||
|
|
|
|||
|
|
@ -109,7 +109,7 @@ struct ServerTests {
|
|||
|
||||
try await server.start(transport: transport) { clientInfo, _ in
|
||||
if clientInfo.name == "BlockedClient" {
|
||||
throw Error.invalidRequest("Client not allowed")
|
||||
throw MCPError.invalidRequest("Client not allowed")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -57,7 +57,7 @@ struct StdioTransportTests {
|
|||
try writer.close()
|
||||
|
||||
// Start receiving messages
|
||||
let stream: AsyncThrowingStream<Data, Swift.Error> = await transport.receive()
|
||||
let stream: AsyncThrowingStream<Data, Error> = await transport.receive()
|
||||
var iterator = stream.makeAsyncIterator()
|
||||
|
||||
// Get first message
|
||||
|
|
@ -79,7 +79,7 @@ struct StdioTransportTests {
|
|||
try writer.writeAll(invalidJSON.data(using: .utf8)!)
|
||||
try writer.close()
|
||||
|
||||
let stream: AsyncThrowingStream<Data, Swift.Error> = await transport.receive()
|
||||
let stream: AsyncThrowingStream<Data, Error> = await transport.receive()
|
||||
var iterator = stream.makeAsyncIterator()
|
||||
|
||||
_ = try await iterator.next()
|
||||
|
|
@ -100,7 +100,7 @@ struct StdioTransportTests {
|
|||
try await transport.connect()
|
||||
#expect(Bool(false), "Expected connect to throw an error")
|
||||
} catch {
|
||||
#expect(error is MCP.Error)
|
||||
#expect(error is MCPError)
|
||||
}
|
||||
|
||||
await transport.disconnect()
|
||||
|
|
@ -119,7 +119,7 @@ struct StdioTransportTests {
|
|||
try await transport.connect()
|
||||
#expect(Bool(false), "Expected connect to throw an error")
|
||||
} catch {
|
||||
#expect(error is MCP.Error)
|
||||
#expect(error is MCPError)
|
||||
}
|
||||
|
||||
await transport.disconnect()
|
||||
|
|
|
|||
Loading…
Reference in a new issue