Skip to content

Commit 9872574

Browse files
committed
Write exception message correctly in case of CommandParseException. Fixes #114
1 parent 54d145b commit 9872574

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

CommandLineParser.Tests/Exceptions/ExceptionsTest.cs

+43
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using MatthiWare.CommandLine.Abstractions.Command;
22
using MatthiWare.CommandLine.Core.Attributes;
33
using MatthiWare.CommandLine.Core.Exceptions;
4+
using Moq;
5+
using System;
6+
using System.Collections.Generic;
47
using System.Linq;
58
using System.Threading.Tasks;
69
using Xunit;
@@ -130,6 +133,46 @@ public async Task CommandParseExceptionTestAsync()
130133
Assert.Same(parser.Commands.First(), result.Errors.Cast<CommandParseException>().First().Command);
131134
}
132135

136+
[Fact]
137+
public void CommandParseException_Should_Contain_Correct_Message_Single()
138+
{
139+
var cmdMock = new Mock<ICommandLineCommand>();
140+
cmdMock.SetupGet(_ => _.Name).Returns("test");
141+
142+
var exceptionList = new List<Exception>
143+
{
144+
new Exception("msg1")
145+
};
146+
147+
var parseException = new CommandParseException(cmdMock.Object, exceptionList.AsReadOnly());
148+
var msg = parseException.Message;
149+
var expected = @"Unable to parse command 'test' because msg1";
150+
151+
Assert.Equal(expected, msg);
152+
}
153+
154+
[Fact]
155+
public void CommandParseException_Should_Contain_Correct_Message_Multiple()
156+
{
157+
var cmdMock = new Mock<ICommandLineCommand>();
158+
cmdMock.SetupGet(_ => _.Name).Returns("test");
159+
160+
var exceptionList = new List<Exception>
161+
{
162+
new Exception("msg1"),
163+
new Exception("msg2")
164+
};
165+
166+
var parseException = new CommandParseException(cmdMock.Object, exceptionList.AsReadOnly());
167+
var msg = parseException.Message;
168+
var expected = @"Unable to parse command 'test' because 2 errors occured
169+
- msg1
170+
- msg2
171+
";
172+
173+
Assert.Equal(expected, msg);
174+
}
175+
133176
[Fact]
134177
public void OptionParseExceptionTest()
135178
{

CommandLineParser/Core/Exceptions/CommandParseException.cs

+32-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
35
using MatthiWare.CommandLine.Abstractions.Command;
46

57
namespace MatthiWare.CommandLine.Core.Exceptions
@@ -20,7 +22,36 @@ public class CommandParseException : BaseParserException
2022
/// <param name="command">the failed command</param>
2123
/// <param name="innerExceptions">collection of inner exception</param>
2224
public CommandParseException(ICommandLineCommand command, IReadOnlyCollection<Exception> innerExceptions)
23-
: base(command, "", new AggregateException(innerExceptions))
25+
: base(command, CreateMessage(command, innerExceptions), new AggregateException(innerExceptions))
2426
{ }
27+
28+
private static string CreateMessage(ICommandLineCommand command, IReadOnlyCollection<Exception> exceptions)
29+
{
30+
if (exceptions.Count > 1)
31+
{
32+
return CreateMultipleExceptionsMessage(command, exceptions);
33+
}
34+
else
35+
{
36+
return CreateSingleExceptionMessage(command, exceptions.First());
37+
}
38+
}
39+
40+
private static string CreateSingleExceptionMessage(ICommandLineCommand command, Exception exception)
41+
=> $"Unable to parse command '{command.Name}' because {exception.Message}";
42+
43+
44+
private static string CreateMultipleExceptionsMessage(ICommandLineCommand command, IReadOnlyCollection<Exception> exceptions)
45+
{
46+
var message = new StringBuilder();
47+
message.AppendLine($"Unable to parse command '{command.Name}' because {exceptions.Count} errors occured");
48+
49+
foreach (var exception in exceptions)
50+
{
51+
message.AppendLine($" - {exception.Message}");
52+
}
53+
54+
return message.ToString();
55+
}
2556
}
2657
}

0 commit comments

Comments
 (0)