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

Amend validation error raised #506

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
11 changes: 7 additions & 4 deletions ocpp/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

from ocpp.exceptions import (
FormatViolationError,
NotImplementedError,
OCPPError,
PropertyConstraintViolationError,
ProtocolError,
Expand Down Expand Up @@ -217,9 +216,13 @@ def validate_payload(message: Union[Call, CallResult], ocpp_version: str) -> Non
validator = get_validator(
message.message_type_id, message.action, ocpp_version
)
except (OSError, json.JSONDecodeError):
raise NotImplementedError(
details={"cause": f"Failed to validate action: {message.action}"}
except OSError:
raise ValidationError(
f"JSON validation schema not found for action: {message.action}"
)
except json.JSONDecodeError:
raise ValidationError(
f"Error in decoding JSON validation schema for action: {message.action}"
Copy link
Contributor

Choose a reason for hiding this comment

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

Only a json.JSONDecodeError should be mapped to a ValidationError. An OSError means that no schema with a given name was found. That means user is using a action that doesn't exists. Therefore, a NotSupportedError is applicable. See my comment here: #494 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

With #494 a FileNotFoundError should not be raised by the OSError exception, as before it gets to this part of the code a NotSupportedError will be raised by_raise_key_error. So an OSError should only be raised for one of these other reasons https://docs.python.org/3/library/exceptions.html#os-exceptions

)

try:
Expand Down
3 changes: 1 addition & 2 deletions tests/test_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

from ocpp.exceptions import (
FormatViolationError,
NotImplementedError,
PropertyConstraintViolationError,
ProtocolError,
TypeConstraintViolationError,
Expand Down Expand Up @@ -240,7 +239,7 @@ def test_validate_payload_with_non_existing_schema():
payload={"invalid_key": True},
)

with pytest.raises(NotImplementedError):
with pytest.raises(ValidationError):
validate_payload(message, ocpp_version="1.6")


Expand Down
Loading