| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | // Copied from https://github.com/aspnet/Extensions/tree/master/src/Primitives/src |
| | | 5 | | |
| | | 6 | | using System; |
| | | 7 | | using System.Diagnostics; |
| | | 8 | | |
| | | 9 | | #pragma warning disable CA1305 // ToString Locale |
| | | 10 | | #pragma warning disable IDE0051 // Private member not used |
| | | 11 | | |
| | | 12 | | namespace Azure.Core.Http.Multipart |
| | | 13 | | { |
| | | 14 | | internal static class ThrowHelper |
| | | 15 | | { |
| | | 16 | | internal static void ThrowArgumentNullException(ExceptionArgument argument) |
| | | 17 | | { |
| | 0 | 18 | | throw new ArgumentNullException(GetArgumentName(argument)); |
| | | 19 | | } |
| | | 20 | | |
| | | 21 | | internal static void ThrowArgumentOutOfRangeException(ExceptionArgument argument) |
| | | 22 | | { |
| | 0 | 23 | | throw new ArgumentOutOfRangeException(GetArgumentName(argument)); |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | internal static void ThrowArgumentException(ExceptionResource resource) |
| | | 27 | | { |
| | 0 | 28 | | throw new ArgumentException(GetResourceText(resource)); |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | internal static void ThrowInvalidOperationException(ExceptionResource resource) |
| | | 32 | | { |
| | 0 | 33 | | throw new InvalidOperationException(GetResourceText(resource)); |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | internal static void ThrowInvalidOperationException(ExceptionResource resource, params object[] args) |
| | | 37 | | { |
| | 0 | 38 | | var message = string.Format(GetResourceText(resource), args); |
| | | 39 | | |
| | 0 | 40 | | throw new InvalidOperationException(message); |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | internal static ArgumentNullException GetArgumentNullException(ExceptionArgument argument) |
| | | 44 | | { |
| | 0 | 45 | | return new ArgumentNullException(GetArgumentName(argument)); |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | internal static ArgumentOutOfRangeException GetArgumentOutOfRangeException(ExceptionArgument argument) |
| | | 49 | | { |
| | 0 | 50 | | return new ArgumentOutOfRangeException(GetArgumentName(argument)); |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | internal static ArgumentException GetArgumentException(ExceptionResource resource) |
| | | 54 | | { |
| | 0 | 55 | | return new ArgumentException(GetResourceText(resource)); |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | private static string GetResourceText(ExceptionResource resource) |
| | | 59 | | { |
| | | 60 | | // return Resources.ResourceManager.GetString(GetResourceName(resource), Resources.Culture); |
| | | 61 | | // Hack to avoid including the resx: |
| | 0 | 62 | | return resource switch |
| | 0 | 63 | | { |
| | 0 | 64 | | ExceptionResource.Argument_InvalidOffsetLength => "Offset and length are out of bounds for the string or |
| | 0 | 65 | | ExceptionResource.Argument_InvalidOffsetLengthStringSegment => "Offset and length are out of bounds for |
| | 0 | 66 | | ExceptionResource.Capacity_CannotChangeAfterWriteStarted => "Cannot change capacity after write started. |
| | 0 | 67 | | ExceptionResource.Capacity_NotEnough => "Not enough capacity to write '{0}' characters, only '{1}' left. |
| | 0 | 68 | | ExceptionResource.Capacity_NotUsedEntirely => "Entire reserved capacity was not used. Capacity: '{0}', w |
| | 0 | 69 | | _ => throw new ArgumentOutOfRangeException(nameof(resource)) |
| | 0 | 70 | | }; |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | private static string GetArgumentName(ExceptionArgument argument) |
| | | 74 | | { |
| | | 75 | | Debug.Assert(Enum.IsDefined(typeof(ExceptionArgument), argument), |
| | | 76 | | "The enum value is not defined, please check the ExceptionArgument Enum."); |
| | | 77 | | |
| | 0 | 78 | | return argument.ToString(); |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | private static string GetResourceName(ExceptionResource resource) |
| | | 82 | | { |
| | | 83 | | Debug.Assert(Enum.IsDefined(typeof(ExceptionResource), resource), |
| | | 84 | | "The enum value is not defined, please check the ExceptionResource Enum."); |
| | | 85 | | |
| | 0 | 86 | | return resource.ToString(); |
| | | 87 | | } |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | internal enum ExceptionArgument |
| | | 91 | | { |
| | | 92 | | buffer, |
| | | 93 | | offset, |
| | | 94 | | length, |
| | | 95 | | text, |
| | | 96 | | start, |
| | | 97 | | count, |
| | | 98 | | index, |
| | | 99 | | value, |
| | | 100 | | capacity, |
| | | 101 | | separators |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | internal enum ExceptionResource |
| | | 105 | | { |
| | | 106 | | Argument_InvalidOffsetLength, |
| | | 107 | | Argument_InvalidOffsetLengthStringSegment, |
| | | 108 | | Capacity_CannotChangeAfterWriteStarted, |
| | | 109 | | Capacity_NotEnough, |
| | | 110 | | Capacity_NotUsedEntirely |
| | | 111 | | } |
| | | 112 | | } |