Skip to content

Conversation

@deltamikeromeo
Copy link

Enhance user authentication by adding more robust error handling, ensuring clearer feedback for users during login failures.

Defines custom exception classes for various Control4 error scenarios (Unauthorized, NotFound, BadCredentials, InvalidCategory, C4CorruptXMLResponse).
Implements checkResponseForError(response_text) to:
Detect and raise exceptions for known error messages in plain text, JSON, or XML responses from the Control4 Director.
Handles malformed XML responses and logs parsing errors.
Raises specific exceptions based on error codes or messages found in the response.

… (Unauthorized, NotFound, BadCredentials, InvalidCategory, C4CorruptXMLResponse).

Implements checkResponseForError(response_text) to:
Detect and raise exceptions for known error messages in plain text, JSON, or XML responses from the Control4 Director.
Handles malformed XML responses and logs parsing errors.
Raises specific exceptions based on error codes or messages found in the response.
@deltamikeromeo
Copy link
Author

In relation to #35

Copy link
Author

@deltamikeromeo deltamikeromeo left a comment

Choose a reason for hiding this comment

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

Format updates

Copy link
Owner

@lawtancool lawtancool left a comment

Choose a reason for hiding this comment

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

Thanks for submitting this PR! Overall your approach of decoding it as JSON, then trying it as XML makes sense. Just requested a few changes to make it better match the old behavior.

raise NotFound("404 Not Found from Control4 Director.")
if "error" in dictionary:
if "Invalid category" in dictionary["error"]:
raise InvalidCategory(dictionary["error"])
Copy link
Owner

Choose a reason for hiding this comment

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

In all the exception cases in this function, can you change the behavior so that it includes the entire response_text in the thrown exception? This would match the old behavior and make sure the entire response gets printed in the error logs

# Other error codes can be added here if necessary
else:
# Generic error for other codes
raise Exception(
Copy link
Owner

Choose a reason for hiding this comment

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

Raise C4Exception instead to match old behavior

Comment on lines +65 to +73
_LOGGER.error(
(
"Failed to parse XML response from Director due to a mismatched tag or other corruption. "
"The raw text received from the controller was: \n%s"
),
response_text,
)
# Re-raise the original error so the integration still fails as expected
raise e
Copy link
Owner

Choose a reason for hiding this comment

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

Instead of using _LOGGER is there a way to include this error message plus the ExpatError inside a C4CorruptXMLResponse instead? Right now it seems that C4CorruptXMLResponse is completely unused.

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