How to find the kind of errors a method may throw and catch them in Swift?

TLDR; how do I find out what errors a foundation method can throw so I can properly handle the error case?


The swift error handling documentation says you handle errors like this


enum VendingMachineError: ErrorType 
{ 
    case InvalidSelection 
    case InsufficientFunds(centsNeeded: Int) 
    case OutOfStock 
}


do {
    try vend(itemNamed: "Candy Bar")
    // Enjoy delicious snack
} catch VendingMachineError.InvalidSelection {
    print("Invalid Selection.")
} catch VendingMachineError.OutOfStock {
    print("Out of Stock.")
} catch VendingMachineError.InsufficientFunds(let amountNeeded) {
    print("Insufficient funds. Please insert an additional \(amountNeeded) cents.")
}


I want to do the same thing for CocaTouch classes like FileManager. However I can't find the errors that each method throws. I didn't see it in any documentation and as far as I'm aware this code isn't open source.


do {
    let docsArray = try fileManager.contentsOfDirectoryAtPath(docsPath)
} catch FileManagerError.PathNotFound {
    print("The path you selected does not exist.")
} catch FileManagerError.PermissionDenied {
    print("You do not have permission to access this directory.")
} catch ErrorType {
    print("An error occured.")
}


How are you supposed to handle all the error cases?

Replies

Someone seem to have asked exactly the same question with the same comments some time ago here:

h ttps://stackoverflow.com/questions/31977738/how-to-find-the-kind-of-errors-a-method-may-throw-and-catch-them-in-swift


with the following answer

As for knowing which error can be returned by a specific call, only the documentation can help. Almost all Foundation errors are part of the

NSCocoaError
domain and can be found in
FoundationErrors.h


In the XCode documentation, at Foundation > File System > File System Error Codes:



Error Codes



var NSFileNoSuchFileError: Int

File-system operation attempted on non-existent file.


var NSFileLockingError: Int

Failure to get a lock on file.


var NSFileReadUnknownError: Int

Read error, reason unknown.


var NSFileReadNoPermissionError: Int

Read error because of a permission problem.


var NSFileReadInvalidFileNameError: Int

Read error because of an invalid file name.


var NSFileReadCorruptFileError: Int

Read error because of a corrupted file, bad format, or similar reason.


var NSFileReadNoSuchFileError: Int

Read error because no such file was found.


var NSFileReadInapplicableStringEncodingError: Int

Read error because the string encoding was not applicable.


var NSFileReadUnsupportedSchemeError: Int

Read error because the specified URL scheme is unsupported.


var NSFileReadTooLargeError: Int

Read error because the specified file was too large.


var NSFileReadUnknownStringEncodingError: Int

Read error because the string coding of the file could not be determined.


var NSFileWriteUnknownError: Int

Write error, reason unknown.


var NSFileWriteNoPermissionError: Int

Write error because of a permission problem.


var NSFileWriteInvalidFileNameError: Int

Write error because of an invalid file name.


var NSFileWriteFileExistsError: Int

Write error returned when NSFileManager class’s copy, move, and link methods report errors when the destination file already exists.


var NSFileWriteInapplicableStringEncodingError: Int

Write error because the string encoding was not applicable.


var NSFileWriteUnsupportedSchemeError: Int

Write error because the specified URL scheme is unsupported.


var NSFileWriteOutOfSpaceError: Int

Write error because of a lack of disk space.


var NSFileWriteVolumeReadOnlyError: Int

Write error because because the volume is read only.


var NSFileManagerUnmountBusyError: Int

The volume could not be unmounted because it is in use.


var NSFileManagerUnmountUnknownError: Int

The volume could not be unmounted, reason unknown.


var NSFileErrorMinimum: Int

Marks the start of the range of error codes reserved for file errors.


var NSFileErrorMaximum: Int

Marks the end of the range of error codes reserved for file errors.