Errors
class Errors
Create exceptions for common error cases.
using System;
using System.Globalization;
using System.IO;
using System.Runtime.CompilerServices;
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)
{
DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(25, 1);
defaultInterpolatedStringHandler.AppendLiteral("Invalid resource type: '");
defaultInterpolatedStringHandler.AppendFormatted(s);
defaultInterpolatedStringHandler.AppendLiteral("'");
return new ArgumentException(defaultInterpolatedStringHandler.ToStringAndClear());
}
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)
{
DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(19, 1);
defaultInterpolatedStringHandler.AppendLiteral("Invalid service: '");
defaultInterpolatedStringHandler.AppendFormatted(s);
defaultInterpolatedStringHandler.AppendLiteral("'");
return new ArgumentException(defaultInterpolatedStringHandler.ToStringAndClear());
}
public static ArgumentException InsufficientStorageTransferOptions(long streamLength, long statedMaxBlockSize, long necessaryMinBlockSize)
{
DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(147, 3);
defaultInterpolatedStringHandler.AppendLiteral("Cannot upload ");
defaultInterpolatedStringHandler.AppendFormatted(streamLength);
defaultInterpolatedStringHandler.AppendLiteral(" bytes with a maximum transfer size of ");
defaultInterpolatedStringHandler.AppendFormatted(statedMaxBlockSize);
defaultInterpolatedStringHandler.AppendLiteral(" bytes per block. Please increase the StorageTransferOptions.MaximumTransferSize to at least ");
defaultInterpolatedStringHandler.AppendFormatted(necessaryMinBlockSize);
defaultInterpolatedStringHandler.AppendLiteral(".");
return new ArgumentException(defaultInterpolatedStringHandler.ToStringAndClear());
}
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, Base64FormattingOptions.None) + " Right: " + Convert.ToBase64String(right, Base64FormattingOptions.None));
}
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) {
DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(71, 6);
defaultInterpolatedStringHandler.AppendFormatted(streamName);
defaultInterpolatedStringHandler.AppendLiteral(".");
defaultInterpolatedStringHandler.AppendFormatted("Position");
defaultInterpolatedStringHandler.AppendLiteral(" must be less than ");
defaultInterpolatedStringHandler.AppendFormatted(streamName);
defaultInterpolatedStringHandler.AppendLiteral(".");
defaultInterpolatedStringHandler.AppendFormatted("Length");
defaultInterpolatedStringHandler.AppendLiteral(". Please set ");
defaultInterpolatedStringHandler.AppendFormatted(streamName);
defaultInterpolatedStringHandler.AppendLiteral(".");
defaultInterpolatedStringHandler.AppendFormatted("Position");
defaultInterpolatedStringHandler.AppendLiteral(" to the start of the data to upload.");
throw new ArgumentException(defaultInterpolatedStringHandler.ToStringAndClear());
}
}
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.");
}
}
}