Merge pull request #328 from NIC619/another_error_handling

Some minor error handlings
This commit is contained in:
Kevin Mai-Husan Chia 2019-10-24 14:53:19 +08:00 committed by GitHub
commit 73251a0c36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 31 additions and 7 deletions

View File

@ -21,3 +21,7 @@ class MissingLengthException(MsgioException):
class MissingMessageException(MsgioException):
pass
class DecryptionFailedException(MsgioException):
pass

View File

@ -3,7 +3,7 @@ from typing import Any, Dict, List, Optional, Sequence
from multiaddr import Multiaddr
from .id import ID
from .peerdata import PeerData
from .peerdata import PeerData, PeerDataError
from .peerinfo import PeerInfo
from .peerstore_interface import IPeerStore
@ -54,7 +54,10 @@ class PeerStore(IPeerStore):
def get(self, peer_id: ID, key: str) -> Any:
if peer_id in self.peer_map:
try:
val = self.peer_map[peer_id].get_metadata(key)
except PeerDataError as error:
raise PeerStoreError(error)
return val
raise PeerStoreError("peer ID not found")

View File

@ -1,5 +1,6 @@
from typing import Optional
from libp2p.crypto.exceptions import MissingDeserializerError
from libp2p.crypto.keys import PrivateKey, PublicKey
from libp2p.crypto.pb import crypto_pb2
from libp2p.crypto.serialization import deserialize_public_key
@ -82,6 +83,8 @@ class InsecureSession(BaseSession):
raise HandshakeFailure(
f"unknown `key_type` of remote_msg.pubkey={remote_msg.pubkey}"
)
except MissingDeserializerError as error:
raise HandshakeFailure(error)
peer_id_from_received_pubkey = ID.from_pubkey(received_pubkey)
if peer_id_from_received_pubkey != received_peer_id:
raise HandshakeFailure(

View File

@ -28,3 +28,7 @@ class IncompatibleChoices(SecioException):
class InconsistentNonce(SecioException):
pass
class SedesException(SecioException):
pass

View File

@ -11,12 +11,14 @@ from libp2p.crypto.authenticated_encryption import (
from libp2p.crypto.authenticated_encryption import (
initialize_pair as initialize_pair_for_encryption,
)
from libp2p.crypto.authenticated_encryption import InvalidMACException
from libp2p.crypto.authenticated_encryption import MacAndCipher as Encrypter
from libp2p.crypto.ecc import ECCPublicKey
from libp2p.crypto.exceptions import MissingDeserializerError
from libp2p.crypto.key_exchange import create_ephemeral_key_pair
from libp2p.crypto.keys import PrivateKey, PublicKey
from libp2p.crypto.serialization import deserialize_public_key
from libp2p.io.exceptions import IOException
from libp2p.io.exceptions import DecryptionFailedException, IOException
from libp2p.io.msgio import MsgIOReadWriter
from libp2p.network.connection.raw_connection_interface import IRawConnection
from libp2p.peer.id import ID as PeerID
@ -30,6 +32,7 @@ from .exceptions import (
InvalidSignatureOnExchange,
PeerMismatchException,
SecioException,
SedesException,
SelfEncryption,
)
from .pb.spipe_pb2 import Exchange, Propose
@ -122,7 +125,11 @@ class SecureSession(BaseSession):
async def read_msg(self) -> bytes:
msg = await self.conn.read_msg()
return self.remote_encrypter.decrypt_if_valid(msg)
try:
decrypted_msg = self.remote_encrypter.decrypt_if_valid(msg)
except InvalidMACException:
raise DecryptionFailedException
return decrypted_msg
async def write(self, data: bytes) -> int:
await self.write_msg(data)
@ -163,7 +170,10 @@ class Proposal:
nonce = protobuf.rand
public_key_protobuf_bytes = protobuf.public_key
try:
public_key = deserialize_public_key(public_key_protobuf_bytes)
except MissingDeserializerError as error:
raise SedesException(error)
exchanges = protobuf.exchanges
ciphers = protobuf.ciphers
hashes = protobuf.hashes

View File

@ -146,7 +146,7 @@ class MplexStream(IMuxedStream):
:return: number of bytes written
"""
if self.event_local_closed.is_set():
raise MplexStreamClosed(f"cannot write to closed stream: data={data}")
raise MplexStreamClosed(f"cannot write to closed stream: data={data!r}")
flag = (
HeaderTags.MessageInitiator
if self.is_initiator

View File

@ -77,7 +77,7 @@ async def read_delim(reader: Reader) -> bytes:
raise ParseError(f"`len(msg_bytes)` should not be 0")
if msg_bytes[-1:] != b"\n":
raise ParseError(
f'`msg_bytes` is not delimited by b"\\n": `msg_bytes`={msg_bytes}'
f'`msg_bytes` is not delimited by b"\\n": `msg_bytes`={msg_bytes!r}'
)
return msg_bytes[:-1]