Skip to content

Commit ea2b466

Browse files
committed
Extract simple error prefix into computed variable
Signed-off-by: Moritz Lang <[email protected]>
1 parent e14fc54 commit ea2b466

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

Sources/Valkey/Connection/ValkeyConnection.swift

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -205,15 +205,12 @@ public final actor ValkeyConnection: ValkeyClientProtocol, Sendable {
205205
}
206206
} catch let error as ValkeyClientError {
207207
#if DistributedTracingSupport
208-
span?.recordError(error)
209-
if let message = error.message {
210-
var prefixEndIndex = message.startIndex
211-
while prefixEndIndex < message.endIndex, message[prefixEndIndex] != " " {
212-
message.formIndex(after: &prefixEndIndex)
208+
if let span {
209+
span.recordError(error)
210+
span.setStatus(SpanStatus(code: .error))
211+
if let prefix = error.simpleErrorPrefix {
212+
span.attributes["db.response.status_code"] = "\(prefix)"
213213
}
214-
let prefix = message[message.startIndex..<prefixEndIndex]
215-
span?.attributes["db.response.status_code"] = "\(prefix)"
216-
span?.setStatus(SpanStatus(code: .error))
217214
}
218215
#endif
219216
throw error
@@ -461,3 +458,20 @@ struct AutoIncrementingInteger {
461458
return value - 1
462459
}
463460
}
461+
462+
#if DistributedTracingSupport
463+
extension ValkeyClientError {
464+
/// Extract the simple error prefix from this error.
465+
///
466+
/// - SeeAlso: [](https://valkey.io/topics/protocol/#simple-errors)
467+
@usableFromInline
468+
var simpleErrorPrefix: Substring? {
469+
guard let message else { return nil }
470+
var prefixEndIndex = message.startIndex
471+
while prefixEndIndex < message.endIndex, message[prefixEndIndex] != " " {
472+
message.formIndex(after: &prefixEndIndex)
473+
}
474+
return message[message.startIndex..<prefixEndIndex]
475+
}
476+
}
477+
#endif

0 commit comments

Comments
 (0)