1

We have a set of custom server messages we change according to the language of the device. The function is returning

(Error Domain=Proyect.ErrorHandler.DatabaseErrors Code=11 "(null)") instead of

(Error Domain=ourwebpage Code=11 "Text we have in Localizable.strings")

class ErrorHandler {

    public enum DatabaseErrors: Error {
        case ok
        case incorrectFormat
        case userHasNoRights
        case doesntExists
        case authError
        case serverError
        case missingData
        case alreadyRegistered
        case loadError
        case inactive
        case updateRequired
        case incorrectData
        case databaseError

        private var domain: String {
            return "ourwebpage"
        }

        private var errorCode: Int {
            switch self {
                case .ok:
                    return 0
                case .incorrectFormat:
                    return 1
                case .userHasNoRights:
                    return 2
                case .doesntExists:
                    return 3
                case .authError:
                    return 4
                case .serverError:
                    return 5
                case .missingData:
                    return 6
                case .alreadyRegistered:
                    return 7
                case .loadError:
                    return 8
                case .inactive:
                    return 9
                case .updateRequired:
                    return 11
                case .incorrectData:
                    return 12
                case .databaseError:
                    return 13

            }
        }

        var description: String {
            switch self {
                case .ok:
                    return NSLocalizedString("server_message_00", comment: "")
                case .incorrectFormat:
                    return NSLocalizedString("server_message_01", comment: "")
                case .userHasNoRights:
                    return NSLocalizedString("server_message_02", comment: "")
                case .doesntExists:
                    return NSLocalizedString("server_message_03", comment: "")
                case .authError:
                    return NSLocalizedString("server_message_04", comment: "")
                case .serverError:
                    return NSLocalizedString("server_message_05", comment: "")
                case .missingData:
                    return NSLocalizedString("server_message_06", comment: "")
                case .alreadyRegistered:
                    return NSLocalizedString("server_message_07", comment: "")
                case .loadError:
                    return NSLocalizedString("server_message_08", comment: "")
                case .inactive:
                    return NSLocalizedString("server_message_09", comment: "")
                case .updateRequired:
                    return NSLocalizedString("server_message_11", comment: "")
                case .incorrectData:
                    return NSLocalizedString("server_message_12", comment: "")
                case .databaseError:
                    return NSLocalizedString("server_message_13", comment: "")
            }
        }

        var foundationError: NSError {
            return NSError(domain: domain, code: errorCode, userInfo: [NSLocalizedDescriptionKey: description])
        }
    }

We call this function like ErrorHandler.checkErrorCode(customCode) to send the custom code we have as an Error in the completionHandler like completionHandler(nil, ErrorHandler.checkErrorCode(customCode) as NSError)

    static func checkErrorCode(_ errorCode: Int) -> DatabaseErrors{
        switch errorCode {
            case 0:
                return .ok
            case 1:
                return .incorrectFormat
            case 2:
                return .userHasNoRights
            case 3:
                return .doesntExists
            case 4:
                return .authError
            case 5:
                return .serverError
            case 6:
                return .missingData
            case 7:
                return .alreadyRegistered
            case 8:
                return .loadError
            case 9:
                return .inactive
            case 11:
                return .updateRequired
            case 12:
                return .incorrectData
            case 13:
                return .databaseError
            default:
                return .ok
        }
    }

}

I tried adding .foundationError to the cases but I get Type of expression is ambiguous without more context. I followed this and this

Jalil
  • 1,167
  • 11
  • 34

1 Answers1

3

Your code is epic DRY resistant!

First you really need to refactor it:

class ErrorHandler {

    public enum DatabaseErrors: Int, Error {
        case ok
        case incorrectFormat
        case userHasNoRights
        case doesNotExists
        case authError
        case serverError
        case missingData
        case alreadyRegistered
        case loadError
        case inactive
        case updateRequired
        case incorrectData
        case databaseError

        private var domain: String { "ourwebpage" }
        private var errorCode: Int { rawValue }

        var description: String { NSLocalizedString(String(format: "server_message_%.2d", errorCode), comment: "") }
        var foundationError: NSError { NSError(domain: domain, code: errorCode, userInfo: [NSLocalizedDescriptionKey: description]) }
    }
}

So now that static function would be:

static func checkErrorCode(_ errorCode: Int) -> DatabaseErrors { DatabaseErrors(rawValue: errorCode) ?? .ok }

That's it! 124 lines of code reduced to just 24 lines and now you can add any case you want without any ambiguity.

Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
  • When I print `error.domain` it still says `Proyect.ErrorHandler.DatabaseErrors` instead of `ourwebpage` and it still has not description nor userInfo – Jalil Nov 21 '19 at 22:23
  • Okay, my bad, the problem was that I was casting it to NSError again – Jalil Nov 21 '19 at 23:05