ursan526
ursan526

Reputation: 535

How to detect and handle firebase Permission Denied error code with swift

I am getting the following error while observe data from firebase realtime database. What I want to do is if the error is Permission Denied, I want to do a different action. How can I tell if the error is Permission Denied?

error :

Optional(Error Domain=com.firebase Code=1 "Permission Denied" UserInfo={NSLocalizedDescription=Permission Denied})

mycode:

func observeData(completion: @escaping (Bool) -> Void){
    Database.database().reference().child("values").observe(.value, with: { (snap) in
        completion(true)
    }){ (error) in
        let errorCode = (error as NSError).code
        if errorCode == .?????? {  //-->> what to come here
            self.anotherFunc()  //--> if Permission Denied call this func
            completion(false)
        }else{
            completion(true)
        }
    }
}

Upvotes: 3

Views: 545

Answers (1)

John Sorensen
John Sorensen

Reputation: 960

This is what I did for Auth errors in my firebase app. Not sure if it will work in your context, but you might find it useful.

func firebaseErrorParser(error: Error) -> String? {
    if let errorCode = AuthErrorCode(rawValue: error._code) { // <- here!!

        switch errorCode { // switch case
        case .invalidVerificationCode:
            return "Wrong code or phone number!"
        default:
            return "An error occured... "
        }
    } else {
        return nil
    }
}

I only had one entry in my switch/case because the only error I wanted to give specific feedback to the user about was if the user entered the wrong phone number. It seems like you can do something similar with firestore permissions errors

Upvotes: 0

Related Questions