Skip to content

Commit

Permalink
Fixed error messages issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshClose committed Sep 24, 2012
1 parent b0f925a commit f1dc941
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
36 changes: 36 additions & 0 deletions src/CsvHelper.Tests/CsvReaderErrorMessageTests.cs
Expand Up @@ -160,6 +160,30 @@ public void SecondColumnEmptyErrorTest()
}
}

[Fact]
public void Test()
{
using( var stream = new MemoryStream() )
using( var writer = new StreamWriter( stream ) )
using( var reader = new StreamReader( stream ) )
using( var csvReader = new CsvReader( reader ) )
{
writer.WriteLine("1,9/24/2012");
writer.Flush();
stream.Position = 0;

try
{
csvReader.Configuration.HasHeaderRecord = false;
var records = csvReader.GetRecords<Test3>().ToList();
}
catch( CsvReaderException ex )
{
// Should throw this exception.
}
}
}

private class Test1
{
[CsvField( Index = 0 )]
Expand All @@ -175,5 +199,17 @@ private class Test2

public int IntColumn { get; set; }
}

private class Test3
{
[CsvField(Index = 0)]
public int Id { get; set; }

[CsvField(Index = 1)]
public DateTime CreationDate { get; set; }

[CsvField(Index = 2)]
public string Description { get; set; }
}
}
}
21 changes: 12 additions & 9 deletions src/CsvHelper/CsvReader.cs
Expand Up @@ -532,7 +532,7 @@ record = GetReadRecordFunc<T>()( this );
}
catch( Exception ex )
{
throw new CsvReaderException( GetExceptionMessage( typeof( T ), ex ) );
throw new CsvReaderException( GetExceptionMessage( typeof( T ), ex ), ex );
}
return record;
}
Expand All @@ -559,7 +559,7 @@ record = GetReadRecordFunc( type )( this );
}
catch( Exception ex )
{
throw new CsvReaderException( GetExceptionMessage( type, ex ) );
throw new CsvReaderException( GetExceptionMessage( type, ex ), ex );
}
return record;
}
Expand Down Expand Up @@ -591,7 +591,7 @@ record = GetReadRecordFunc<T>()( this );
}
catch( Exception ex )
{
throw new CsvReaderException( GetExceptionMessage( typeof( T ), ex ) );
throw new CsvReaderException( GetExceptionMessage( typeof( T ), ex ), ex );
}
yield return record;
}
Expand Down Expand Up @@ -624,7 +624,7 @@ record = GetReadRecordFunc( type )( this );
}
catch( Exception ex )
{
throw new CsvReaderException( GetExceptionMessage( type, ex ) );
throw new CsvReaderException( GetExceptionMessage( type, ex ), ex );
}
yield return record;
}
Expand Down Expand Up @@ -881,7 +881,7 @@ protected virtual void ParseNamedIndexes()
/// <param name="type">The type of record.</param>
/// <param name="ex">The original exception.</param>
/// <returns>The new exception message.</returns>
protected virtual string GetExceptionMessage( Type type, Exception ex )
protected virtual string GetExceptionMessage(Type type, Exception ex)
{
var error = new StringBuilder();
error.AppendFormat( "An error occurred trying to read a record of type '{0}'.", type.FullName ).AppendLine();
Expand All @@ -890,15 +890,18 @@ protected virtual string GetExceptionMessage( Type type, Exception ex )
if( configuration.HasHeaderRecord )
{
var name = ( from pair in namedIndexes
from index in pair.Value
where index == currentIndex
select pair.Key ).SingleOrDefault();
from index in pair.Value
where index == currentIndex
select pair.Key ).SingleOrDefault();
if( name != null )
{
error.AppendFormat( "Field Name: '{0}'", name ).AppendLine();
}
}
error.AppendFormat( "Field Value: '{0}'", currentRecord[currentIndex] ).AppendLine();
if( currentIndex < currentRecord.Length )
{
error.AppendFormat( "Field Value: '{0}'", currentRecord[currentIndex] ).AppendLine();
}
error.AppendLine();
error.AppendLine( ex.ToString() );

Expand Down

0 comments on commit f1dc941

Please sign in to comment.