Skip to content
This repository has been archived by the owner on Nov 20, 2023. It is now read-only.

Commit

Permalink
remove 'tye.yaml' verbiage for YAML errors (#1033) (#1034)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
brettveenstra and davidfowl authored Dec 12, 2022
1 parent 3f3442d commit 68beabe
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/Microsoft.Tye.Core/CoreStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,13 @@
<value>Services must have unique names.</value>
</data>
<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 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 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 name="PathNotFound" xml:space="preserve">
<value>Path "{path}" was not found.</value>
Expand Down
8 changes: 7 additions & 1 deletion src/Microsoft.Tye.Core/Serialization/TyeYamlException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System;
using System.IO;
using YamlDotNet.Core;

namespace Tye.Serialization
Expand All @@ -20,7 +21,12 @@ public TyeYamlException(Mark start, string message)
}

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)
{
}

Expand Down
16 changes: 13 additions & 3 deletions src/Microsoft.Tye.Core/Serialization/YamlParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,20 @@ public ConfigApplication ParseConfigApplication()
}
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();

// TODO assuming first document.
var document = _yamlStream.Documents[0];
var node = document.RootNode;
ThrowIfNotYamlMapping(node);
ThrowIfNotYamlMapping(node, _fileInfo);

app.Source = _fileInfo!;

Expand Down Expand Up @@ -132,10 +137,15 @@ public static void ThrowIfNotYamlSequence(string key, YamlNode node)
}
}

public static void ThrowIfNotYamlMapping(YamlNode node)
public static void ThrowIfNotYamlMapping(YamlNode node, FileInfo? fileInfo = null)
{
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,
CoreStrings.FormatUnexpectedType(YamlNodeType.Mapping.ToString(), node.NodeType.ToString()));
}
Expand Down
22 changes: 21 additions & 1 deletion test/UnitTests/TyeDeserializationValidationTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Tye;
using System;
using System.IO;
using Tye;
using Tye.Serialization;
using Xunit;

Expand Down Expand Up @@ -274,6 +276,24 @@ public void LivenessProbeSuccessThresholdMustBeOne()
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]
public void DockerFileWithArgs()
{
Expand Down

0 comments on commit 68beabe

Please sign in to comment.