Back

[SOLVED] How to catch on promise errors?

  • 0
  • Flutter
Ponguta_
7 Mar, 2023, 22:59

Hello again, sorry for bother you guys.

I'm getting this exception when trying to login. I put bad credentials on purpose.

I just want to know how to catch this exceptions, I have added already onError, and make it returns null. But it throws another exception.

This is my code:

TypeScript
  Future? login({required String email, required String password}) {
    try {
      return account
          .createEmailSession(email: email, password: password)
          .then((value) => value)
          .onError((error) => null); // <-- null is marked as error, Want to know with what can I replace that
    } catch (e) {
      print(e);
      return null;
    }
  }

Thanks for your help

TL;DR
The user is experiencing an exception when trying to catch promise errors in their code. They have tried using the `onError` method and returning null, but it throws another exception. They are seeking guidance on how to properly catch the exceptions. Solution: Instead of using `onError`, it is recommended to use `async/await` and `try/catch` for easier code readability. The user should rewrite their code using `async/await` and `try/catch`.
Drake
7 Mar, 2023, 23:09

As the error says, you must return a value of the future's type (in this case, session) and can't return null.

Btw, i highly suggest you avoid trying to mix and match then/onError with try/catch. When possible, only use async/await/try/catch as it makes code much easier to read

Ponguta_
8 Mar, 2023, 00:37

You point me in the right direction, using async await turns into more readable code and easy debug.

Thank you so much @Steven!

You are awesome

Drake
8 Mar, 2023, 01:01

[SOLVED] How to catch on promise errors?

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more