mitch-dc / volkswagen_we_connect_id

Apache License 2.0
196 stars 52 forks source link

Improve authentication error handling in config flow #246

Closed pdcastro closed 7 months ago

pdcastro commented 7 months ago

Before this PR, entering an incorrect username or password in the config flow would cause the form to display “Unexpected error”, while the hass logs would print “Unexpected exception” and a stack trace:

Unexpected Error

2024-02-09 18:25:24.786 ERROR (MainThread) [custom_components.volkswagen_we_connect_id.config_flow] Unexpected exception
Traceback (most recent call last):
  File "/home/vscode/.homeassistant/custom_components/volkswagen_we_connect_id/config_flow.py", line 62, in async_step_user
    info = await validate_input(self.hass, user_input)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.homeassistant/custom_components/volkswagen_we_connect_id/config_flow.py", line 37, in validate_input
    await hass.async_add_executor_job(we_connect.login)
  File "/usr/local/lib/python3.12/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.local/lib/python3.12/site-packages/weconnect/weconnect.py", line 187, in login
    self.__session.login()
  File "/home/vscode/.local/lib/python3.12/site-packages/weconnect/auth/we_connect_session.py", line 76, in login
    response = self.doWebAuth(authorizationUrl)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.local/lib/python3.12/site-packages/weconnect/auth/we_connect_session.py", line 198, in doWebAuth
    raise AuthentificationError(f'Error during login, account {self.sessionuser.username} does not exist')
weconnect.errors.AuthentificationError: Error during login, account foo@example.com does not exist

The last line above might also be:

weconnect.errors.AuthentificationError: Error during login, email invalid

After this PR, nothing is printed in the hass logs (at least not with my default log level), and the user interface displays a seemingly hass-standard “Invalid authentication” message:

Invalid Authentication

The reason why I was messing with that is that I was testing my code changes in another PR, #245.