Skip to content

Commit

Permalink
CSVError adoption to CustomDebugStringConvertible
Browse files Browse the repository at this point in the history
  • Loading branch information
dehesa committed Mar 23, 2020
1 parent 849f2cf commit e133d0b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
38 changes: 35 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ You can choose to add the library through SPM or Cocoapods:

let package = Package(
/* Your package name, supported platforms, and generated products go here */
dependencies: [ .package(url: "https://github.com/dehesa/CodableCSV.git", .upToNextMinor(from: "0.5.0")) ],
targets: [ .target(name: /* Your target name here */, dependencies: ["CodableCSV"]) ]
dependencies: [
.package(url: "https://github.com/dehesa/CodableCSV.git", .upToNextMinor(from: "0.5.0"))
],
targets: [
.target(name: /* Your target name here */, dependencies: ["CodableCSV"])
]
)
```

Expand Down Expand Up @@ -256,6 +260,34 @@ let writer = CSWriter(fileURL: ...) {
}
```

</p></details>

<details><summary><code>CSVError</code>.</summary><p>

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.

```swift
do {
let writer = try CSVWriter()
for row in customData {
try writer.write(row: row)
}
} catch let error {
print(error)
}
```

`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.

- `type`: The error group category.
- `failureReason`: Explanation on what went wrong.
- `helpAnchor`: Advice on how to solve the problem.
- `errorUserInfo`: Arguments associated with the operation that threw the error.
- `underlyingError`: Optional underlying error, which provoked the operation to fail (most of the time is `nil`).
- `localizedDescription`: Returns a human readable string with all the information contained in the error.

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>`.

</p></details>
</ul>

Expand Down Expand Up @@ -481,7 +513,7 @@ struct Student: Codable {

</details>

<details><summary>Configuration values and encoding/decoding strategies.</summary><p>
<details><summary>Encoding/decoding strategies.</summary><p>

#warning("TODO:")

Expand Down
6 changes: 5 additions & 1 deletion Sources/Active/Error.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation

/// Errors that can be thrown from a CSV reader instance.
public final class CSVError<F>: LocalizedError, CustomNSError where F:Failable {
public final class CSVError<F>: LocalizedError, CustomNSError, CustomDebugStringConvertible where F:Failable {
/// The type of error being raised.
public let type: F.Failure
/// A localized message describing the reason for the failure.
Expand Down Expand Up @@ -57,6 +57,10 @@ public final class CSVError<F>: LocalizedError, CustomNSError where F:Failable {
}
return result
}

public var debugDescription: String {
return self.localizedDescription
}
}

/// An instance that throws custom errors.
Expand Down

0 comments on commit e133d0b

Please sign in to comment.