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

Log bare exceptions in the config flow #135584

Merged
merged 6 commits into from
Mar 25, 2025
Merged

Conversation

joostlek
Copy link
Member

@joostlek joostlek commented Jan 14, 2025

Proposed change

So I found out that BLE001 does not get flagged if the exception is logged using LOGGER.exception, which is a good thing I think. So let's get all the BLE001 noqas out of the codebase!

By logging the exception stacktrace while encountering an unknown exception, we use this to provide more info to the user than just unknown error. If we want more specific errors, we should just catch more specific

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.

To help with the load of incoming pull requests:

@home-assistant
Copy link

Hey there @disforw, mind taking a look at this pull request as it has been labeled with an integration (qnap) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of qnap can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign qnap Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

@home-assistant
Copy link

Hey there @gagebenne, mind taking a look at this pull request as it has been labeled with an integration (dexcom) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of dexcom can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign dexcom Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

@home-assistant
Copy link

Hey there @home-assistant/matter, mind taking a look at this pull request as it has been labeled with an integration (matter) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of matter can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign matter Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

@home-assistant
Copy link

Hey there @ManneW, mind taking a look at this pull request as it has been labeled with an integration (vilfo) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of vilfo can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign vilfo Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

@home-assistant
Copy link

Hey there @tkdrob, mind taking a look at this pull request as it has been labeled with an integration (deluge) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of deluge can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign deluge Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

Comment on lines +92 to 96
except Exception as ex:
_LOGGER.exception("Unexpected error")
if type(ex).__name__ == "BadLoginError":
return "invalid_auth"
return "unknown"
Copy link
Member Author

Choose a reason for hiding this comment

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

I am genuinely wondering why we don't do except BadLoginError. I also could not find a reference in the library. @tkdrob Can you maybe give a hint on this?

Copy link
Member

Choose a reason for hiding this comment

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

Seems maybe it was still supporting py2 at the time this was done.
Could be fixed now I guess.

Comment on lines 225 to 227
except Exception:
LOGGER.exception("Unexpected exception")
errors["base"] = "unknown"
Copy link
Member Author

Choose a reason for hiding this comment

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

Opened an issue for this one as it should work since we log the exception

astral-sh/ruff#15473

jpelgrom added a commit to jpelgrom/home-assistant-core that referenced this pull request Jan 15, 2025
@joostlek joostlek marked this pull request as ready for review January 18, 2025 10:59
@joostlek joostlek requested a review from scop as a code owner January 18, 2025 10:59
Copy link
Contributor

@jbouwh jbouwh left a comment

Choose a reason for hiding this comment

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

LGTM,
Thanks @joostlek 👍

@gjohansson-ST
Copy link
Member

I'm not entirely sure. Is there really a benefit of the logging instead?

@joostlek
Copy link
Member Author

How do you mean instead? We use the bare exception to at least show the user a proper error when something happens, and ideally those exceptions are the one we really do not expect. Everything we do expect, we should catch and handle appropriately.

This will at least leave more information behind then "🤷🏻 unknown error happened" and will in the end help people catch more exceptions and make them more specific

@gjohansson-ST
Copy link
Member

How do you mean instead? We use the bare exception to at least show the user a proper error when something happens, and ideally those exceptions are the one we really do not expect. Everything we do expect, we should catch and handle appropriately.

Sorry, meant also and not instead.
But since this is only on occasions of actually trying to setup/configure an entry, I guess it makes sense.

Comment on lines -191 to +192
except Exception: # noqa: BLE001
_LOGGER.debug("Disconnect error", exc_info=True)
except Exception:
_LOGGER.exception("Disconnect error")
Copy link
Member

Choose a reason for hiding this comment

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

I think this will add some unnecessary noise to logs, as there's likely nothing we can generally do if the disconnect fails, and IIRC it is or at least was a common phenomenon with some boxes/firmwares/control flows. I'd be fine with logging the exceptions we know we can do nothing about and that are known to be harmless with .debug and others with .exception (I can't tell which of them are such offhand, and don't have time to figure out/work on this any time soon), but I'm not quite convinced this change would be an improvement as is.

Copy link
Member Author

Choose a reason for hiding this comment

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

But I think if there are exceptions that we did not catch, which do have a reason, we should be clearer to the user. For example if we suddenly get a TimeoutError, either tell the user we cannot connect, or we timed out, both are better than just "Unexpected error".

These excepts are usually just here to make sure the user is guaranteed not to see a "500 - invalid flow specified" (which I think is also something we can fix in the frontend to be more graceful)

Copy link
Member

@edenhaus edenhaus Mar 4, 2025

Choose a reason for hiding this comment

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

I agree with Joost.
I also don't expect that the noise to the logs will increase that much as all exceptions are unexpected, and they should be rare.
If an exception is raised often you should catch this one explicit and handle it differently

@home-assistant
Copy link

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@home-assistant home-assistant bot marked this pull request as draft January 19, 2025 13:12
Copy link
Contributor

@emontnemery emontnemery left a comment

Choose a reason for hiding this comment

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

The PR implementation is mostly good, but the title and description need some TLC

@@ -86,7 +89,8 @@ async def validate_input(self, user_input: dict[str, Any]) -> str | None:
await self.hass.async_add_executor_job(api.connect)
except (ConnectionRefusedError, TimeoutError, SSLError):
return "cannot_connect"
except Exception as ex: # noqa: BLE001
except Exception as ex:
_LOGGER.exception("Unexpected error")
Copy link
Contributor

Choose a reason for hiding this comment

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

Move this log down to after we handle the BadLoginError

Copy link
Member Author

Choose a reason for hiding this comment

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

I can't seem to find a BadLoginError in the code at all, so this is something to look into

@joostlek joostlek marked this pull request as ready for review March 4, 2025 11:07
@home-assistant home-assistant bot requested a review from scop March 4, 2025 11:07
@frenck
Copy link
Member

frenck commented Mar 25, 2025

@joost there is a merge conflict, can you take a look?

@frenck frenck marked this pull request as draft March 25, 2025 08:55
# Conflicts:
#	homeassistant/components/tado/config_flow.py
#	homeassistant/components/webdav/config_flow.py
@joostlek joostlek marked this pull request as ready for review March 25, 2025 09:00
Copy link
Member

@frenck frenck left a comment

Choose a reason for hiding this comment

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

Thanks, @joostlek 👍

../Frenck

@frenck frenck merged commit 615afeb into home-assistant:dev Mar 25, 2025
77 of 78 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Mar 26, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants