It turns out there is much more code duplication regarding
error message reporting. Fix that so the error message
strings appear in the code only once and are reused for
both, the exception and the log file. As a result the new
code is more readable and less confusing.
Change-Id: Ib72b7de11648b718e2db41ee1d576ad1993cbc10
Signed-off-by: Jozef Behran <jbehran@cisco.com>
# Some validation.
if len(msg_in) < 37:
# 37 is minimal length of open message with 4-byte AS number.
# Some validation.
if len(msg_in) < 37:
# 37 is minimal length of open message with 4-byte AS number.
- logger.error("Got something else than open with 4-byte AS number: " +
- binascii.hexlify(msg_in))
- raise MessageError("Got something else than open with 4-byte AS number", msg_in)
+ error_msg = (
+ "Message length (" + str(len(msg_in)) + ") is smaller than "
+ "minimal length of OPEN message with 4-byte AS number (37)"
+ )
+ logger.error(error_msg + ": " + binascii.hexlify(msg_in))
+ raise MessageError(error_msg, msg_in)
# TODO: We could check BGP marker, but it is defined only later;
# decide what to do.
reported_length = get_short_int_from_message(msg_in)
if len(msg_in) != reported_length:
# TODO: We could check BGP marker, but it is defined only later;
# decide what to do.
reported_length = get_short_int_from_message(msg_in)
if len(msg_in) != reported_length:
- logger.error("Message length is not " + str(reported_length) +
- " as stated in " + binascii.hexlify(msg_in))
- raise MessageError("Message length is not " + reported_length +
- " as stated in ", msg_in)
+ error_msg = (
+ "Expected message length (" + reported_length +
+ ") does not match actual length (" + str(len(msg_in)) + ")"
+ )
+ logger.error(error_msg + binascii.hexlify(msg_in))
+ raise MessageError(error_msg, msg_in)
logger.info("Open message received.")
return msg_in
logger.info("Open message received.")
return msg_in
# Using exact keepalive length to not to see possible updates.
msg_in = bgp_socket.recv(19)
if msg_in != generator.keepalive_message():
# Using exact keepalive length to not to see possible updates.
msg_in = bgp_socket.recv(19)
if msg_in != generator.keepalive_message():
- error_msg = (
- "Open not confirmed by keepalive, instead got " +
- binascii.hexlify(msg_in)
- )
- logger.error(error_msg)
- raise MessageError(error_msg)
+ error_msg = "Open not confirmed by keepalive, instead got"
+ logger.error(error_msg + ": " + binascii.hexlify(msg_in))
+ raise MessageError(error_msg, msg_in)
timer.reset_peer_hold_time()
# Send the keepalive to indicate the connection is accepted.
timer.snapshot() # Remember this time.
timer.reset_peer_hold_time()
# Send the keepalive to indicate the connection is accepted.
timer.snapshot() # Remember this time.