Skip to content

Commit 25d5559

Browse files
committed
Handle invalid provider config exception
1 parent 32f01dc commit 25d5559

File tree

5 files changed

+39
-26
lines changed

5 files changed

+39
-26
lines changed

src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,12 @@ internal async Task<int> Collect(CancellationToken ct, CommandLineConfiguration
474474
}
475475
}
476476
}
477+
catch (ArgumentException e)
478+
{
479+
Console.Error.WriteLine($"[ERROR] {e.Message}");
480+
collectionStopped = true;
481+
ret = (int)ReturnCode.ArgumentError;
482+
}
477483
catch (CommandLineErrorException e)
478484
{
479485
Console.Error.WriteLine($"[ERROR] {e.Message}");

src/Tools/dotnet-trace/CommandLine/Commands/CollectLinuxCommand.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ internal int CollectLinux(CollectLinuxArgs args)
7272
stopwatch.Start();
7373
ret = RecordTraceInvoker(command, (UIntPtr)command.Length, OutputHandler);
7474
}
75+
catch (ArgumentException e)
76+
{
77+
Console.Error.WriteLine($"[ERROR] {e.Message}");
78+
ret = (int)ReturnCode.ArgumentError;
79+
}
80+
catch (CommandLineErrorException e)
81+
{
82+
Console.Error.WriteLine($"[ERROR] {e.Message}");
83+
ret = (int)ReturnCode.TracingError;
84+
}
7585
catch (Exception ex)
7686
{
7787
Console.Error.WriteLine($"[ERROR] {ex}");

src/tests/Common/MockConsole.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public void AssertLinesEqual(int startLine, params string[] expectedLines)
158158
}
159159
}
160160

161-
public void AssertSanitizedLinesEqual(Func<string[], string[]> sanitizer, bool ignorePastExpected = false, params string[] expectedLines)
161+
public void AssertSanitizedLinesEqual(Func<string[], string[]> sanitizer, params string[] expectedLines)
162162
{
163163
string[] actualLines = Lines;
164164
if (sanitizer is not null)
@@ -178,12 +178,9 @@ public void AssertSanitizedLinesEqual(Func<string[], string[]> sanitizer, bool i
178178
$"Line {i,2} Actual : {actualLines[i]}");
179179
}
180180
}
181-
if (!ignorePastExpected)
181+
for (int i = expectedLines.Length; i < actualLines.Length; i++)
182182
{
183-
for (int i = expectedLines.Length; i < actualLines.Length; i++)
184-
{
185-
Assert.True(string.IsNullOrEmpty(actualLines[i]), $"Actual line #{i} beyond expected lines is not empty: {actualLines[i]}");
186-
}
183+
Assert.True(string.IsNullOrEmpty(actualLines[i]), $"Actual line #{i} beyond expected lines is not empty: {actualLines[i]}");
187184
}
188185
}
189186
}

src/tests/dotnet-trace/CollectCommandFunctionalTests.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public async Task CollectCommandProviderConfigurationConsolidation(CollectArgs a
5454
MockConsole console = new(200, 30);
5555
int exitCode = await RunAsync(args, console).ConfigureAwait(true);
5656
Assert.Equal((int)ReturnCode.Ok, exitCode);
57-
console.AssertSanitizedLinesEqual(CollectSanitizer, expectedLines: expectedSubset);
57+
console.AssertSanitizedLinesEqual(CollectSanitizer, expectedSubset);
5858

5959
byte[] expected = Encoding.UTF8.GetBytes(ExpectedPayload);
6060
Assert.Equal(expected, args.EventStream.ToArray());
@@ -66,8 +66,8 @@ public async Task CollectCommandInvalidProviderConfiguration_Throws(CollectArgs
6666
{
6767
MockConsole console = new(200, 30);
6868
int exitCode = await RunAsync(args, console).ConfigureAwait(true);
69-
Assert.Equal((int)ReturnCode.TracingError, exitCode);
70-
console.AssertSanitizedLinesEqual(CollectSanitizer, true, expectedException);
69+
Assert.Equal((int)ReturnCode.ArgumentError, exitCode);
70+
console.AssertSanitizedLinesEqual(CollectSanitizer, expectedException);
7171
}
7272

7373
private static async Task<int> RunAsync(CollectArgs config, MockConsole console)
@@ -225,31 +225,31 @@ public static IEnumerable<object[]> InvalidProviders()
225225
yield return new object[]
226226
{
227227
new CollectArgs(profile: new[] { "cpu-sampling" }),
228-
new [] { FormatException("The specified profile 'cpu-sampling' does not apply to `dotnet-trace collect`.", "System.ArgumentException") }
228+
new [] { FormatException("The specified profile 'cpu-sampling' does not apply to `dotnet-trace collect`.") }
229229
};
230230

231231
yield return new object[]
232232
{
233233
new CollectArgs(profile: new[] { "unknown" }),
234-
new [] { FormatException("Invalid profile name: unknown", "System.ArgumentException") }
234+
new [] { FormatException("Invalid profile name: unknown") }
235235
};
236236

237237
yield return new object[]
238238
{
239239
new CollectArgs(providers: new[] { "Foo:::Bar=0", "Foo:::Bar=1" }),
240-
new [] { FormatException($"Provider \"Foo\" is declared multiple times with filter arguments.", "System.ArgumentException") }
240+
new [] { FormatException($"Provider \"Foo\" is declared multiple times with filter arguments.") }
241241
};
242242

243243
yield return new object[]
244244
{
245245
new CollectArgs(clrevents: "unknown"),
246-
new [] { FormatException("unknown is not a valid CLR event keyword", "System.ArgumentException") }
246+
new [] { FormatException("unknown is not a valid CLR event keyword") }
247247
};
248248

249249
yield return new object[]
250250
{
251251
new CollectArgs(clrevents: "gc", clreventlevel: "unknown"),
252-
new [] { FormatException("Unknown EventLevel: unknown", "System.ArgumentException") }
252+
new [] { FormatException("Unknown EventLevel: unknown") }
253253
};
254254
}
255255

@@ -277,6 +277,6 @@ private static string FormatProvider(string name, string keywordsHex, string lev
277277
return string.Format("{0, -80}", display) + enabledBy;
278278
}
279279

280-
private static string FormatException(string message, string exceptionType) => $"[ERROR] {exceptionType}: {message}";
280+
private static string FormatException(string message) => $"[ERROR] {message}";
281281
}
282282
}

src/tests/dotnet-trace/CollectLinuxCommandFunctionalTests.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ public void CollectLinuxCommandProviderConfigurationConsolidation(object testArg
4747
if (OperatingSystem.IsLinux())
4848
{
4949
Assert.Equal((int)ReturnCode.Ok, exitCode);
50-
console.AssertSanitizedLinesEqual(CollectLinuxSanitizer, expectedLines: expectedLines);
50+
console.AssertSanitizedLinesEqual(CollectLinuxSanitizer, expectedLines);
5151
}
5252
else
5353
{
5454
Assert.Equal((int)ReturnCode.PlatformNotSupportedError, exitCode);
55-
console.AssertSanitizedLinesEqual(null, expectedLines: new string[] {
55+
console.AssertSanitizedLinesEqual(null, new string[] {
5656
"The collect-linux command is only supported on Linux.",
5757
});
5858
}
@@ -66,13 +66,13 @@ public void CollectLinuxCommandProviderConfigurationConsolidation_Throws(object
6666
int exitCode = Run(testArgs, console);
6767
if (OperatingSystem.IsLinux())
6868
{
69-
Assert.Equal((int)ReturnCode.TracingError, exitCode);
70-
console.AssertSanitizedLinesEqual(null, true, expectedLines: expectedException);
69+
Assert.Equal((int)ReturnCode.ArgumentError, exitCode);
70+
console.AssertSanitizedLinesEqual(null, expectedException);
7171
}
7272
else
7373
{
7474
Assert.Equal((int)ReturnCode.PlatformNotSupportedError, exitCode);
75-
console.AssertSanitizedLinesEqual(null, expectedLines: new string[] {
75+
console.AssertSanitizedLinesEqual(null, new string[] {
7676
"The collect-linux command is only supported on Linux.",
7777
});
7878
}
@@ -197,31 +197,31 @@ public static IEnumerable<object[]> InvalidProviders()
197197
yield return new object[]
198198
{
199199
TestArgs(profile: new[] { "dotnet-sampled-thread-time" }),
200-
new [] { FormatException("The specified profile 'dotnet-sampled-thread-time' does not apply to `dotnet-trace collect-linux`.", "System.ArgumentException") }
200+
new [] { FormatException("The specified profile 'dotnet-sampled-thread-time' does not apply to `dotnet-trace collect-linux`.") }
201201
};
202202

203203
yield return new object[]
204204
{
205205
TestArgs(profile: new[] { "unknown" }),
206-
new [] { FormatException("Invalid profile name: unknown", "System.ArgumentException") }
206+
new [] { FormatException("Invalid profile name: unknown") }
207207
};
208208

209209
yield return new object[]
210210
{
211211
TestArgs(providers: new[] { "Foo:::Bar=0", "Foo:::Bar=1" }),
212-
new [] { FormatException($"Provider \"Foo\" is declared multiple times with filter arguments.", "System.ArgumentException") }
212+
new [] { FormatException($"Provider \"Foo\" is declared multiple times with filter arguments.") }
213213
};
214214

215215
yield return new object[]
216216
{
217217
TestArgs(clrEvents: "unknown"),
218-
new [] { FormatException("unknown is not a valid CLR event keyword", "System.ArgumentException") }
218+
new [] { FormatException("unknown is not a valid CLR event keyword") }
219219
};
220220

221221
yield return new object[]
222222
{
223223
TestArgs(clrEvents: "gc", clrEventLevel: "unknown"),
224-
new [] { FormatException("Unknown EventLevel: unknown", "System.ArgumentException") }
224+
new [] { FormatException("Unknown EventLevel: unknown") }
225225
};
226226
}
227227

@@ -236,7 +236,7 @@ private static string FormatProvider(string name, string keywordsHex, string lev
236236
string.Format("{0, -8}", $"{levelName}({levelValue})");
237237
return string.Format("{0, -80}", display) + enabledBy;
238238
}
239-
private static string FormatException(string message, string exceptionType) => $"[ERROR] {exceptionType}: {message}";
239+
private static string FormatException(string message) => $"[ERROR] {message}";
240240
private static string DefaultOutputFile => $"Output File : {Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar}trace.nettrace";
241241
private static readonly string[] CommonTail = [
242242
DefaultOutputFile,

0 commit comments

Comments
 (0)