-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDevToolsClientError.swift
More file actions
308 lines (299 loc) · 12.6 KB
/
DevToolsClientError.swift
File metadata and controls
308 lines (299 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import Foundation
enum DevToolsClientError: Error, Equatable {
case runNotFound(String)
case frameOutOfRange(Int)
case invalidRunId(String)
case invalidNodeId(String)
case invalidIteration(Int?)
case invalidFrameNo(Int?)
case seqOutOfRange(Int)
case nodeNotFound(String)
case attemptNotFound(String)
case attemptNotFinished
case nodeHasNoOutput
case iterationNotFound(Int)
case malformedOutputRow
case payloadTooLarge(Int?)
case confirmationRequired
case busy
case unsupportedSandbox(String?)
case vcsError(String?)
case workingTreeDirty(String?)
case rewindFailed(String?)
case diffTooLarge(String?)
case rateLimited
case backpressureDisconnect
case network(URLError)
case malformedEvent(String)
case unknown(String)
var displayMessage: String {
switch self {
case .runNotFound(let runId):
return "Run not found: \(runId)"
case .frameOutOfRange(let frame):
return "Frame out of range: \(frame)"
case .invalidRunId(let runId):
return "Invalid run ID: \(runId)"
case .invalidNodeId(let nodeId):
return "Invalid node ID: \(nodeId)"
case .invalidIteration(let iteration):
if let iteration {
return "Invalid iteration: \(iteration)"
}
return "Invalid iteration"
case .invalidFrameNo(let frame):
if let frame {
return "Invalid frame number: \(frame)"
}
return "Invalid frame number"
case .seqOutOfRange(let seq):
return "Sequence out of range: \(seq)"
case .nodeNotFound(let nodeRef):
return "Node not found: \(nodeRef)"
case .attemptNotFound(let attemptRef):
return "Attempt not found: \(attemptRef)"
case .attemptNotFinished:
return "Attempt is still running."
case .nodeHasNoOutput:
return "This node has no output table."
case .iterationNotFound(let iteration):
if iteration >= 0 {
return "Iteration not found: \(iteration)"
}
return "Iteration not found."
case .malformedOutputRow:
return "Output row is malformed and cannot be rendered."
case .payloadTooLarge(let bytes):
if let bytes, bytes > 0 {
return "Output payload is too large (\(bytes) bytes)."
}
return "Output payload is too large."
case .confirmationRequired:
return "Rewind confirmation required."
case .busy:
return "Another rewind is in progress."
case .unsupportedSandbox(let reason):
if let reason, !reason.isEmpty {
return reason
}
return "This run uses a sandbox that cannot be rewound."
case .vcsError(let reason):
if let reason, !reason.isEmpty {
return reason
}
return "Failed due to a VCS error."
case .workingTreeDirty(let reason):
if let reason, !reason.isEmpty {
return reason
}
return "Working copy is dirty and cannot be diffed safely."
case .rewindFailed(let reason):
if let reason, !reason.isEmpty {
return reason
}
return "Rewind failed and rollback was partial."
case .diffTooLarge(let reason):
if let reason, !reason.isEmpty {
return reason
}
return "Diff is too large to render."
case .rateLimited:
return "Rewind rate limit exceeded."
case .backpressureDisconnect:
return "Disconnected due to backpressure"
case .network(let urlError):
return "Network error: \(urlError.localizedDescription)"
case .malformedEvent(let detail):
return "Malformed event: \(detail)"
case .unknown(let code):
return "Unknown error: \(code)"
}
}
var hint: String? {
switch self {
case .runNotFound:
return "Check that the run ID is correct and the run has not been deleted."
case .frameOutOfRange:
return "The requested frame may no longer exist. Try the latest frame."
case .invalidRunId:
return "Run IDs must match [a-z0-9_-]{1,64}."
case .invalidNodeId:
return "Node IDs must match [a-zA-Z0-9:_-]{1,128}."
case .invalidIteration:
return "Iteration must be a non-negative i32."
case .invalidFrameNo:
return "Frame numbers must be non-negative integers."
case .seqOutOfRange:
return "Reconnect without afterSeq to resync."
case .nodeNotFound:
return "Select an existing node from the tree and retry."
case .attemptNotFound:
return "No attempt exists for that node iteration."
case .attemptNotFinished:
return "Wait for the task attempt to finish before opening Diff."
case .nodeHasNoOutput:
return "Run the task to completion before opening Output."
case .iterationNotFound:
return "Choose a valid iteration and retry."
case .malformedOutputRow:
return "The server returned malformed output data. Retry or inspect server logs."
case .payloadTooLarge:
return "Output exceeds the 100 MB limit. Narrow fields or query via CLI."
case .confirmationRequired:
return "Confirm the destructive action before submitting rewind."
case .busy:
return "Wait for the current rewind to finish and retry."
case .unsupportedSandbox:
return "This run cannot be rewound in-place. Use historical view-only mode."
case .vcsError:
return "Check repository health, then retry."
case .workingTreeDirty:
return "Resolve working copy conflicts or dirtiness, then retry."
case .rewindFailed:
return "The rewind did not complete cleanly. Retry or inspect server logs."
case .diffTooLarge:
return "Diff exceeds the 50 MB cap. Narrow the task or inspect via CLI."
case .rateLimited:
return "Too many rewinds recently. Wait and retry."
case .backpressureDisconnect:
return "The client fell behind. Reconnecting will resume from the latest state."
case .network:
return "Check your network connection and that the Smithers server is running."
case .malformedEvent:
return "A corrupted event was received. The stream will attempt to resync."
case .unknown:
return nil
}
}
static func from(serverErrorCode code: String, message: String? = nil) -> DevToolsClientError {
switch code {
case "RunNotFound":
return .runNotFound(message ?? code)
case "FrameOutOfRange":
if let msg = message, let frame = Int(msg) {
return .frameOutOfRange(frame)
}
return .frameOutOfRange(-1)
case "InvalidRunId":
return .invalidRunId(message ?? code)
case "InvalidNodeId":
return .invalidNodeId(message ?? code)
case "InvalidIteration":
return .invalidIteration(firstInteger(in: message))
case "InvalidFrameNo":
if let msg = message, let frame = Int(msg) {
return .invalidFrameNo(frame)
}
return .invalidFrameNo(nil)
case "SeqOutOfRange":
if let msg = message, let seq = Int(msg) {
return .seqOutOfRange(seq)
}
return .seqOutOfRange(-1)
case "NodeNotFound":
return .nodeNotFound(message ?? "unknown")
case "AttemptNotFound":
return .attemptNotFound(message ?? "unknown")
case "AttemptNotFinished":
return .attemptNotFinished
case "NodeHasNoOutput":
return .nodeHasNoOutput
case "IterationNotFound":
return .iterationNotFound(firstInteger(in: message) ?? -1)
case "MalformedOutputRow":
return .malformedOutputRow
case "PayloadTooLarge":
return .payloadTooLarge(firstInteger(in: message))
case "ConfirmationRequired":
return .confirmationRequired
case "Busy":
return .busy
case "UnsupportedSandbox":
return .unsupportedSandbox(message)
case "VcsError":
return .vcsError(message)
case "WorkingTreeDirty":
return .workingTreeDirty(message)
case "RewindFailed":
return .rewindFailed(message)
case "DiffTooLarge":
return .diffTooLarge(message)
case "RateLimited":
return .rateLimited
case "BackpressureDisconnect":
return .backpressureDisconnect
default:
return .unknown(code)
}
}
private static func firstInteger(in message: String?) -> Int? {
guard let message else { return nil }
var buffer = ""
for char in message {
if char.isNumber || (char == "-" && buffer.isEmpty) {
buffer.append(char)
} else if !buffer.isEmpty {
return Int(buffer)
}
}
return buffer.isEmpty ? nil : Int(buffer)
}
static func from(urlError: URLError) -> DevToolsClientError {
.network(urlError)
}
// libsmithers formats anyerror via `{}` as `error.<Name>`, wrapped by the
// ffi layer as `client call: error.<Name>`. Parse the suffix so GUI
// surfaces map known codes (e.g. AttemptNotFinished) to typed errors
// instead of the opaque `.unknown("api(\"…\")")` fallback.
static func from(libsmithersMessage message: String) -> DevToolsClientError? {
let marker = "error."
guard let range = message.range(of: marker, options: .backwards) else { return nil }
let tail = message[range.upperBound...]
var code = ""
for character in tail {
if character.isLetter || character.isNumber {
code.append(character)
} else {
break
}
}
guard let first = code.first, first.isUppercase else { return nil }
let mapped = DevToolsClientError.from(serverErrorCode: code)
if case .unknown = mapped { return nil }
return mapped
}
static func from(decodingError: DecodingError) -> DevToolsClientError {
.malformedEvent(String(describing: decodingError))
}
static func == (lhs: DevToolsClientError, rhs: DevToolsClientError) -> Bool {
switch (lhs, rhs) {
case (.runNotFound(let l), .runNotFound(let r)): return l == r
case (.frameOutOfRange(let l), .frameOutOfRange(let r)): return l == r
case (.invalidRunId(let l), .invalidRunId(let r)): return l == r
case (.invalidNodeId(let l), .invalidNodeId(let r)): return l == r
case (.invalidIteration(let l), .invalidIteration(let r)): return l == r
case (.invalidFrameNo(let l), .invalidFrameNo(let r)): return l == r
case (.seqOutOfRange(let l), .seqOutOfRange(let r)): return l == r
case (.nodeNotFound(let l), .nodeNotFound(let r)): return l == r
case (.attemptNotFound(let l), .attemptNotFound(let r)): return l == r
case (.attemptNotFinished, .attemptNotFinished): return true
case (.nodeHasNoOutput, .nodeHasNoOutput): return true
case (.iterationNotFound(let l), .iterationNotFound(let r)): return l == r
case (.malformedOutputRow, .malformedOutputRow): return true
case (.payloadTooLarge(let l), .payloadTooLarge(let r)): return l == r
case (.confirmationRequired, .confirmationRequired): return true
case (.busy, .busy): return true
case (.unsupportedSandbox(let l), .unsupportedSandbox(let r)): return l == r
case (.vcsError(let l), .vcsError(let r)): return l == r
case (.workingTreeDirty(let l), .workingTreeDirty(let r)): return l == r
case (.rewindFailed(let l), .rewindFailed(let r)): return l == r
case (.diffTooLarge(let l), .diffTooLarge(let r)): return l == r
case (.rateLimited, .rateLimited): return true
case (.backpressureDisconnect, .backpressureDisconnect): return true
case (.network(let l), .network(let r)): return l.code == r.code
case (.malformedEvent(let l), .malformedEvent(let r)): return l == r
case (.unknown(let l), .unknown(let r)): return l == r
default: return false
}
}
}