Skip to content

Commit 71d7d29

Browse files
committed
Improve error thrown when port binding is not possible
1 parent 068aff0 commit 71d7d29

File tree

3 files changed

+56
-5
lines changed

3 files changed

+56
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ DerivedData/
1111
.env.*
1212
!.env.example
1313
Package.resolved
14+
.swiftpm

Sources/SwiftMCP/Transport/HTTPSSETransport.swift

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,29 @@ public final class HTTPSSETransport: Transport, @unchecked Sendable {
115115

116116
self.channel?.closeFuture.whenComplete { [logger] result in
117117
switch result {
118-
case .success:
119-
logger.info("Server channel closed normally")
120-
case .failure(let error):
121-
logger.error("Server channel closed with error: \(error)")
118+
case .success:
119+
logger.info("Server channel closed normally")
120+
case .failure(let error):
121+
logger.error("Server channel closed with error: \(error)")
122122
}
123123
}
124+
} catch let error as IOError {
125+
let errorMessage: String
126+
switch error.errnoCode {
127+
case EADDRINUSE:
128+
errorMessage = "Port \(port) is already in use. Please choose a different port or ensure no other service is using this port."
129+
case EACCES:
130+
errorMessage = "Permission denied to bind to port \(port). This port may require elevated privileges."
131+
case EADDRNOTAVAIL:
132+
errorMessage = "The address \(host) is not available for binding."
133+
default:
134+
errorMessage = "Failed to bind to \(host):\(port). Error: \(error.localizedDescription)"
135+
}
136+
logger.error("\(errorMessage)")
137+
throw TransportError.bindingFailed(errorMessage)
124138
} catch {
125139
logger.error("Server error: \(error)")
126-
throw error
140+
throw TransportError.bindingFailed(error.localizedDescription)
127141
}
128142
}
129143

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// TransportError.swift
3+
// SwiftMCP
4+
//
5+
// Created by Oliver Drobnik on 21.03.25.
6+
//
7+
8+
import Foundation
9+
10+
/**
11+
Errors that can occur during transport operations in SwiftMCP.
12+
13+
This enum provides specific error types and localized descriptions for various
14+
transport-related failures, such as binding failures when starting a server.
15+
*/
16+
public enum TransportError: LocalizedError {
17+
/**
18+
Indicates that the transport failed to bind to a specific address and port.
19+
20+
- Parameter message: A human-readable description of the binding failure,
21+
including details about the specific cause (e.g., port in use, permission denied).
22+
*/
23+
case bindingFailed(String)
24+
25+
/**
26+
Provides a localized description of the error.
27+
28+
- Returns: A human-readable string describing the error.
29+
*/
30+
public var errorDescription: String? {
31+
switch self {
32+
case .bindingFailed(let message):
33+
return message
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)