JohnSundell / Files

A nicer way to handle files & folders in Swift
MIT License
2.53k stars 182 forks source link

Capturing the underlying error. #39

Closed ghost closed 6 years ago

ghost commented 6 years ago

The following explains that the folder 'clone' failed to delete.

Failed to delete item: Folder(name: clone, path: /Users/robertnash/.marathon/Scripts/Temp/https:--github.com-johnsundell-testdrive.git/clone/)
  - deleteFailed : Folder(name: clone, path: /Users/robertnash/.marathon/Scripts/Temp/https:--github.com-johnsundell-testdrive.git/clone/)

This is likely a permissions issue or some other trivial problem. But I'm more interested in how to find the underlying error.

What's your opinion on capturing the underlying error here ?

public func delete() throws {
    do {
        try fileManager.removeItem(atPath: path)
    } catch let underlyingError as NSError {
        throw OperationError.deleteFailed(self, underlyingError)
    } catch {
      throw OperationError.deleteFailed(self, _)
  }
}

Or something like that.

Taking inspiration from NSError, further investigation into an error could be done by unwrapping objects associated with keys like NSUnderlyingErrorKey or NSValidationMultipleErrorsError.

JohnSundell commented 6 years ago

Hi @rob-nash 👋

That's a great idea! 👍 Since all Swift errors are convertible to NSError, I suggest skipping the middle catch and just pass any thrown error as the underlying error.

Will you submit a PR that implements this? 🙂

ghost commented 6 years ago

Ye I’ll take a look tomorrow morning UK time

JohnSundell commented 6 years ago

@rob-nash cool, no stress - you're the only one asking for this feature so take your time 😄

ghost commented 6 years ago

A few discussion points over at Pull 40. Travis is upset and I'm not sure why. Locally, the tests are passing.