Some generic exceptions refactored into custom and specific exceptions

This commit is contained in:
Aurimas 2017-02-13 15:28:47 +01:00
parent f6ef04ed01
commit cac93b3b20
3 changed files with 70 additions and 5 deletions

View file

@ -175,7 +175,7 @@ namespace TLSharp.Core.Auth
if (!newNonceHash1.SequenceEqual(newNonceHashCalculated))
{
throw new InvalidOperationException("invalid new nonce hash");
throw new InvalidNewNonceHashException();
}
//logger.info("generated new auth key: {0}", gab);
@ -206,4 +206,11 @@ namespace TLSharp.Core.Auth
}
}
}
internal class InvalidNewNonceHashException : Exception
{
internal InvalidNewNonceHashException() : base($"invalid new nonce hash")
{
}
}
}

View file

@ -110,7 +110,7 @@ namespace TLSharp.Core.Network
using (var inputReader = new BinaryReader(inputStream))
{
if (inputReader.BaseStream.Length < 8)
throw new InvalidOperationException($"Can't decode packet");
throw new DecodePacketException();
ulong remoteAuthKeyId = inputReader.ReadUInt64(); // TODO: check auth key id
byte[] msgKey = inputReader.ReadBytes(16); // TODO: check msg_key correctness
@ -313,6 +313,15 @@ namespace TLSharp.Core.Network
{
throw new CloudPasswordNeededException("This Account has Cloud Password !");
}
else if (errorMessage == "AUTH_KEY_UNREGISTERED")
{
//
throw new AuthKeyUnregisteredException();
}
else if (errorMessage == "RPC_MCGET_FAIL")
{
throw new RpcMcGetFailException();
}
else
{
throw new InvalidOperationException(errorMessage);
@ -378,7 +387,7 @@ namespace TLSharp.Core.Network
case 20:
throw new InvalidOperationException("message too old, and it cannot be verified whether the server has received a message with this msg_id or not");
case 32:
throw new InvalidOperationException("msg_seqno too low (the server has already received a message with a lower msg_id but with either a higher or an equal and odd seqno)");
throw new MsgSeqnoTooLowException();
case 33:
throw new InvalidOperationException(" msg_seqno too high (similarly, there is a message with a higher msg_id but with either a lower or an equal and odd seqno)");
case 34:
@ -535,6 +544,39 @@ namespace TLSharp.Core.Network
}
}
internal class DecodePacketException : Exception
{
internal DecodePacketException() : base($"Can't decode packet.")
{
}
}
public class AuthKeyUnregisteredException : Exception
{
internal AuthKeyUnregisteredException() : base($"Auth key is unregistered.")
{
}
}
internal class RpcMcGetFailException : Exception
{
internal RpcMcGetFailException() : base($"Rpc Mc Get Fail.")
{
}
}
internal class MsgSeqnoTooLowException : Exception
{
internal MsgSeqnoTooLowException() : base($"msg_seqno too low (the server has already received a message with a lower msg_id but with either a higher or an equal and odd seqno)")
{
}
}
internal abstract class DataCenterMigrationException : Exception
{
internal int DC { get; private set; }

View file

@ -28,7 +28,7 @@ namespace TLSharp.Core.Network
public async Task Send(byte[] packet)
{
if (!_tcpClient.Connected)
throw new InvalidOperationException("Client not connected to server.");
throw new TcpClientNotConnectedException();
var tcpMessage = new TcpMessage(sendCounter, packet);
@ -42,7 +42,7 @@ namespace TLSharp.Core.Network
var packetLengthBytes = new byte[4];
if (await stream.ReadAsync(packetLengthBytes, 0, 4) != 4)
throw new InvalidOperationException("Couldn't read the packet length");
throw new TcpClientCouldntReadPacketLengthException();
int packetLength = BitConverter.ToInt32(packetLengthBytes, 0);
var seqBytes = new byte[4];
@ -92,4 +92,20 @@ namespace TLSharp.Core.Network
_tcpClient.Close();
}
}
internal class TcpClientNotConnectedException : Exception
{
internal TcpClientNotConnectedException() : base($"Client not connected to server.")
{
}
}
internal class TcpClientCouldntReadPacketLengthException : Exception
{
internal TcpClientCouldntReadPacketLengthException() : base($"Couldn't read the packet length")
{
}
}
}