ThrowHelper
using System;
using System.Diagnostics.CodeAnalysis;
namespace Microsoft.Extensions.Primitives
{
internal static class ThrowHelper
{
[System.Diagnostics.CodeAnalysis.DoesNotReturn]
internal static void ThrowArgumentNullException(ExceptionArgument argument)
{
throw new ArgumentNullException(GetArgumentName(argument));
}
[System.Diagnostics.CodeAnalysis.DoesNotReturn]
internal static void ThrowArgumentOutOfRangeException(ExceptionArgument argument)
{
throw new ArgumentOutOfRangeException(GetArgumentName(argument));
}
[System.Diagnostics.CodeAnalysis.DoesNotReturn]
internal static void ThrowArgumentException(ExceptionResource resource)
{
throw new ArgumentException(GetResourceText(resource));
}
[System.Diagnostics.CodeAnalysis.DoesNotReturn]
internal static void ThrowInvalidOperationException(ExceptionResource resource)
{
throw new InvalidOperationException(GetResourceText(resource));
}
[System.Diagnostics.CodeAnalysis.DoesNotReturn]
internal static void ThrowInvalidOperationException(ExceptionResource resource, params object[] args)
{
throw new InvalidOperationException(string.Format(GetResourceText(resource), args));
}
internal static ArgumentNullException GetArgumentNullException(ExceptionArgument argument)
{
return new ArgumentNullException(GetArgumentName(argument));
}
internal static ArgumentOutOfRangeException GetArgumentOutOfRangeException(ExceptionArgument argument)
{
return new ArgumentOutOfRangeException(GetArgumentName(argument));
}
internal static ArgumentException GetArgumentException(ExceptionResource resource)
{
return new ArgumentException(GetResourceText(resource));
}
private static string GetResourceText(ExceptionResource resource)
{
switch (resource) {
case ExceptionResource.Argument_InvalidOffsetLength:
return System.SR.Argument_InvalidOffsetLength;
case ExceptionResource.Argument_InvalidOffsetLengthStringSegment:
return System.SR.Argument_InvalidOffsetLengthStringSegment;
case ExceptionResource.Capacity_CannotChangeAfterWriteStarted:
return System.SR.Capacity_CannotChangeAfterWriteStarted;
case ExceptionResource.Capacity_NotEnough:
return System.SR.Capacity_NotEnough;
case ExceptionResource.Capacity_NotUsedEntirely:
return System.SR.Capacity_NotUsedEntirely;
default:
return "";
}
}
private static string GetArgumentName(ExceptionArgument argument)
{
return argument.ToString();
}
}
}