Skip to content

Commit b0c7a45

Browse files
committed
Update errors to capture source
1 parent cd80235 commit b0c7a45

14 files changed

+74
-97
lines changed

Sources/MySQL/Connection/PacketParser.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ extension Packet {
194194
let length = try parser.parseUInt32()
195195

196196
guard let fieldType = Field.FieldType(rawValue: try parser.byte()) else {
197-
throw MySQLError(.invalidPacket)
197+
throw MySQLError(.invalidPacket, source: .capture())
198198
}
199199

200200
let flags = Field.Flags(rawValue: try parser.parseUInt16())

Sources/MySQL/Errors/Error.swift

Lines changed: 11 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Debugging
22

3-
public struct MySQLError : Swift.Error, Debuggable, Traceable, Helpable {
3+
public struct MySQLError : Swift.Error, Debuggable {
44
public var possibleCauses: [String] {
55
switch problem {
66
case .invalidCredentials:
@@ -75,32 +75,21 @@ public struct MySQLError : Swift.Error, Debuggable, Traceable, Helpable {
7575
}
7676

7777
/// Creates a new problem
78-
init(_ problem: Problem,
79-
file: String = #file,
80-
function: String = #function,
81-
line: UInt = #line,
82-
column: UInt = #column
78+
init(
79+
_ problem: Problem,
80+
source: SourceLocation
8381
) {
8482
self.stackTrace = MySQLError.makeStackTrace()
85-
self.file = file
86-
self.function = function
87-
self.line = line
88-
self.column = column
83+
self.sourceLocation = source
8984
self.problem = problem
9085
}
9186

9287
init(
9388
packet: Packet,
94-
file: String = #file,
95-
function: String = #function,
96-
line: UInt = #line,
97-
column: UInt = #column
89+
source: SourceLocation
9890
) {
9991
self.stackTrace = MySQLError.makeStackTrace()
100-
self.file = file
101-
self.function = function
102-
self.line = line
103-
self.column = column
92+
self.sourceLocation = source
10493

10594
var parser = Parser(packet: packet, position: 1)
10695

@@ -133,30 +122,15 @@ public struct MySQLError : Swift.Error, Debuggable, Traceable, Helpable {
133122
init(
134123
identifier: String,
135124
reason: String,
136-
file: String = #file,
137-
function: String = #function,
138-
line: UInt = #line,
139-
column: UInt = #column
125+
sourceLocation: SourceLocation
140126
) {
141127
self.problem = .other(identifier: identifier, reason: reason)
142128
self.stackTrace = MySQLError.makeStackTrace()
143-
self.file = file
144-
self.function = function
145-
self.line = line
146-
self.column = column
129+
self.sourceLocation = sourceLocation
147130
}
148131

149-
/// The file this occurred in
150-
public let file: String
151-
152-
/// The function this occurred from
153-
public let function: String
154-
155-
/// The line this occurred at
156-
public let line: UInt
157-
158-
/// The column this occurred at
159-
public let column: UInt
132+
/// The location in Swift code this error originated from
133+
public let sourceLocation: SourceLocation?
160134

161135
/// Which problem
162136
internal let problem: Problem

Sources/MySQL/Internals/Connection+Handshake.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ extension MySQLStateMachine {
128128
packet.sequenceId = self.sequenceId
129129
return packet
130130
} else {
131-
throw MySQLError(.invalidHandshake)
131+
throw MySQLError(.invalidHandshake, source: .capture())
132132
}
133133
}
134134

@@ -151,7 +151,7 @@ extension MySQLStateMachine {
151151
switch packet.payload.first {
152152
case 0xfe:
153153
if packet.payload.count == 0 {
154-
throw MySQLError(.invalidHandshake)
154+
throw MySQLError(.invalidHandshake, source: .capture())
155155
} else {
156156
var offset = 1
157157

@@ -164,13 +164,13 @@ extension MySQLStateMachine {
164164
let password = self.password,
165165
let mechanism = String(bytes: packet.payload[1..<offset], encoding: .utf8)
166166
else {
167-
throw MySQLError(.invalidHandshake)
167+
throw MySQLError(.invalidHandshake, source: .capture())
168168
}
169169

170170
switch mechanism {
171171
case "mysql_native_password":
172172
guard offset &+ 2 < packet.payload.count else {
173-
throw MySQLError(.invalidHandshake)
173+
throw MySQLError(.invalidHandshake, source: .capture())
174174
}
175175

176176
let hash = sha1Encrypted(from: password, seed: Array(packet.payload[(offset &+ 1)...]))
@@ -183,11 +183,11 @@ extension MySQLStateMachine {
183183
packet.sequenceId = self.sequenceId
184184
return packet
185185
default:
186-
throw MySQLError(.invalidHandshake)
186+
throw MySQLError(.invalidHandshake, source: .capture())
187187
}
188188
}
189189
case 0xff:
190-
throw MySQLError(packet: packet)
190+
throw MySQLError(packet: packet, source: .capture())
191191
default:
192192
// auth is finished, have the parser stream to the packet stream now
193193
return nil

Sources/MySQL/Internals/Handshake.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ extension Packet {
4646

4747
// Require decimal `10` to be the protocol version
4848
guard try parser.byte() == 10 else {
49-
throw MySQLError(.invalidHandshake)
49+
throw MySQLError(.invalidHandshake, source: .capture())
5050
}
5151

5252
// UTF-8
@@ -58,11 +58,11 @@ extension Packet {
5858
}
5959

6060
guard try parser.byte() == 0 else {
61-
throw MySQLError(.invalidHandshake)
61+
throw MySQLError(.invalidHandshake, source: .capture())
6262
}
6363

6464
guard let serverVersion = String(bytes: serverVersionBuffer, encoding: .utf8) else {
65-
throw MySQLError(.invalidHandshake)
65+
throw MySQLError(.invalidHandshake, source: .capture())
6666
}
6767

6868
// ID of the MySQL internal thread handling this connection
@@ -73,7 +73,7 @@ extension Packet {
7373

7474
// null terminator of the random seed
7575
guard try parser.byte() == 0 else {
76-
throw MySQLError(.invalidHandshake)
76+
throw MySQLError(.invalidHandshake, source: .capture())
7777
}
7878

7979
// capabilities + default collation
@@ -97,7 +97,7 @@ extension Packet {
9797
randomSeed.append(contentsOf: try parser.buffer(length: 12))
9898

9999
guard try parser.byte() == 0 else {
100-
throw MySQLError(.invalidHandshake)
100+
throw MySQLError(.invalidHandshake, source: .capture())
101101
}
102102

103103
if parser.position < payload.count &- 1 {

Sources/MySQL/Internals/MySQLStream.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ struct EOF {
9898
var parser = Parser(packet: packet)
9999

100100
guard try parser.byte() == 0xfe, packet.payload.count == 5 else {
101-
throw MySQLError(.invalidPacket)
101+
throw MySQLError(.invalidPacket, source: .capture())
102102
}
103103

104104
self.flags = try parser.parseUInt16()

Sources/MySQL/Internals/Task.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ struct StartHandshake: Task {
2020
// Incoming packet can be an error packet so fail fast
2121
// Do not silence the error!
2222
guard packet.payload.first != 0xff else {
23-
throw MySQLError(packet:packet)
23+
throw MySQLError(packet:packet, source: .capture())
2424
}
2525

2626
let handshake = try context.doHandshake(from: packet)
@@ -46,7 +46,7 @@ struct SendHandshake: Task {
4646
// Incoming packet can be an error packet so fail fast
4747
// Do not silence the error!
4848
guard packet.payload.first != 0xff else {
49-
throw MySQLError(packet:packet)
49+
throw MySQLError(packet:packet, source: .capture())
5050
}
5151

5252
guard let packet = try context.finishAuthentication(for: packet) else {
@@ -74,7 +74,7 @@ struct SendAuthentication: Task {
7474
// Incoming packet can be an error packet so fail fast
7575
// Do not silence the error!
7676
guard packet.payload.first != 0xff else {
77-
throw MySQLError(packet:packet)
77+
throw MySQLError(packet:packet, source: .capture())
7878
}
7979

8080
_ = try packet.parseBinaryOK()
@@ -107,7 +107,7 @@ final class ParseResults: Task {
107107
// Incoming packet can be an error packet so fail fast
108108
// Do not silence the error!
109109
guard packet.payload.first != 0xff else {
110-
throw MySQLError(packet:packet)
110+
throw MySQLError(packet:packet, source: .capture())
111111
}
112112

113113
guard let columnCount = columnCount else {
@@ -180,7 +180,7 @@ final class ParseResults: Task {
180180
let length = try parser.parseLenEnc()
181181

182182
guard length < Int.max else {
183-
throw MySQLError(.unexpectedResponse)
183+
throw MySQLError(.unexpectedResponse, source: .capture())
184184
}
185185

186186
self.columnCount = numericCast(length)
@@ -227,7 +227,7 @@ struct TextQuery: Task {
227227
// Incoming packet can be an error packet so fail fast
228228
// Do not silence the error!
229229
guard packet.payload.first != 0xff else {
230-
throw MySQLError(packet:packet)
230+
throw MySQLError(packet: packet, source: .capture())
231231
}
232232

233233
return try parse.update(with: packet)
@@ -277,7 +277,7 @@ final class PrepareQuery: Task {
277277
// We cannot just throw an MySQLError here, because the supplied closure must fail,
278278
// otherways we are endup with hanging Future.
279279
// So we are bubble up the error to the supplied closure and end the Task
280-
callback(nil, MySQLError(packet: packet))
280+
callback(nil, MySQLError(packet: packet, source: .capture()))
281281
return true
282282
}
283283

@@ -394,7 +394,7 @@ struct ResetPreparation: Task {
394394
/// Do not silence the error!
395395
/// FIXME: invalidate statement id if error.
396396
guard packet.payload.first != 0xff else {
397-
throw MySQLError(packet:packet)
397+
throw MySQLError(packet: packet, source: .capture())
398398
}
399399
return true
400400
}
@@ -428,7 +428,7 @@ struct GetMore: Task {
428428
// Incoming packet can be an error packet so fail fast
429429
// Do not silence the error!
430430
guard packet.payload.first != 0xff else {
431-
throw MySQLError(packet:packet)
431+
throw MySQLError(packet: packet, source: .capture())
432432
}
433433
// FIXME: Implement binary resultset row parsing and pass it up!
434434
// The protocol is good, only need to implement it properly
@@ -464,7 +464,7 @@ struct ExecutePreparation: Task {
464464
// Incoming packet can be an error packet so fail fast
465465
// Do not silence the error
466466
guard packet.payload.first != 0xff else {
467-
throw MySQLError(packet:packet)
467+
throw MySQLError(packet: packet, source: .capture())
468468
}
469469
return try parse.update(with: packet)
470470
}

Sources/MySQL/Parsing/Packet+Response.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ extension Packet {
1919
} else if byte == 0xfe {
2020
return (try parser.parseLenEnc(), try parser.parseLenEnc())
2121
} else if byte == 0xff {
22-
throw MySQLError(packet: self)
22+
throw MySQLError(packet: self, source: .capture())
2323
}
2424

2525
return nil

Sources/MySQL/Parsing/ParsingHelpers.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ struct Parser {
2323
/// Requires `n` amount of bytes or throws an error
2424
mutating func require(_ n: Int) throws {
2525
guard position &+ n <= packet.payload.count else {
26-
throw MySQLError(.invalidPacket)
26+
throw MySQLError(.invalidPacket, source: .capture())
2727
}
2828
}
2929

@@ -75,7 +75,7 @@ struct Parser {
7575
/// Parses the length encoded integer
7676
mutating func parseLenEnc() throws -> UInt64 {
7777
guard position < self.payload.count else {
78-
throw MySQLError(.invalidResponse)
78+
throw MySQLError(.invalidResponse, source: .capture())
7979
}
8080

8181
switch self.payload[position] {
@@ -92,7 +92,7 @@ struct Parser {
9292

9393
return try parseUInt64()
9494
case 0xff:
95-
throw MySQLError(packet: packet)
95+
throw MySQLError(packet: packet, source: .capture())
9696
default:
9797
defer { position = position &+ 1 }
9898
return numericCast(self.payload[position])
@@ -119,7 +119,7 @@ struct Parser {
119119
let length = Int(try parseLenEnc())
120120

121121
guard position &+ length <= self.payload.count else {
122-
throw MySQLError(.invalidResponse)
122+
throw MySQLError(.invalidResponse, source: .capture())
123123
}
124124

125125
defer { position = position &+ length }

0 commit comments

Comments
 (0)