Akres
Akres

Reputation: 124

Print message of exception inside a ParseException

I've got a simple log in/signup form for an app I'm developing as part of a course. I'm using ParseUser to sign up users and log them in.

When the user enters a username that already exists, a Toast is shown...

    ParseUser user = new ParseUser();
    user.setUsername(username);
    user.setPassword(password);

    user.signUpInBackground(new SignUpCallback() {
        @Override
        public void done(ParseException e) {

            if (e == null) {

                Toast.makeText(MainActivity.this, "Sign up successful!", Toast.LENGTH_SHORT).show();

            } else {

                Log.i("Sign up", "failed. Reason - " + e.toString());
                Toast.makeText(MainActivity.this, "Sign up failed. " + e.getMessage(), Toast.LENGTH_SHORT).show();

            }
        }
    });

...with the error message from e.getMessage(), e.g., "Sign up failed. Account already exists for this username."

However, if the user does not enter anything, i.e., leaves either the pw or username field blank, I get this in the app

and the following in the logs: I/Sign up: failed. Reason - com.parse.ParseException: java.lang.IllegalArgumentException: Username cannot be missing or blank

What I want to see is only the error message Username cannot be missing or blank, which is the ".getMessage()" of the second exception java.lang.IllegalArgumentException

How would I go about doing that? Thanks!

Upvotes: 2

Views: 795

Answers (1)

Austin Schaefer
Austin Schaefer

Reputation: 696

There are two main options I see here:

  1. Get the cause using getCause(), and call getMessage() on the cause, rather than from the top level exception itself.
  2. Manually format the string by getting the last instance of the "Exception" substring and discarding everything before (and including) it; this should probably be used as a last-resort option since it increases complexity and decreases readability.

Upvotes: 2

Related Questions