5

While on a signup process, the user can cause several errors like Username already taken, invalid email address etc...

Parse returns within the error object all needed infos see http://parse.com/docs/dotnet/api/html/T_Parse_ParseException_ErrorCode.htm

What I can't find out is how to use them eg how to access them in order to write a switch to catch all possibilities:

                user.signUpInBackgroundWithBlock {
                    (succeeded: Bool!, error: NSError!) -> Void in
                    if error == nil {
                        // Hooray! Let them use the app now.
                        self.updateLabel("Erfolgreich registriert")
                    } else {
                        println(error.userInfo)
                    }
                }

What can I do to switch through the possible error code numbers? Please advice Thanks!

3 Answers 3

8

An NSError also has a property called code. That code contains the error-code you need. So you can make a switch-statement with that code:

user.signUpInBackgroundWithBlock {
    (succeeded: Bool!, error: NSError!) -> Void in
    if error == nil {
        // Hooray! Let them use the app now.
        self.updateLabel("Erfolgreich registriert")
    } else {
        println(error.userInfo)
        var errorCode = error.code

        switch errorCode {
        case 100:
            println("ConnectionFailed")
            break
        case 101:
            println("ObjectNotFound")
            break
        default:
            break
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Is there any way to use the constants? I really don't like hard-coding values...
@TakeshiKaga Yes you can, for instance: case NSURLErrorCannotConnectToHost:
2

Hi i would do something like

if let error = error,let code = PFErrorCode(rawValue: error._code) {
    switch code {
    case .errorConnectionFailed:
        print("errorConnectionFailed")
    case .errorObjectNotFound:
        print("errorObjectNotFound")
    default:
        break
    }
}

You have the whole errors list there : https://github.com/parse-community/Parse-SDK-iOS-OSX/blob/master/Parse/PFConstants.h#L128

Comments

0

You can also use PFErrorCode:

user.signUpInBackgroundWithBlock {
    (succeeded: Bool!, error: NSError!) -> Void in
    if error == nil {
        // Hooray! Let them use the app now.
        self.updateLabel("Erfolgreich registriert")
    } else {
        println(error.userInfo)
        var errorCode = error!.code

        switch errorCode {
           case PFErrorCode.ErrorConnectionFailed.rawValue:
              println("ConnectionFailed")
           case PFErrorCode.ErrorObjectNotFound.rawValue:
              println("ObjectNotFound")
           default:
              break
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.