[swift-users] Proper Way to make Errors in Swift 3

Ronak ronak2121 at yahoo.com
Thu Sep 29 12:17:32 CDT 2016


Hello all,

We are proceeding to update all of our Swift code to Swift 3 now and had a few questions about the proper way to implement Errors. We need these entities to be available in Objective-C and they are actively being used in Swift classes marked as @objc.

I read: https://github.com/apple/swift-evolution/blob/master/proposals/0112-nserror-bridging.md <https://github.com/apple/swift-evolution/blob/master/proposals/0112-nserror-bridging.md> completely and came up with this implementation:


/// The enumeration of the possible error codes in the Foundation error domain
@objc public class FoundationError: NSObject, CustomNSError {

    /// The underlying error code
    private let code: FoundationError.Code

    /// The type of an error code.
    @objc public enum Code: Int {

        /// An ARCOperationCondition failed during evaluation
        case operationConditionFailed = 10000

        /// An ARCOperation failed during execution
        case operationExecutionFailed = 10001
    }

    /// The domain of the error.
    public static var errorDomain: String {
        return "FoundationError"
    }

    /// The error code within the given domain.
    public var errorCode: Int {
        return code.rawValue
    }

    /// The user-info dictionary.
    public let errorUserInfo: [String : Any]

    /// Initializes a new FoundationError with an empty userInfo dictionary
    ///
    /// - parameter code: one of the available error codes
    ///
    /// - returns: a new instance of FoundationError
    public convenience init(code: FoundationError.Code) {
        self.init(code: code, userInfo: [:])
    }

    /// Initializes a new FoundationError with an userInfo dictionary
    ///
    /// - parameter code: one of the available error codes
    /// - parameter userInfo: the user-info dictionary
    ///
    /// - returns: a new instance of FoundationError
    public init(code: FoundationError.Code, userInfo: [String : Any]) {
        self.code = code
        errorUserInfo = userInfo
    }

    /// Computes whether two FoundationErrors are equal
    ///
    /// - parameter object: a FoundationError
    ///
    /// - returns: true, if the two errors are equal
    public override func isEqual(_ object: Any?) -> Bool {
        guard let object = object as? FoundationError else { return false }

        return errorCode == object.errorCode && errorUserInfo.keys.elementsEqual(object.errorUserInfo.keys)
    }
}

My question is whether this is the correct way to do this now; or is there another solution we should be doing? We would like to follow Swift Best Practices here, but unfortunately, the documentation is quite vague on this subject.


Thanks for your help!

Ronak Patel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160929/951566ca/attachment.html>


More information about the swift-users mailing list