0

When I run my app with the code below, even if the responseData['error'] returns null, I still get an error that is thrown to my HttpException for error handling and it returns the catch portion of the try catch block of my error handling. Thus every time I test my signup authorization, which successfully enrolls in firebase, I get an alert popup saying "Could not authenticate you. Please try again later."

Provider catching the error

final response = await http.post(
        url,
        body: json.encode(
          {
            'email': email,
            'password': password,
            'returnSecureToken': true,
          },
        ),
      );
      final responseData = json.decode(response.body);
      print(responseData['error']);
      if (responseData['error'] != null) {
        throw HttpException(responseData['error']['message']);
      }
} catch (error) {
      throw error;
    }

Login/SignUp Screen

try {
      if (_authMode == AuthMode.Login) {
        // Log user in
        await Provider.of<AuthProvider>(context, listen: false).login(
          _authData['email'].toString(),
          _authData['password'].toString(),
        );
      } else {
        // Sign user up
        await Provider.of<AuthProvider>(context, listen: false).signup(
          _authData['email'].toString(),
          _authData['password'].toString(),
        );
      }
    } on HttpException catch (error) {
      var errorMessage = 'Authentication failed';
      if (error.toString().contains('EMAIL_EXISTS')) {
        errorMessage = 'This email address is already in use.';
      } else if (error.toString().contains('INVALID_EMAIL')) {
        errorMessage = 'This is not a valid email address';
      } else if (error.toString().contains('WEAK_PASSWORD')) {
        errorMessage = 'This password is too weak.';
      } else if (error.toString().contains('EMAIL_NOT_FOUND')) {
        errorMessage = 'Could not find a user with that email.';
      } else if (error.toString().contains('INVALID_PASSWORD')) {
        errorMessage = 'Invalid password.';
      }
      _showErrorDialog(errorMessage);
    } catch (error) {
      const errorMessage =
          'Could not authenticate you. Please try again later.';
      _showErrorDialog(errorMessage);
    }
Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
  • `catch (error) { throw error; }` PLEASE don't do this. You just threw away your stack trace. If you're not doing anything to recover in the catch, REMOVE the catch entirely. – Randal Schwartz Jul 02 '22 at 14:54
  • @RandalSchwartz- thanks! I have the bottom catch for any other errors that may occur (ie a network error). I guess I should just move that line of code up into the If statement as an ‘’’else’’’ – Peter Moran Jul 02 '22 at 15:49
  • No, just remove the part I referenced. Oh wait. You don't even need try then So remove try/catch entirely from that first code. – Randal Schwartz Jul 02 '22 at 15:54
  • I still got the error after removing the try/catch on the provider. Any other suggestions? – Peter Moran Jul 02 '22 at 21:16
  • Yes, my advice wasn't to fix your problem, but to restructure your code so you don't lose stacktraces. I'm betting you're getting an empty string for `responseData['error']` which of course is not null. Can you verify what's in that value? – Randal Schwartz Jul 02 '22 at 21:41
  • when I print responseData['error'], it actually does state null which is why I am confused – Peter Moran Jul 02 '22 at 21:46

0 Answers0