Errors
class Errors
Create exceptions for common error cases.
using System;
using System.Globalization;
using System.IO;
using System.Security.Cryptography;
namespace Azure.Storage
{
internal class Errors
{
public static ArgumentException AccountMismatch(string accountNameCredential, string accountNameValue)
{
return new ArgumentException(string.Format(CultureInfo.CurrentCulture, "Account Name Mismatch: {0} != {1}", accountNameCredential, accountNameValue));
}
public static InvalidOperationException AccountSasMissingData()
{
return new InvalidOperationException("Account SAS is missing at least one of these: ExpiryTime, Permissions, Service, or ResourceType");
}
public static ArgumentNullException ArgumentNull(string paramName)
{
return new ArgumentNullException(paramName);
}
public static ArgumentException InvalidArgument(string paramName)
{
return new ArgumentException(paramName + " is invalid");
}
public static ArgumentException InvalidResourceType(char s)
{
return new ArgumentException($"""{s}""");
}
public static InvalidOperationException TaskIncomplete()
{
return new InvalidOperationException("Task is not completed");
}
public static FormatException InvalidFormat(string err)
{
return new FormatException(err);
}
public static ArgumentException ParsingConnectionStringFailed()
{
return new ArgumentException("Connection string parsing error");
}
public static ArgumentOutOfRangeException InvalidSasProtocol(string protocol, string sasProtocol)
{
return new ArgumentOutOfRangeException(protocol, "Invalid " + sasProtocol + " value");
}
public static ArgumentException InvalidService(char s)
{
return new ArgumentException($"""{s}""");
}
public static ArgumentException InsufficientStorageTransferOptions(long streamLength, long statedMaxBlockSize, long necessaryMinBlockSize)
{
return new ArgumentException($"""{streamLength}""{statedMaxBlockSize}""{necessaryMinBlockSize}""");
}
public static InvalidDataException HashMismatch(string hashHeaderName)
{
return new InvalidDataException(hashHeaderName + " did not match hash of recieved data.");
}
public static InvalidDataException ChecksumMismatch(ReadOnlySpan<byte> left, ReadOnlySpan<byte> right)
{
return new InvalidDataException("Compared checksums did not match. Invalid data may have been written to the destination. Left: " + Convert.ToBase64String(left.ToArray()) + " Right: " + Convert.ToBase64String(right.ToArray()));
}
public static InvalidDataException HashMismatchOnStreamedDownload(string mismatchedRange)
{
return new InvalidDataException("Detected invalid data while streaming to the destination. Range " + mismatchedRange + " produced mismatched checksum.");
}
public static ArgumentException PrecalculatedHashNotSupportedOnSplit()
{
return new ArgumentException("Precalculated checksum not supported when potentially partitioning an upload.");
}
public static ArgumentException CannotDeferTransactionalHashVerification()
{
return new ArgumentException("Cannot defer transactional hash verification. Returned hash is unavailable to caller.");
}
public static ArgumentException CannotInitializeWriteStreamWithData()
{
return new ArgumentException("Initialized buffer for StorageWriteStream must be empty.");
}
internal static void VerifyStreamPosition(Stream stream, string streamName)
{
if (stream != null && stream.CanSeek && stream.Length > 0 && stream.Position >= stream.Length)
throw new ArgumentException(streamName + ".Position must be less than " + streamName + ".Length. Please set " + streamName + ".Position to the start of the data to upload.");
}
public static void ThrowIfParamNull(object obj, string paramName)
{
if (obj == null)
throw new ArgumentNullException("Param: \"" + paramName + "\" is null");
}
internal static void CheckCryptKeySize(int keySizeInBytes)
{
if (keySizeInBytes != 16 && keySizeInBytes != 24 && keySizeInBytes != 32)
throw new CryptographicException("Specified key is not a valid size for this algorithm.");
}
public static CryptographicException CryptographyAuthTagMismatch()
{
throw new CryptographicException("The computed authentication tag did not match the input authentication tag.");
}
public static ArgumentException CryptographyPlaintextCiphertextLengthMismatch()
{
throw new ArgumentException("Plaintext and ciphertext must have the same length.");
}
public static ArgumentException CryptographyInvalidNonceLength()
{
throw new ArgumentException("The specified nonce is not a valid size for this algorithm.");
}
public static ArgumentException CryptographyInvalidTagLength()
{
throw new ArgumentException("The specified tag is not a valid size for this algorithm.");
}
}
}