Skip to content

Commit 24b0b9d

Browse files
committed
Add URLEncodedFormError.description, update array value in error
1 parent 6e82952 commit 24b0b9d

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

Sources/Hummingbird/Codable/URLEncodedForm/URLEncodedFormNode.swift

+17-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
public struct URLEncodedFormError: Error {
15+
/// Error thrown from parsing URLEncoded forms
16+
public struct URLEncodedFormError: Error, CustomStringConvertible {
1617
public struct Code: Sendable, Equatable {
1718
fileprivate enum Internal: Equatable {
1819
case duplicateKeys
@@ -55,6 +56,20 @@ public struct URLEncodedFormError: Error {
5556
}
5657
}
5758

59+
extension URLEncodedFormError {
60+
public var description: String {
61+
switch self.code.value {
62+
case .duplicateKeys: "Found duplicate keys with name '\(self.value)'"
63+
case .addingToInvalidType: "Adding array or dictionary value to non array or dictionary value '\(self.value)'"
64+
case .failedToPercentDecode: "Failed to percent decode '\(self.value)'"
65+
case .corruptKeyValue: "Parsing dictionary key value failed '\(self.value)'"
66+
case .notSupported: "URLEncoded form structure not supported '\(self.value)'"
67+
case .invalidArrayIndex: "Invalid array index '\(self.value)'"
68+
case .unexpectedError:
69+
"Unexpected error with '\(self.value)' please add an issue at https://github.com/hummingbird-project/hummingbird/issues"
70+
}
71+
}
72+
}
5873
/// Internal representation of URL encoded form data used by both encode and decode
5974
enum URLEncodedFormNode: CustomStringConvertible, Equatable {
6075
/// holds a value
@@ -142,7 +157,7 @@ enum URLEncodedFormNode: CustomStringConvertible, Equatable {
142157
}
143158
case (.array(let array), .arrayWithIndices(let index)):
144159
guard keys.count == 0, array.values.count == index else {
145-
throw URLEncodedFormError(code: .invalidArrayIndex, value: key)
160+
throw URLEncodedFormError(code: .invalidArrayIndex, value: "\(key)[\(index)]")
146161
}
147162
array.values.append(.leaf(value))
148163
case (_, .arrayWithIndices), (_, .array):

Tests/HummingbirdTests/URLEncodedForm/URLDecoderTests.swift

+18-2
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ final class URLDecodedFormDecoderTests: XCTestCase {
141141
return
142142
}
143143
XCTAssertEqual(error.code, .invalidArrayIndex)
144-
XCTAssertEqual(error.value, "arr")
144+
XCTAssertEqual(error.value, "arr[2]")
145145
}
146146
}
147147

@@ -295,7 +295,7 @@ final class URLDecodedFormDecoderTests: XCTestCase {
295295
self.testForm(ArrayDecoding(array: [], map: [:], a: 3), query: "a=3")
296296
}
297297

298-
func testDecodeErrors() throws {
298+
func testParsingErrors() throws {
299299
struct Input1: Decodable {}
300300
XCTAssertThrowsError(try URLEncodedFormDecoder().decode(Input1.self, from: "someField=1&someField=2")) { error in
301301
guard let error = try? XCTUnwrap(error as? URLEncodedFormError) else {
@@ -305,6 +305,14 @@ final class URLDecodedFormDecoderTests: XCTestCase {
305305
XCTAssertEqual(error.code, .duplicateKeys)
306306
XCTAssertEqual(error.value, "someField")
307307
}
308+
XCTAssertThrowsError(try URLEncodedFormDecoder().decode(Input1.self, from: "someField[]=1&someField=2")) { error in
309+
guard let error = try? XCTUnwrap(error as? URLEncodedFormError) else {
310+
XCTFail()
311+
return
312+
}
313+
XCTAssertEqual(error.code, .duplicateKeys)
314+
XCTAssertEqual(error.value, "someField")
315+
}
308316
XCTAssertThrowsError(try URLEncodedFormDecoder().decode(Input1.self, from: "someField=1&someField[]=2")) { error in
309317
guard let error = try? XCTUnwrap(error as? URLEncodedFormError) else {
310318
XCTFail()
@@ -321,5 +329,13 @@ final class URLDecodedFormDecoderTests: XCTestCase {
321329
XCTAssertEqual(error.code, .addingToInvalidType)
322330
XCTAssertEqual(error.value, "someField")
323331
}
332+
XCTAssertThrowsError(try URLEncodedFormDecoder().decode(Input1.self, from: "someField[=2")) { error in
333+
guard let error = try? XCTUnwrap(error as? URLEncodedFormError) else {
334+
XCTFail()
335+
return
336+
}
337+
XCTAssertEqual(error.code, .corruptKeyValue)
338+
XCTAssertEqual(error.value, "someField[")
339+
}
324340
}
325341
}

0 commit comments

Comments
 (0)