Browse Source

remove 'tye.yaml' verbiage for YAML errors (#1033) (#1034)

* remove 'tye.yaml' verbiage for YAML errors - leverage file info when available, otherwise use more generic 'YAML file'  (#1033)

* Apply suggestions from code review

* Apply suggestions from code review

Co-authored-by: David Fowler <davidfowl@gmail.com>
pull/1494/head
Brett Veenstra 4 years ago
committed by GitHub
parent
commit
68beabecfd
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      src/Microsoft.Tye.Core/CoreStrings.resx
  2. 8
      src/Microsoft.Tye.Core/Serialization/TyeYamlException.cs
  3. 16
      src/Microsoft.Tye.Core/Serialization/YamlParser.cs
  4. 22
      test/UnitTests/TyeDeserializationValidationTests.cs

6
src/Microsoft.Tye.Core/CoreStrings.resx

@ -169,13 +169,13 @@
<value>Services must have unique names.</value> <value>Services must have unique names.</value>
</data> </data>
<data name="UnexpectedType" xml:space="preserve"> <data name="UnexpectedType" xml:space="preserve">
<value>Unexpected node type in tye.yaml. Expected "{expected}" but got "{actual}".</value> <value>Unexpected node type in the tye configuration file. Expected "{expected}" but got "{actual}".</value>
</data> </data>
<data name="UnrecognizedKey" xml:space="preserve"> <data name="UnrecognizedKey" xml:space="preserve">
<value>Unexpected key "{key}" in tye.yaml.</value> <value>Unexpected key "{key}" in the tye configuration file.</value>
</data> </data>
<data name="UnexpectedTypes" xml:space="preserve"> <data name="UnexpectedTypes" xml:space="preserve">
<value>Unexpected node type in tye.yaml. Expected one of ({expected}) but got "{actual}".</value> <value>Unexpected node type in the tye configuration file. Expected one of ({expected}) but got "{actual}".</value>
</data> </data>
<data name="PathNotFound" xml:space="preserve"> <data name="PathNotFound" xml:space="preserve">
<value>Path "{path}" was not found.</value> <value>Path "{path}" was not found.</value>

8
src/Microsoft.Tye.Core/Serialization/TyeYamlException.cs

@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information. // See the LICENSE file in the project root for more information.
using System; using System;
using System.IO;
using YamlDotNet.Core; using YamlDotNet.Core;
namespace Tye.Serialization namespace Tye.Serialization
@ -20,7 +21,12 @@ namespace Tye.Serialization
} }
public TyeYamlException(Mark start, string message, Exception? innerException) public TyeYamlException(Mark start, string message, Exception? innerException)
: base($"Error parsing tye.yaml: ({start.Line}, {start.Column}): {message}", innerException) : base($"Error parsing YAML: ({start.Line}, {start.Column}): {message}", innerException)
{
}
public TyeYamlException(Mark start, string message, Exception? innerException, FileInfo fileInfo)
: base($"Error parsing '{fileInfo.Name}': ({start.Line}, {start.Column}): {message}", innerException)
{ {
} }

16
src/Microsoft.Tye.Core/Serialization/YamlParser.cs

@ -42,7 +42,12 @@ namespace Tye.Serialization
} }
catch (YamlException ex) catch (YamlException ex)
{ {
throw new TyeYamlException(ex.Start, "Unable to parse tye.yaml. See inner exception.", ex); if (_fileInfo != null)
{
throw new TyeYamlException(ex.Start, $"Unable to parse '{_fileInfo.Name}'. See inner exception.", ex, _fileInfo);
}
throw new TyeYamlException(ex.Start, $"Unable to parse YAML. See inner exception.", ex);
} }
var app = new ConfigApplication(); var app = new ConfigApplication();
@ -50,7 +55,7 @@ namespace Tye.Serialization
// TODO assuming first document. // TODO assuming first document.
var document = _yamlStream.Documents[0]; var document = _yamlStream.Documents[0];
var node = document.RootNode; var node = document.RootNode;
ThrowIfNotYamlMapping(node); ThrowIfNotYamlMapping(node, _fileInfo);
app.Source = _fileInfo!; app.Source = _fileInfo!;
@ -132,10 +137,15 @@ namespace Tye.Serialization
} }
} }
public static void ThrowIfNotYamlMapping(YamlNode node) public static void ThrowIfNotYamlMapping(YamlNode node, FileInfo? fileInfo = null)
{ {
if (node.NodeType != YamlNodeType.Mapping) if (node.NodeType != YamlNodeType.Mapping)
{ {
if (fileInfo != null)
{
throw new TyeYamlException(node.Start,
CoreStrings.FormatUnexpectedType(YamlNodeType.Mapping.ToString(), node.NodeType.ToString()), null, fileInfo);
}
throw new TyeYamlException(node.Start, throw new TyeYamlException(node.Start,
CoreStrings.FormatUnexpectedType(YamlNodeType.Mapping.ToString(), node.NodeType.ToString())); CoreStrings.FormatUnexpectedType(YamlNodeType.Mapping.ToString(), node.NodeType.ToString()));
} }

22
test/UnitTests/TyeDeserializationValidationTests.cs

@ -1,4 +1,6 @@
using Tye; using System;
using System.IO;
using Tye;
using Tye.Serialization; using Tye.Serialization;
using Xunit; using Xunit;
@ -274,6 +276,24 @@ services:
Assert.Contains(errorMessage, exception.Message); Assert.Contains(errorMessage, exception.Message);
} }
[Fact]
public void BadYmlFileWithArgs_ThrowsExceptionWithUsefulFilePath()
{
var input = @"
flimflam";
using var parser = new YamlParser(input, new FileInfo("foobar.yml"));
try
{
parser.ParseConfigApplication();
Assert.False(true, "YML parsing exception expected with supplied input");
}
catch (TyeYamlException e)
{
Assert.StartsWith("Error parsing 'foobar.yml': (2, 1): Unexpected node type in the tye configuration file.", e.Message);
}
}
[Fact] [Fact]
public void DockerFileWithArgs() public void DockerFileWithArgs()
{ {

Loading…
Cancel
Save