Skip to content

Conversation

@andrew-blake
Copy link

PR to detect if a login 403 error and throw a more obvious exception message than a JSON decode error.
Helps address issue #49

@indykoning
Copy link
Owner

Thank you! i agree that this is something that should be addressed because the current messages are not clear.
I'd prefer to leave the error messages to the implementation of this package though.

While reviewing i found response.raise_for_status()
https://requests.readthedocs.io/en/latest/user/quickstart/#response-status-codes
This seems like the best solution to me, since it will throw the proper exception for the different cases automatically.
Thus applications implementing this package can show their own customized, translated messages for the different error codes

@andrew-blake andrew-blake force-pushed the dev_handle_login_error branch from 11f04bf to 797e531 Compare January 2, 2023 20:24
@andrew-blake
Copy link
Author

I hadn't noticed response.raise_for_status() before - that will come in useful elsewhere, thanks.

I've updated the PR to use it (forced pushed).

I've noticed Growatt have updated their WAF to block the default User Agent, so I think it makes sense to add a specific GrowattAPI exception for that. I've proposed growattServer.GrowattApiUserAgentBlockedException, with the generic requests.exceptions.HTTPError for all other HTTP level issues.

Also updated the PR to detect GrowattApiIncorrectPasswordException and GrowattApiUnknownException for unknown JSON responses.

@indykoning
Copy link
Owner

Is it okay if i implement the raise_for_status, and think about the other exceptions a little longer?

I'm afraid of losing context for the developer using it instead of gaining it.
E.g. https://github.com/home-assistant/core/blob/dev/homeassistant/components/growatt_server/config_flow.py#L60 would need to update their implementation of checking login status, however they wouldn't be able to check the return message anymore in the exception.

I think the raise_for_status will not cause a breaking change (If it will not raise a http exception it will raise a json decode exception) but the check for success will

@muppet3000
Copy link
Contributor

Is it okay if i implement the raise_for_status, and think about the other exceptions a little longer?

I'm afraid of losing context for the developer using it instead of gaining it. E.g. https://github.com/home-assistant/core/blob/dev/homeassistant/components/growatt_server/config_flow.py#L60 would need to update their implementation of checking login status, however they wouldn't be able to check the return message anymore in the exception.

I think the raise_for_status will not cause a breaking change (If it will not raise a http exception it will raise a json decode exception) but the check for success will

I really like this approach @indykoning - Good catch on the Home Assistant implementation. I think throwing the exception for users of the library to handle is a really good idea. It should also mean that we can handle the scenarios when the servers are unavailable in a more consistent manner as well.

… is incorrect

Throw GrowattApiUserAgentBlockedException if User Agent is being blocked (HTTP 403 error)
Throw requests.exceptions.HTTPError for other API HTTP errors
Throw GrowattApiUnknownException for unknown JSON response errors
@andrew-blake andrew-blake force-pushed the dev_handle_login_error branch from 797e531 to f46f7ee Compare April 1, 2023 14:46
@marcovtwout
Copy link
Contributor

@indykoning Could you implement the basic raise_for_status as you suggested? That might make future debugging easier as for example HTTP 405 occurs more and more often.

@marcovtwout
Copy link
Contributor

marcovtwout commented Feb 13, 2024

If I understand correctly raising exceptions was already implemented in 1.3.1 and this PR should be closed: 667a520

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.

4 participants