Replace "Dbg.Assert" with 'if () throw' in CSVCommands.cs.. (#6910)

Add more useful messages.
This commit is contained in:
Sergey Vasin 2018-06-05 20:12:57 +03:00 committed by Ilya
parent f54085f8a3
commit 3b1a4a4722
2 changed files with 67 additions and 11 deletions

View file

@ -329,7 +329,10 @@ namespace Microsoft.PowerShell.Commands
private void CreateFileStream() private void CreateFileStream()
{ {
Dbg.Assert(_path != null, "FileName is mandatory parameter"); if (_path == null)
{
throw new InvalidOperationException(CsvCommandStrings.FileNameIsAMandatoryParameter);
}
string resolvedFilePath = PathUtils.ResolveFilePath(this.Path, this, _isLiteralPath); string resolvedFilePath = PathUtils.ResolveFilePath(this.Path, this, _isLiteralPath);
@ -416,8 +419,15 @@ namespace Microsoft.PowerShell.Commands
private void ReconcilePreexistingPropertyNames() private void ReconcilePreexistingPropertyNames()
{ {
Dbg.Assert(_isActuallyAppending, "This method should only get called when appending"); if (!_isActuallyAppending)
Dbg.Assert(_preexistingPropertyNames != null, "This method should only get called when we have successfully read preexisting property names"); {
throw new InvalidOperationException(CsvCommandStrings.ReconcilePreexistingPropertyNamesMethodShouldOnlyGetCalledWhenAppending);
}
if (_preexistingPropertyNames == null)
{
throw new InvalidOperationException(CsvCommandStrings.ReconcilePreexistingPropertyNamesMethodShouldOnlyGetCalledWhenPreexistingPropertyNamesHaveBeenReadSuccessfully);
}
HashSet<string> appendedPropertyNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase); HashSet<string> appendedPropertyNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (string appendedPropertyName in _propertyNames) foreach (string appendedPropertyName in _propertyNames)
@ -896,7 +906,10 @@ namespace Microsoft.PowerShell.Commands
IList<string> IList<string>
BuildPropertyNames(PSObject source, IList<string> propertyNames) BuildPropertyNames(PSObject source, IList<string> propertyNames)
{ {
Dbg.Assert(propertyNames == null, "This method should be called only once per cmdlet instance"); if (propertyNames != null)
{
throw new InvalidOperationException(CsvCommandStrings.BuildPropertyNamesMethodShouldBeCalledOnlyOncePerCmdletInstance);
}
// serialize only Extended and Adapted properties.. // serialize only Extended and Adapted properties..
PSMemberInfoCollection<PSPropertyInfo> srcPropertiesToSearch = PSMemberInfoCollection<PSPropertyInfo> srcPropertiesToSearch =
@ -919,7 +932,10 @@ namespace Microsoft.PowerShell.Commands
string string
ConvertPropertyNamesCSV(IList<string> propertyNames) ConvertPropertyNamesCSV(IList<string> propertyNames)
{ {
Dbg.Assert(propertyNames != null, "BuildPropertyNames should be called before this method"); if (propertyNames == null)
{
throw new ArgumentNullException("propertyNames");
}
StringBuilder dest = new StringBuilder(); StringBuilder dest = new StringBuilder();
bool first = true; bool first = true;
@ -949,7 +965,10 @@ namespace Microsoft.PowerShell.Commands
string string
ConvertPSObjectToCSV(PSObject mshObject, IList<string> propertyNames) ConvertPSObjectToCSV(PSObject mshObject, IList<string> propertyNames)
{ {
Dbg.Assert(propertyNames != null, "PropertyName collection can be empty here, but it should not be null"); if (propertyNames == null)
{
throw new ArgumentNullException("propertyNames");
}
StringBuilder dest = new StringBuilder(); StringBuilder dest = new StringBuilder();
bool first = true; bool first = true;
@ -985,7 +1004,11 @@ namespace Microsoft.PowerShell.Commands
string string
GetToStringValueForProperty(PSPropertyInfo property) GetToStringValueForProperty(PSPropertyInfo property)
{ {
Dbg.Assert(property != null, "Caller should validate the parameter"); if (property == null)
{
throw new ArgumentNullException("property");
}
string value = null; string value = null;
try try
{ {
@ -1021,7 +1044,11 @@ namespace Microsoft.PowerShell.Commands
} }
else else
{ {
Dbg.Assert(tnh[0] != null, "type hierarchy should not have null values"); if (tnh[0] == null)
{
throw new InvalidOperationException(CsvCommandStrings.TypeHierarchyShouldNotHaveNullValues);
}
string temp = tnh[0]; string temp = tnh[0];
//If type starts with CSV: remove it. This would happen when you export //If type starts with CSV: remove it. This would happen when you export
//an imported object. import-csv adds CSV. prefix to the type. //an imported object. import-csv adds CSV. prefix to the type.
@ -1135,8 +1162,15 @@ namespace Microsoft.PowerShell.Commands
internal ImportCsvHelper(PSCmdlet cmdlet, char delimiter, IList<string> header, string typeName, StreamReader streamReader) internal ImportCsvHelper(PSCmdlet cmdlet, char delimiter, IList<string> header, string typeName, StreamReader streamReader)
{ {
Dbg.Assert(cmdlet != null, "Caller should verify cmdlet != null"); if (cmdlet == null)
Dbg.Assert(streamReader != null, "Caller should verify textReader != null"); {
throw new ArgumentNullException("cmdlet");
}
if (streamReader == null)
{
throw new ArgumentNullException("streamReader");
}
_cmdlet = cmdlet; _cmdlet = cmdlet;
_delimiter = delimiter; _delimiter = delimiter;
@ -1165,7 +1199,11 @@ namespace Microsoft.PowerShell.Commands
char char
ReadChar() ReadChar()
{ {
Dbg.Assert(!EOF, "This should not be called if EOF is reached"); if (EOF)
{
throw new InvalidOperationException(CsvCommandStrings.EOFIsReached);
}
int i = _sr.Read(); int i = _sr.Read();
return (char)i; return (char)i;
} }

View file

@ -136,4 +136,22 @@
<data name="UseDefaultNameForUnspecifiedHeader" xml:space="preserve"> <data name="UseDefaultNameForUnspecifiedHeader" xml:space="preserve">
<value>One or more headers were not specified. Default names starting with "H" have been used in place of any missing headers.</value> <value>One or more headers were not specified. Default names starting with "H" have been used in place of any missing headers.</value>
</data> </data>
<data name="FileNameIsAMandatoryParameter" xml:space="preserve">
<value>FileName is a mandatory parameter.</value>
</data>
<data name="ReconcilePreexistingPropertyNamesMethodShouldOnlyGetCalledWhenAppending" xml:space="preserve">
<value>ReconcilePreexistingPropertyNames method should only get called when appending.</value>
</data>
<data name="ReconcilePreexistingPropertyNamesMethodShouldOnlyGetCalledWhenPreexistingPropertyNamesHaveBeenReadSuccessfully" xml:space="preserve">
<value>ReconcilePreexistingPropertyNames method should only get called when preexisting property names have been read successfully.</value>
</data>
<data name="BuildPropertyNamesMethodShouldBeCalledOnlyOncePerCmdletInstance" xml:space="preserve">
<value>BuildPropertyNames method should be called only once per cmdlet instance.</value>
</data>
<data name="TypeHierarchyShouldNotHaveNullValues" xml:space="preserve">
<value>Type hierarchy should not have null values.</value>
</data>
<data name="EOFIsReached" xml:space="preserve">
<value>EOF is reached.</value>
</data>
</root> </root>