Skip to content

Commit e133d0b

Browse files
committed
CSVError adoption to CustomDebugStringConvertible
1 parent 849f2cf commit e133d0b

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

README.md

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,12 @@ You can choose to add the library through SPM or Cocoapods:
3737

3838
let package = Package(
3939
/* Your package name, supported platforms, and generated products go here */
40-
dependencies: [ .package(url: "https://github.com/dehesa/CodableCSV.git", .upToNextMinor(from: "0.5.0")) ],
41-
targets: [ .target(name: /* Your target name here */, dependencies: ["CodableCSV"]) ]
40+
dependencies: [
41+
.package(url: "https://github.com/dehesa/CodableCSV.git", .upToNextMinor(from: "0.5.0"))
42+
],
43+
targets: [
44+
.target(name: /* Your target name here */, dependencies: ["CodableCSV"])
45+
]
4246
)
4347
```
4448

@@ -256,6 +260,34 @@ let writer = CSWriter(fileURL: ...) {
256260
}
257261
```
258262

263+
</p></details>
264+
265+
<details><summary><code>CSVError</code>.</summary><p>
266+
267+
Many of `CodableCSV`'s imperative functions may throw errors due to invalid configuration values, invalid CSV input, file stream failures, etc. All these throwing operations exclusively throw `CSVError`s that can be easily caught with `do`-`catch` clause.
268+
269+
```swift
270+
do {
271+
let writer = try CSVWriter()
272+
for row in customData {
273+
try writer.write(row: row)
274+
}
275+
} catch let error {
276+
print(error)
277+
}
278+
```
279+
280+
`CSVError` adopts [Swift Evolution's SE-112](https://github.com/apple/swift-evolution/blob/master/proposals/0112-nserror-bridging.md) protocols (`LocalizedError` and `CustomNSError`) and `CustomDebugStringConvertible`. The error's properties provide rich commentary explaining what went wrong and giving indication on how to fix the problem.
281+
282+
- `type`: The error group category.
283+
- `failureReason`: Explanation on what went wrong.
284+
- `helpAnchor`: Advice on how to solve the problem.
285+
- `errorUserInfo`: Arguments associated with the operation that threw the error.
286+
- `underlyingError`: Optional underlying error, which provoked the operation to fail (most of the time is `nil`).
287+
- `localizedDescription`: Returns a human readable string with all the information contained in the error.
288+
289+
You can get all the information by simply printing the error or calling the `localizedDescription` property on a properly casted `CSVError<CSVReader>` or `CSVError<CSVWriter>`.
290+
259291
</p></details>
260292
</ul>
261293

@@ -481,7 +513,7 @@ struct Student: Codable {
481513

482514
</details>
483515

484-
<details><summary>Configuration values and encoding/decoding strategies.</summary><p>
516+
<details><summary>Encoding/decoding strategies.</summary><p>
485517

486518
#warning("TODO:")
487519

Sources/Active/Error.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Foundation
22

33
/// Errors that can be thrown from a CSV reader instance.
4-
public final class CSVError<F>: LocalizedError, CustomNSError where F:Failable {
4+
public final class CSVError<F>: LocalizedError, CustomNSError, CustomDebugStringConvertible where F:Failable {
55
/// The type of error being raised.
66
public let type: F.Failure
77
/// A localized message describing the reason for the failure.
@@ -57,6 +57,10 @@ public final class CSVError<F>: LocalizedError, CustomNSError where F:Failable {
5757
}
5858
return result
5959
}
60+
61+
public var debugDescription: String {
62+
return self.localizedDescription
63+
}
6064
}
6165

6266
/// An instance that throws custom errors.

0 commit comments

Comments
 (0)