From ded585373c0617d68e373412c950a6b9d26bd850 Mon Sep 17 00:00:00 2001 From: Marco Fatica Date: Mon, 7 May 2018 23:16:48 -0400 Subject: [PATCH] Pass correct field name to exception and message --- src/Microsoft.ML/TextLoader.cs | 2 +- test/Microsoft.ML.Tests/TextLoaderTests.cs | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.ML/TextLoader.cs b/src/Microsoft.ML/TextLoader.cs index de592a4f69..f63a14611b 100644 --- a/src/Microsoft.ML/TextLoader.cs +++ b/src/Microsoft.ML/TextLoader.cs @@ -55,7 +55,7 @@ private void SetCustomStringFromType(bool useHeader, string separator, { var mappingAttr = field.GetCustomAttribute(); if(mappingAttr == null) - throw Contracts.ExceptParam(nameof(field.Name), " is missing ColumnAttribute"); + throw Contracts.ExceptParam(field.Name, $"{field.Name} is missing ColumnAttribute"); schemaBuilder.AppendFormat("col={0}:{1}:{2} ", mappingAttr.Name ?? field.Name, diff --git a/test/Microsoft.ML.Tests/TextLoaderTests.cs b/test/Microsoft.ML.Tests/TextLoaderTests.cs index ad85ae47f7..96075b625a 100644 --- a/test/Microsoft.ML.Tests/TextLoaderTests.cs +++ b/test/Microsoft.ML.Tests/TextLoaderTests.cs @@ -7,6 +7,7 @@ using Microsoft.ML.Runtime.Api; using Microsoft.ML.Runtime.Data; using Microsoft.ML.TestFramework; +using System; using Xunit; using Xunit.Abstractions; @@ -219,6 +220,13 @@ public void CanSuccessfullyTrimSpaces() } } + [Fact] + public void ThrowsExceptionWithPropertyName() + { + Exception ex = Assert.Throws( () => new TextLoader("fakefile.txt") ); + Assert.StartsWith("String1 is missing ColumnAttribute", ex.Message); + } + public class QuoteInput { [Column("0")] @@ -254,5 +262,10 @@ public class Input [Column("1")] public float Number1; } + + public class ModelWithoutColumnAttribute + { + public string String1; + } } }