Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve authentication error handling in config flow #246

Merged
merged 1 commit into from
Feb 19, 2024

Conversation

pdcastro
Copy link
Contributor

@pdcastro pdcastro commented Feb 9, 2024

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 [email protected] 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.

@@ -6,6 +6,7 @@

import voluptuous as vol
from weconnect import weconnect
from weconnect.errors import AuthentificationError
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That spelling is not a typo of mine! It is how it reads in the library:
https://github.com/tillsteinbach/WeConnect-python/blob/v0.59.5/weconnect/errors.py#L17

Comment on lines -90 to -91
class InvalidAuth(HomeAssistantError):
"""Error to indicate there is invalid auth."""
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This exception class was defined but not raised anywhere, and therefore not capable of being caught through except InvalidAuth. I assume it was connected to that comment earlier in the file:

# TODO: ADD Validation on credentials

@mitch-dc mitch-dc merged commit 9cd2c06 into mitch-dc:main Feb 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants