What does ParseUser.signUpInBackground() return in case of Error?

931 views Asked by At

I am trying to implement my registration flow using Parse SDK and Bolts framework. Using the Bolts-android documentation I created a task which tries to fetch personal information from social networks and then creates new ParseUser with these data and returns Task. I chained this task with another using the onSuccessTask() method where I am trying to sign up new User to the Parse using the signUpInBackground() method, which returns another task. I chained the latter with onSuccess() method call, where I check whether sign up finished successfully and then proceed user to another screen. When the new user tries to register everything goes ok - user successfully signs up. The problem is when the user already exists on the server Parse should return ParseException with code 202 and I thought that my second Task, where I am trying to Sign up should produce new task with error state, but it seems like it never finishes.

This is my first task:

private Task<ParseUser> fetchUserTask() {
    final Task.TaskCompletionSource tcs = Task.create();
    mNetwork.requestDetailedCurrentPerson(new OnRequestDetailedSocialPersonCompleteListener() {
        @Override
        public void onRequestDetailedSocialPersonSuccess(int socialNetworkID, SocialPerson socialPerson) {
            ParseUser user = new ParseUser();
            if (mNetworkId == VkSocialNetwork.ID) {
                user.setUsername("vk" + socialPerson.id);
            } else if (mNetworkId == FacebookSocialNetwork.ID) {
                user.setUsername("fb" + socialPerson.id);
            }
            user.put("fullName", socialPerson.name);
            user.put("photo", socialPerson.avatarURL);
            tcs.setResult(user);
        }

        @Override
        public void onError(int socialNetworkID, String requestID, String errorMessage, Object data) {
            tcs.setError(new Exception(errorMessage));
        }
    });
    return tcs.getTask();
}

This is my login method:

private void loginUser() {
    fetchUserTask()
            .onSuccessTask(new Continuation<ParseUser, Task<Void>>() {
                @Override
                public Task<Void> then(Task<ParseUser> task) throws Exception {
                    if (task.isCompleted()) {
                        Log.d(EristicaConstants.LOG_TAG, "task1 ended");
                        return task.getResult().signUpInBackground();
                    } else {
                        Log.d(EristicaConstants.LOG_TAG, "some error happened " + task
                                .getError().getLocalizedMessage());
                    }
                    return null;
                }
            })
            .onSuccess(new Continuation<Void, Void>() {
                @Override
                public Void then(Task<Void> task) throws Exception {
                    if (task.isCompleted()) {
                        Log.d(EristicaConstants.LOG_TAG, "task 2 ended");
                        mSuccessCallback.success();
                    } else {
                        Log.d(EristicaConstants.LOG_TAG, "some error happened 2" + task
                                .getError().getLocalizedMessage());
                    }
                    return null;
                }
            });
}

So in case of new User I see these sequence of logs:

Task 1 ended
Task 2 ended

But in case of existing user I only see the first line, so onSuccess() never called. Of course I could use ParseUser.signUpInBackground(SignUpCallback callback) and produce new Task depending on callback, but it's a bit uglier solution. So, the question is why signUpInBackground does not returns any errors?

1

There are 1 answers

0
Oleg Osipenko On

Finally I got this chain of Tasks:

private void loginUser() {
    fetchUserTask()
            .onSuccessTask(new Continuation<ParseUser, Task<Void>>() {
                @Override
                public Task<Void> then(Task<ParseUser> task) throws Exception {
                    if (task.isCompleted()) {
                        return task.getResult().signUpInBackground();
                    }
                    return null;
                }
            })
            .continueWithTask(new Continuation<Void, Task<ParseUser>>() {
                @Override
                public Task<ParseUser> then(Task<Void> task) throws Exception {
                    if (task.isFaulted()) {
                        return ParseUser.logInInBackground(mUserId, PASSWORD);
                    }
                    return null;
                }
            })
            .continueWith(new Continuation<ParseUser, Object>() {
                @Override
                public Object then(Task<ParseUser> task) throws Exception {
                    if (task.isCompleted()) {
                        mSuccessCallback.success();
                    }
                    return null;
                }
            });
}

If some error happened during execution of signUp in the second task I create another task where the user logs in.