Skip to content
This repository was archived by the owner on Jan 17, 2019. It is now read-only.

Commit 8d82476

Browse files
authored
Merge pull request #120 from JohnSundell/live-cli-improve-error-handling
Live CLI: Improve error handling
2 parents b4b033b + 26a631d commit 8d82476

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

live/sources/SocketClient.swift

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import Foundation
2525
class SocketClient {
2626
private let port: Int
2727
private var stream: OutputStream?
28+
private lazy var asyncQueue: DispatchQueue = .init(label: "SocketClient.async")
2829

2930
/**
3031
* Initialize an instance of this class
@@ -64,16 +65,36 @@ class SocketClient {
6465
return
6566
}
6667

68+
switch stream.streamStatus {
69+
case .notOpen:
70+
printError(message: "Stream is not open")
71+
case .opening, .writing:
72+
self.asyncQueue.async {
73+
self.send(data: data)
74+
}
75+
76+
return
77+
case .open, .reading, .atEnd:
78+
break
79+
case .closed:
80+
printError(message: "Stream has been closed")
81+
return
82+
case .error:
83+
printError(message: "An error occured. Make sure your app is running in the iOS Simulator")
84+
return
85+
}
86+
6787
if !stream.hasSpaceAvailable {
68-
printError()
88+
printError(message: "No space available in stream")
89+
return
6990
}
7091

7192
let result = data.withUnsafeBytes { bytes in
7293
return stream.write(bytes, maxLength: data.count)
7394
}
7495

7596
if result <= 0 {
76-
printError()
97+
printError(message: "No data could be written")
7798
}
7899
}
79100

@@ -99,7 +120,7 @@ class SocketClient {
99120
}
100121
}
101122

102-
private func printError() {
103-
"Could not send JSON data to application".print(withColor: .red)
123+
private func printError(message: String) {
124+
"Could not send JSON data to application. \(message).".print(withColor: .red)
104125
}
105126
}

0 commit comments

Comments
 (0)