Skip to content

Commit 76e2045

Browse files
committed
Add SourceLocation to SwiftProtobufError's description
1 parent d1b209d commit 76e2045

31 files changed

+1018
-582
lines changed

Sources/SwiftProtobuf/AnyMessageStorage.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,15 @@ fileprivate func unpack(contentJSON: [UInt8],
7676
}
7777
if !options.ignoreUnknownFields {
7878
// The only thing within a WKT should be "value".
79-
throw SwiftProtobufError.AnyUnpack.malformedWellKnownTypeJSON
79+
throw SwiftProtobufError.AnyUnpack.malformedWellKnownTypeJSON()
8080
}
8181
let _ = try scanner.skip()
8282
try scanner.skipRequiredComma()
8383
}
8484
if !options.ignoreUnknownFields && !scanner.complete {
8585
// If that wasn't the end, then there was another key, and WKTs should
8686
// only have the one when not skipping unknowns.
87-
throw SwiftProtobufError.AnyUnpack.malformedWellKnownTypeJSON
87+
throw SwiftProtobufError.AnyUnpack.malformedWellKnownTypeJSON()
8888
}
8989
}
9090
}
@@ -174,7 +174,7 @@ internal class AnyMessageStorage {
174174
options: BinaryDecodingOptions
175175
) throws {
176176
guard isA(M.self) else {
177-
throw SwiftProtobufError.AnyUnpack.typeMismatch
177+
throw SwiftProtobufError.AnyUnpack.typeMismatch()
178178
}
179179

180180
switch state {
@@ -243,7 +243,7 @@ extension AnyMessageStorage {
243243
_typeURL = url
244244
guard let messageType = Google_Protobuf_Any.messageType(forTypeURL: url) else {
245245
// The type wasn't registered, can't parse it.
246-
throw SwiftProtobufError.TextFormatDecoding.malformedText
246+
throw SwiftProtobufError.TextFormatDecoding.malformedText()
247247
}
248248
let terminator = try decoder.scanner.skipObjectStart()
249249
var subDecoder = try TextFormatDecoder(messageType: messageType, scanner: decoder.scanner, terminator: terminator)
@@ -259,7 +259,7 @@ extension AnyMessageStorage {
259259
decoder.scanner = subDecoder.scanner
260260
if try decoder.nextFieldNumber() != nil {
261261
// Verbose any can never have additional keys.
262-
throw SwiftProtobufError.TextFormatDecoding.malformedText
262+
throw SwiftProtobufError.TextFormatDecoding.malformedText()
263263
}
264264
}
265265

Sources/SwiftProtobuf/AsyncMessageSequence.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public struct AsyncMessageSequence<
122122
shift += UInt64(7)
123123
if shift > 35 {
124124
iterator = nil
125-
throw SwiftProtobufError.BinaryDecoding.malformedLength
125+
throw SwiftProtobufError.BinaryDecoding.malformedLength()
126126
}
127127
if (byte & 0x80 == 0) {
128128
return messageSize
@@ -131,7 +131,7 @@ public struct AsyncMessageSequence<
131131
if (shift > 0) {
132132
// The stream has ended inside a varint.
133133
iterator = nil
134-
throw SwiftProtobufError.BinaryDecoding.truncated
134+
throw SwiftProtobufError.BinaryDecoding.truncated()
135135
}
136136
return nil // End of stream reached.
137137
}
@@ -153,7 +153,7 @@ public struct AsyncMessageSequence<
153153
guard let byte = try await iterator?.next() else {
154154
// The iterator hit the end, but the chunk wasn't filled, so the full
155155
// payload wasn't read.
156-
throw SwiftProtobufError.BinaryDecoding.truncated
156+
throw SwiftProtobufError.BinaryDecoding.truncated()
157157
}
158158
chunk[consumedBytes] = byte
159159
consumedBytes += 1
@@ -181,7 +181,7 @@ public struct AsyncMessageSequence<
181181
}
182182
guard messageSize <= UInt64(0x7fffffff) else {
183183
iterator = nil
184-
throw SwiftProtobufError.BinaryDecoding.tooLarge
184+
throw SwiftProtobufError.BinaryDecoding.tooLarge()
185185
}
186186
if messageSize == 0 {
187187
return try M(

Sources/SwiftProtobuf/BinaryDecoder.swift

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ internal struct BinaryDecoder: Decoder {
8080
private mutating func incrementRecursionDepth() throws {
8181
recursionBudget -= 1
8282
if recursionBudget < 0 {
83-
throw SwiftProtobufError.BinaryDecoding.messageDepthLimit
83+
throw SwiftProtobufError.BinaryDecoding.messageDepthLimit()
8484
}
8585
}
8686

@@ -139,7 +139,7 @@ internal struct BinaryDecoder: Decoder {
139139
if let wireFormat = WireFormat(rawValue: c0 & 7) {
140140
fieldWireFormat = wireFormat
141141
} else {
142-
throw SwiftProtobufError.BinaryDecoding.malformedProtobuf
142+
throw SwiftProtobufError.BinaryDecoding.malformedProtobuf()
143143
}
144144
if (c0 & 0x80) == 0 {
145145
p += 1
@@ -148,7 +148,7 @@ internal struct BinaryDecoder: Decoder {
148148
} else {
149149
fieldNumber = Int(c0 & 0x7f) >> 3
150150
if available < 2 {
151-
throw SwiftProtobufError.BinaryDecoding.malformedProtobuf
151+
throw SwiftProtobufError.BinaryDecoding.malformedProtobuf()
152152
}
153153
let c1 = start[1]
154154
if (c1 & 0x80) == 0 {
@@ -158,7 +158,7 @@ internal struct BinaryDecoder: Decoder {
158158
} else {
159159
fieldNumber |= Int(c1 & 0x7f) << 4
160160
if available < 3 {
161-
throw SwiftProtobufError.BinaryDecoding.malformedProtobuf
161+
throw SwiftProtobufError.BinaryDecoding.malformedProtobuf()
162162
}
163163
let c2 = start[2]
164164
fieldNumber |= Int(c2 & 0x7f) << 11
@@ -167,7 +167,7 @@ internal struct BinaryDecoder: Decoder {
167167
available -= 3
168168
} else {
169169
if available < 4 {
170-
throw SwiftProtobufError.BinaryDecoding.malformedProtobuf
170+
throw SwiftProtobufError.BinaryDecoding.malformedProtobuf()
171171
}
172172
let c3 = start[3]
173173
fieldNumber |= Int(c3 & 0x7f) << 18
@@ -176,11 +176,11 @@ internal struct BinaryDecoder: Decoder {
176176
available -= 4
177177
} else {
178178
if available < 5 {
179-
throw SwiftProtobufError.BinaryDecoding.malformedProtobuf
179+
throw SwiftProtobufError.BinaryDecoding.malformedProtobuf()
180180
}
181181
let c4 = start[4]
182182
if c4 > 15 {
183-
throw SwiftProtobufError.BinaryDecoding.malformedProtobuf
183+
throw SwiftProtobufError.BinaryDecoding.malformedProtobuf()
184184
}
185185
fieldNumber |= Int(c4 & 0x7f) << 25
186186
p += 5
@@ -200,12 +200,12 @@ internal struct BinaryDecoder: Decoder {
200200
} else {
201201
// .endGroup when not in a group or for a different
202202
// group is an invalid binary.
203-
throw SwiftProtobufError.BinaryDecoding.malformedProtobuf
203+
throw SwiftProtobufError.BinaryDecoding.malformedProtobuf()
204204
}
205205
}
206206
return fieldNumber
207207
}
208-
throw SwiftProtobufError.BinaryDecoding.malformedProtobuf
208+
throw SwiftProtobufError.BinaryDecoding.malformedProtobuf()
209209
}
210210

211211
internal mutating func decodeSingularFloatField(value: inout Float) throws {
@@ -236,7 +236,7 @@ internal struct BinaryDecoder: Decoder {
236236
let itemSize = UInt64(MemoryLayout<Float>.size)
237237
let itemCount = bodyBytes / itemSize
238238
if bodyBytes % itemSize != 0 || bodyBytes > available {
239-
throw SwiftProtobufError.BinaryDecoding.truncated
239+
throw SwiftProtobufError.BinaryDecoding.truncated()
240240
}
241241
value.reserveCapacity(value.count + Int(truncatingIfNeeded: itemCount))
242242
for _ in 1...itemCount {
@@ -277,7 +277,7 @@ internal struct BinaryDecoder: Decoder {
277277
let itemSize = UInt64(MemoryLayout<Double>.size)
278278
let itemCount = bodyBytes / itemSize
279279
if bodyBytes % itemSize != 0 || bodyBytes > available {
280-
throw SwiftProtobufError.BinaryDecoding.truncated
280+
throw SwiftProtobufError.BinaryDecoding.truncated()
281281
}
282282
value.reserveCapacity(value.count + Int(truncatingIfNeeded: itemCount))
283283
for _ in 1...itemCount {
@@ -721,7 +721,7 @@ internal struct BinaryDecoder: Decoder {
721721
value = s
722722
consumed = true
723723
} else {
724-
throw SwiftProtobufError.BinaryDecoding.invalidUTF8
724+
throw SwiftProtobufError.BinaryDecoding.invalidUTF8()
725725
}
726726
}
727727

@@ -735,7 +735,7 @@ internal struct BinaryDecoder: Decoder {
735735
value = s
736736
consumed = true
737737
} else {
738-
throw SwiftProtobufError.BinaryDecoding.invalidUTF8
738+
throw SwiftProtobufError.BinaryDecoding.invalidUTF8()
739739
}
740740
}
741741

@@ -748,7 +748,7 @@ internal struct BinaryDecoder: Decoder {
748748
value.append(s)
749749
consumed = true
750750
} else {
751-
throw SwiftProtobufError.BinaryDecoding.invalidUTF8
751+
throw SwiftProtobufError.BinaryDecoding.invalidUTF8()
752752
}
753753
default:
754754
return
@@ -890,7 +890,7 @@ internal struct BinaryDecoder: Decoder {
890890
try message.decodeMessage(decoder: &self)
891891
decrementRecursionDepth()
892892
guard complete else {
893-
throw SwiftProtobufError.BinaryDecoding.trailingGarbage
893+
throw SwiftProtobufError.BinaryDecoding.trailingGarbage()
894894
}
895895
if let unknownData = unknownData {
896896
message.unknownFields.append(protobufData: unknownData)
@@ -935,7 +935,7 @@ internal struct BinaryDecoder: Decoder {
935935
subDecoder.unknownData = nil
936936
try group.decodeMessage(decoder: &subDecoder)
937937
guard subDecoder.fieldNumber == fieldNumber && subDecoder.fieldWireFormat == .endGroup else {
938-
throw SwiftProtobufError.BinaryDecoding.truncated
938+
throw SwiftProtobufError.BinaryDecoding.truncated()
939939
}
940940
if let groupUnknowns = subDecoder.unknownData {
941941
group.unknownFields.append(protobufData: groupUnknowns)
@@ -958,7 +958,7 @@ internal struct BinaryDecoder: Decoder {
958958
var subdecoder = BinaryDecoder(forReadingFrom: p, count: count, parent: self)
959959
while let tag = try subdecoder.getTag() {
960960
if tag.wireFormat == .endGroup {
961-
throw SwiftProtobufError.BinaryDecoding.malformedProtobuf
961+
throw SwiftProtobufError.BinaryDecoding.malformedProtobuf()
962962
}
963963
let fieldNumber = tag.fieldNumber
964964
switch fieldNumber {
@@ -971,7 +971,7 @@ internal struct BinaryDecoder: Decoder {
971971
}
972972
}
973973
if !subdecoder.complete {
974-
throw SwiftProtobufError.BinaryDecoding.trailingGarbage
974+
throw SwiftProtobufError.BinaryDecoding.trailingGarbage()
975975
}
976976
// A map<> definition can't provide a default value for the keys/values,
977977
// so it is safe to use the proto3 default to get the right
@@ -993,7 +993,7 @@ internal struct BinaryDecoder: Decoder {
993993
var subdecoder = BinaryDecoder(forReadingFrom: p, count: count, parent: self)
994994
while let tag = try subdecoder.getTag() {
995995
if tag.wireFormat == .endGroup {
996-
throw SwiftProtobufError.BinaryDecoding.malformedProtobuf
996+
throw SwiftProtobufError.BinaryDecoding.malformedProtobuf()
997997
}
998998
let fieldNumber = tag.fieldNumber
999999
switch fieldNumber {
@@ -1014,7 +1014,7 @@ internal struct BinaryDecoder: Decoder {
10141014
}
10151015
}
10161016
if !subdecoder.complete {
1017-
throw SwiftProtobufError.BinaryDecoding.trailingGarbage
1017+
throw SwiftProtobufError.BinaryDecoding.trailingGarbage()
10181018
}
10191019
// A map<> definition can't provide a default value for the keys, so it
10201020
// is safe to use the proto3 default to get the right integer/string/bytes.
@@ -1033,7 +1033,7 @@ internal struct BinaryDecoder: Decoder {
10331033
var subdecoder = BinaryDecoder(forReadingFrom: p, count: count, parent: self)
10341034
while let tag = try subdecoder.getTag() {
10351035
if tag.wireFormat == .endGroup {
1036-
throw SwiftProtobufError.BinaryDecoding.malformedProtobuf
1036+
throw SwiftProtobufError.BinaryDecoding.malformedProtobuf()
10371037
}
10381038
let fieldNumber = tag.fieldNumber
10391039
switch fieldNumber {
@@ -1046,7 +1046,7 @@ internal struct BinaryDecoder: Decoder {
10461046
}
10471047
}
10481048
if !subdecoder.complete {
1049-
throw SwiftProtobufError.BinaryDecoding.trailingGarbage
1049+
throw SwiftProtobufError.BinaryDecoding.trailingGarbage()
10501050
}
10511051
// A map<> definition can't provide a default value for the keys, so it
10521052
// is safe to use the proto3 default to get the right integer/string/bytes.
@@ -1090,7 +1090,7 @@ internal struct BinaryDecoder: Decoder {
10901090
// the bytes were consumed, then there should have been a
10911091
// field that consumed them (existing or created). This
10921092
// specific error result is to allow this to be more detectable.
1093-
throw SwiftProtobufError.BinaryDecoding.internalExtensionError
1093+
throw SwiftProtobufError.BinaryDecoding.internalExtensionError()
10941094
}
10951095
}
10961096
}
@@ -1125,7 +1125,7 @@ internal struct BinaryDecoder: Decoder {
11251125
break
11261126

11271127
case .malformed:
1128-
throw SwiftProtobufError.BinaryDecoding.malformedProtobuf
1128+
throw SwiftProtobufError.BinaryDecoding.malformedProtobuf()
11291129
}
11301130

11311131
assert(recursionBudget == subDecoder.recursionBudget)
@@ -1256,7 +1256,7 @@ internal struct BinaryDecoder: Decoder {
12561256
let _ = try decodeVarint()
12571257
case .fixed64:
12581258
if available < 8 {
1259-
throw SwiftProtobufError.BinaryDecoding.truncated
1259+
throw SwiftProtobufError.BinaryDecoding.truncated()
12601260
}
12611261
p += 8
12621262
available -= 8
@@ -1266,7 +1266,7 @@ internal struct BinaryDecoder: Decoder {
12661266
p += Int(n)
12671267
available -= Int(n)
12681268
} else {
1269-
throw SwiftProtobufError.BinaryDecoding.truncated
1269+
throw SwiftProtobufError.BinaryDecoding.truncated()
12701270
}
12711271
case .startGroup:
12721272
try incrementRecursionDepth()
@@ -1279,20 +1279,20 @@ internal struct BinaryDecoder: Decoder {
12791279
} else {
12801280
// .endGroup for a something other than the current
12811281
// group is an invalid binary.
1282-
throw SwiftProtobufError.BinaryDecoding.malformedProtobuf
1282+
throw SwiftProtobufError.BinaryDecoding.malformedProtobuf()
12831283
}
12841284
} else {
12851285
try skipOver(tag: innerTag)
12861286
}
12871287
} else {
1288-
throw SwiftProtobufError.BinaryDecoding.truncated
1288+
throw SwiftProtobufError.BinaryDecoding.truncated()
12891289
}
12901290
}
12911291
case .endGroup:
1292-
throw SwiftProtobufError.BinaryDecoding.malformedProtobuf
1292+
throw SwiftProtobufError.BinaryDecoding.malformedProtobuf()
12931293
case .fixed32:
12941294
if available < 4 {
1295-
throw SwiftProtobufError.BinaryDecoding.truncated
1295+
throw SwiftProtobufError.BinaryDecoding.truncated()
12961296
}
12971297
p += 4
12981298
available -= 4
@@ -1314,7 +1314,7 @@ internal struct BinaryDecoder: Decoder {
13141314
available += p - fieldStartP
13151315
p = fieldStartP
13161316
guard let tag = try getTagWithoutUpdatingFieldStart() else {
1317-
throw SwiftProtobufError.BinaryDecoding.truncated
1317+
throw SwiftProtobufError.BinaryDecoding.truncated()
13181318
}
13191319
try skipOver(tag: tag)
13201320
fieldEndP = p
@@ -1324,7 +1324,7 @@ internal struct BinaryDecoder: Decoder {
13241324
/// Private: Parse the next raw varint from the input.
13251325
private mutating func decodeVarint() throws -> UInt64 {
13261326
if available < 1 {
1327-
throw SwiftProtobufError.BinaryDecoding.truncated
1327+
throw SwiftProtobufError.BinaryDecoding.truncated()
13281328
}
13291329
var start = p
13301330
var length = available
@@ -1340,7 +1340,7 @@ internal struct BinaryDecoder: Decoder {
13401340
var shift = UInt64(7)
13411341
while true {
13421342
if length < 1 || shift > 63 {
1343-
throw SwiftProtobufError.BinaryDecoding.malformedProtobuf
1343+
throw SwiftProtobufError.BinaryDecoding.malformedProtobuf()
13441344
}
13451345
c = start.load(fromByteOffset: 0, as: UInt8.self)
13461346
start += 1
@@ -1373,13 +1373,13 @@ internal struct BinaryDecoder: Decoder {
13731373
let t = try decodeVarint()
13741374
if t < UInt64(UInt32.max) {
13751375
guard let tag = FieldTag(rawValue: UInt32(truncatingIfNeeded: t)) else {
1376-
throw SwiftProtobufError.BinaryDecoding.malformedProtobuf
1376+
throw SwiftProtobufError.BinaryDecoding.malformedProtobuf()
13771377
}
13781378
fieldWireFormat = tag.wireFormat
13791379
fieldNumber = tag.fieldNumber
13801380
return tag
13811381
} else {
1382-
throw SwiftProtobufError.BinaryDecoding.malformedProtobuf
1382+
throw SwiftProtobufError.BinaryDecoding.malformedProtobuf()
13831383
}
13841384
}
13851385

@@ -1394,7 +1394,7 @@ internal struct BinaryDecoder: Decoder {
13941394
private mutating func decodeLittleEndianInteger<T: FixedWidthInteger>() throws -> T {
13951395
let size = MemoryLayout<T>.size
13961396
assert(size == 4 || size == 8)
1397-
guard available >= size else {throw BinaryDecodingError.truncated}
1397+
guard available >= size else {throw SwiftProtobufError.BinaryDecoding.truncated()}
13981398
defer { consume(length: size) }
13991399
return T(littleEndian: p.loadUnaligned(as: T.self))
14001400
}
@@ -1424,11 +1424,11 @@ internal struct BinaryDecoder: Decoder {
14241424
// that is length delimited on the wire, so the spec would imply
14251425
// the limit still applies.
14261426
guard length < 0x7fffffff else {
1427-
throw SwiftProtobufError.BinaryDecoding.tooLarge
1427+
throw SwiftProtobufError.BinaryDecoding.tooLarge()
14281428
}
14291429

14301430
guard length <= UInt64(available) else {
1431-
throw SwiftProtobufError.BinaryDecoding.truncated
1431+
throw SwiftProtobufError.BinaryDecoding.truncated()
14321432
}
14331433

14341434
count = Int(length)

0 commit comments

Comments
 (0)