How to use the ocpp.exceptions.ProtocolError function in ocpp

To help you get started, we’ve selected a few ocpp examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mobilityhouse / ocpp / tests / test_messages.py View on Github external
def test_creating_exception_from_call_error():
    call_error = CallError(
        unique_id="1337",
        error_code="ProtocolError",
        error_description="Something went wrong",
        error_details="Some details about the error"
    )

    assert call_error.to_exception() == ProtocolError(
        description="Something went wrong",
        details="Some details about the error"
    )
github mobilityhouse / ocpp / tests / test_messages.py View on Github external
def test_unpack_without_jsonified_list():
    """
    OCPP messages are JSONified lists. This test make sure that the correct
    exception is raised when input is not a JSONified list.
    """
    with pytest.raises(ProtocolError):
        unpack(json.dumps('3'))
github mobilityhouse / ocpp / ocpp / messages.py View on Github external
def unpack(msg):
    """
    Unpacks a message into either a Call, CallError or CallResult.
    """
    try:
        msg = json.loads(msg)
    except json.JSONDecodeError as e:
        raise FormatViolationError(f'Message is not valid JSON: {e}')

    if not isinstance(msg, list):
        raise ProtocolError("OCPP message hasn't the correct format. It "
                            f"should be a list, but got {type(msg)} instead")

    for cls in [Call, CallResult, CallError]:
        try:
            if msg[0] == cls.message_type_id:
                return cls(*msg[1:])
        except IndexError:
            raise ProtocolError("Message doesn\'t contain MessageTypeID")

    raise PropertyConstraintViolationError(f"MessageTypeId '{msg[0]}' isn't "
                                           "valid")
github mobilityhouse / ocpp / ocpp / messages.py View on Github external
"""
    try:
        msg = json.loads(msg)
    except json.JSONDecodeError as e:
        raise FormatViolationError(f'Message is not valid JSON: {e}')

    if not isinstance(msg, list):
        raise ProtocolError("OCPP message hasn't the correct format. It "
                            f"should be a list, but got {type(msg)} instead")

    for cls in [Call, CallResult, CallError]:
        try:
            if msg[0] == cls.message_type_id:
                return cls(*msg[1:])
        except IndexError:
            raise ProtocolError("Message doesn\'t contain MessageTypeID")

    raise PropertyConstraintViolationError(f"MessageTypeId '{msg[0]}' isn't "
                                           "valid")