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

Add ServiceValidationError and translation support #102592

Merged
merged 16 commits into from
Nov 6, 2023

Conversation

jbouwh
Copy link
Contributor

@jbouwh jbouwh commented Oct 23, 2023

Proposed change

  • Add new ServiceValidationError for suppressing stack traces when an expected and catches validation error occured during a service call.
  • Add translation support to HomeAssistantError and sub classes.
  • Add example for MQTT ServiceValidationError

Implements step 1 and partly step 3 of #102410
See also discussion: home-assistant/architecture#992

Translations need frontend PR: home-assistant/frontend#18447

image

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

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 Black (black --fast 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.
  • Untested files have been added to .coveragerc.

To help with the load of incoming pull requests:

@home-assistant
Copy link

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

Code owner commands

Code owners of websocket_api 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 websocket_api Removes the current integration label and assignees on the pull request, add the integration domain after the command.

@jbouwh
Copy link
Contributor Author

jbouwh commented Oct 23, 2023

Sorry for the unrelated commits

@jbouwh jbouwh changed the base branch from stacktraces-on-expected-errors to dev October 23, 2023 16:24
@jbouwh jbouwh force-pushed the jbouwh-service-validation-error branch from 216fefb to c267160 Compare October 23, 2023 18:10
@jbouwh jbouwh marked this pull request as ready for review October 24, 2023 10:35
@jbouwh jbouwh requested a review from a team as a code owner October 24, 2023 10:35
@jbouwh jbouwh changed the title Add ServiceValidationError with translation support Add ServiceValidationError and translation support Oct 24, 2023
@jbouwh jbouwh marked this pull request as draft October 27, 2023 07:56
@jbouwh jbouwh force-pushed the jbouwh-service-validation-error branch from 0655313 to 20fa883 Compare October 27, 2023 10:46
homeassistant/components/websocket_api/connection.py Outdated Show resolved Hide resolved
homeassistant/components/websocket_api/messages.py Outdated Show resolved Hide resolved
homeassistant/components/websocket_api/commands.py Outdated Show resolved Hide resolved
homeassistant/components/websocket_api/strings.json Outdated Show resolved Hide resolved
"code": code,
"message": message,
}
if translation_key is not None:
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need to guard?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

May be we don't need to do that

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tried to remove it, but reverted that, it seems a lot of CI tests assert on the whole payload. If we leave the guard, we need to adjust all the tests.
I suggest that if this is needed we do this in a different PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

We shouldn't complicate the code to avoid updating tests. Check with frontend developers if they prefer to have the keys always there or only there when needed.
There's no need consider saving data here IMHO since sending error responses is not the common case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, I do not think it matters in the frontend, as there is no explit typing for the error responses. But we need to update quite a few tests. I would prefer to remove the guard in a different PR to avoid this one to become even bigger.
I asked Paul, if he says we should not add it, then we do not need to change is, but I assume he'll say that it does not matter.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had a discussion with @piitaya on Discord. He believes it is the right way to by only adding the translation attributes when there translation information is defined/available for the exception.

homeassistant/components/homeassistant/strings.json Outdated Show resolved Hide resolved
homeassistant/components/websocket_api/commands.py Outdated Show resolved Hide resolved
homeassistant/components/websocket_api/messages.py Outdated Show resolved Hide resolved
homeassistant/helpers/entity_platform.py Outdated Show resolved Hide resolved
homeassistant/helpers/entity_platform.py Outdated Show resolved Hide resolved
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.

I think this is great! Just a minor remark on the language.

homeassistant/components/websocket_api/commands.py Outdated Show resolved Hide resolved
homeassistant/components/websocket_api/strings.json Outdated Show resolved Hide resolved
@emontnemery emontnemery added the second-opinion-wanted Add this label when a reviewer needs a second opinion from another member. label Oct 28, 2023
@emontnemery
Copy link
Contributor

@MartinHjelmare Could you take another look at the PR too?

connection.logger.debug("", exc_info=err)
connection.send_error(
msg["id"],
const.ERR_HOME_ASSISTANT_ERROR,
Copy link
Member

Choose a reason for hiding this comment

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

Why do we use the same code for all different exceptions, besides vol.Invalid? How does the frontend use the code parameter?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I used ERR_HOME_ASSISTANT_ERROR because ServiceNotFound is a subclass of HomeAssistantError and the frontend does not seem to use the error code. But I could make one for ServiceValidationError I guess.

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure. I think we need to define what this parameter means and frame it to know what the value should be.

Copy link
Contributor Author

@jbouwh jbouwh Oct 30, 2023

Choose a reason for hiding this comment

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

I made a seperate code for it: service_validation_error. I checked, but it is not used in the frontend yet. Only in a special case it seems to be used:

try {
      await fetchCloudAlexaEntity(this.hass, this.entityId);
    } catch (err: any) {
      if (err.code === "not_supported") {
        this._unsupported["cloud.alexa"] = true;
        this.requestUpdate("_unsupported");
      }
    }

Copy link
Contributor Author

@jbouwh jbouwh Oct 30, 2023

Choose a reason for hiding this comment

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

I'm not sure. I think we need to define what this parameter means and frame it to know what the value should be.

If has to have a direct relation with the frontend code, then it seems only not_supported has that.
When we are actually redesigning the frontend UX, then the code could be useful to classify the error type.
If we want to define what this parameter means, do you think that should effect this PR? Or is this something to document / rework in a follow up PR?

Copy link
Member

@MartinHjelmare MartinHjelmare Oct 31, 2023

Choose a reason for hiding this comment

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

Offline discussion has concluded that, the current definition of the error code is a string that should be unique per error per websocket command, ie not globally unique. We should be able to distinguish errors using the error code for a websocket command.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I suppose we can answer the discussion home-assistant/architecture#992 now

Copy link
Contributor

@emontnemery emontnemery Oct 31, 2023

Choose a reason for hiding this comment

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

I don't think it's a reasonable requirement that for each possible thing that can go wrong in core when handling a websocket command there's a unique code, but maybe that's not what you mean @MartinHjelmare?

Also, we should clarify the relation between message and translatable error message. Are both needed, or should message be None when there's a translatable error message?

Copy link
Member

Choose a reason for hiding this comment

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

The codes don't have to be globally unique as I wrote above. Per command it should be possible to distinguish different errors that are returned and the same error should always use the same code for that command.

Copy link
Member

Choose a reason for hiding this comment

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

It's up to the developer to decide what error to return when something goes wrong, ie what error the thing going wrong should be labeled as.

homeassistant/components/websocket_api/messages.py Outdated Show resolved Hide resolved
@home-assistant home-assistant bot marked this pull request as draft October 30, 2023 14:35
@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.

@MartinHjelmare MartinHjelmare removed the second-opinion-wanted Add this label when a reviewer needs a second opinion from another member. label Oct 30, 2023
@jbouwh jbouwh marked this pull request as ready for review October 30, 2023 15:50
@jbouwh jbouwh force-pushed the jbouwh-service-validation-error branch from 80cde44 to 496b83e Compare November 2, 2023 22:09
@jbouwh jbouwh merged commit 54cf701 into dev Nov 6, 2023
50 checks passed
@jbouwh jbouwh deleted the jbouwh-service-validation-error branch November 6, 2023 14:45
@github-actions github-actions bot locked and limited conversation to collaborators Nov 7, 2023
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.

3 participants