2

enter image description here

Code:

enum SHError: Error {

    case InvalidInputError(code: Int, message: String, info: [String:Any]? = [:])
    case InvalidProcessingError(code: Int, message: String, info: [String:Any]? = [:])

    var debugDescription: String {
        return "debug info: code: \(code)"
    }
    var localizedDescription: String {
        return "description: \(self)"
    }
}

How can I access properties that are passed by caller while creating my error?

Sazzad Hissain Khan
  • 37,929
  • 33
  • 189
  • 256
  • Please don't post your code as print screen, copy and past it (and maybe add a comment with error) – Maciej Gad Nov 04 '19 at 12:42
  • 2
    The real trouble is that this whole enum is poorly designed. An error already has a code, and if you also wanted a message you should have used a localized error. – matt Nov 04 '19 at 12:45
  • `enum` cases are not making any sense. Instead of an `enum` you better need a `struct/class` if you want to assign `code`, `message`, `info` upon its declaration/usage. Even better you already have `NSError` class that fulfills all your requirements(`NSError.init(domain: String, code: Int, userInfo: [String : Any])`). You can check this official [documentation](https://developer.apple.com/documentation/swift/error) on how to use `enum` for error handling. – Kamran Nov 04 '19 at 12:56
  • @bazyl87 I have also included raw source code – Sazzad Hissain Khan Nov 04 '19 at 13:13
  • @matt totally agree with you. How can I use custom error properly in my project? Any suggestion? – Sazzad Hissain Khan Nov 04 '19 at 13:30
  • 1
    https://stackoverflow.com/questions/39176196/how-to-provide-a-localized-description-with-an-error-type-in-swift – matt Nov 04 '19 at 13:31

1 Answers1

2

If you want to use a value associated with enum case you have to switch it in this way:

enum SHError: Error {

    case InvalidInputError(code: Int, message: String, info: [String:Any]? = [:])
    case InvalidProcessingError(code: Int, message: String, info: [String:Any]? = [:])

    var debugDescription: String {
        let code: Int
        switch self {
        case .InvalidInputError(code: let codeValue, message: _, info: _):
            code = codeValue
        case .InvalidProcessingError(code: let codeValue, message: _, info: _):
            code = codeValue
        }
        return "debug info: code: \(code)"
    }

    var localizedDescription: String {
        return "description: \(self)"
    }
}

or you can create a separate computed variable var code: Int and use it in debugDescription:

enum SHError: Error {

    case InvalidInputError(code: Int, message: String, info: [String:Any]? = [:])
    case InvalidProcessingError(code: Int, message: String, info: [String:Any]? = [:])

    var debugDescription: String {
        return "debug info: code: \(code)"
    }

    var localizedDescription: String {
        return "description: \(self)"
    }

    var code: Int {
        switch self {
        case .InvalidInputError(code: let code, message: _, info: _):
            return code
        case .InvalidProcessingError(code: let code, message: _, info: _):
            return code
        }
    }
}
Maciej Gad
  • 1,701
  • 16
  • 21