Back

The best approach for error handling

  • 1
  • General
  • Flutter
Tomic R.
30 Jul, 2023, 14:13

Hello! I am currently doing some error handling in my app and I noticed, that I can't find any good way to programatically check what the exact error was. Take the following code as an example

TypeScript
try {
  account = await _account.create(
    userId: ID.unique(),
    name: username,
    email: email,
    password: password,
  );
} on AppwriteException catch (e) {
    if(e.code == 400 && e.type == 'general_argument_invalid') {
        // Could be that the email is not provided correctly, that the password is not strong enough etc.
    }
}

The create account method can give multiple exceptions. I can access e.code with the response code and e.type. These two give me some information on what happened, but to really check what has happended I need to check e.message. But I don't feel like e.message is a good approach. The e.message is written in a good human readable form, making it difficult to check. E.g. the e.mesage for when the password is invalid would be: Invalid password: Password must be at least 8 characters and should not be one of the commonly used password. I can't just do e.message = 'Invalid password: Password must be at least 8 characters and should not be one of the commonly used password.', because this message is something that could change very fast in the sdk or somewhere else. So what is the recommended approach to handle specific errors?

TL;DR
The user is looking for the best approach to handle errors in their app. They are currently using a try-catch block to catch exceptions and want to programmatically check the exact error that occurred. They are concerned about relying on the e.message property as it may change in the future. The recommended approach is to use the e.code and e.type properties to identify the type of error. In the given example, the user checks if the code is 400 and the type is 'general_argument_invalid'. This can indicate that the email is not provided correctly or the password is not strong enough. Instead of relying solely on the e.message property,
Drake
30 Jul, 2023, 17:31

And what do you need to do after checking what kind of error it is?

Tomic R.
30 Jul, 2023, 22:31

Showing the user a snackbar that the mail is not provided correctly or that the password is not strong enough. I kno, e.message is written in a human-readable language and can be used, but what if I want to use other messages or want to translate it into other languages?

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