dotnet / dotnet/command-line-api
Gracefully handle exceptions in command handlers
- Vorherrschende Sprache
- C#
- Sterne
- 3.7k
- Forks
- 428
- PR-Merge-Kennzahlen
- Keine gemergten PRs in 30 T.
Beschreibung
When an exception occurs in a command handler the entire stack trace is barfed out into stderr and I'd like to not have that behavior for explicitly handled exceptions. Consider a handler that tries to read a file but the filename is not found. The standard libraries throw a FileNotFoundException which I'd like to catch and display just the message and not the entire stack trace. For unhandled exceptions I think the current behavior of the stack trace is fine since by definition it is unhandled.
I looks like I can get the invocation context and set the invocation result to a custom IInvocationResult. The problem is there doesn't appear to be (or I missed it 😅) an ErrorResult for setting a user-defined error that the handler failed. I made my own that is essentially a copy of `ParseErrorResult` but you can specify the message in the ctor.
```c#
public class ErrorResult : IInvocationResult
{
private readonly string _errorMessage;
private readonly int _errorExitCode;
public ErrorResult(string errorMessage, int errorExitCode = 1)
{
_errorMessage = errorMessage;
_errorExitCode = errorExitCode;
}
public void Apply(InvocationContext context)
{
context.Console.ResetTerminalForegroundColor();
context.Console.SetTerminalForegroundRed();
context.Console.Error.WriteLine(_errorMessage);
context.Console.Error.WriteLine();
context.ExitCode = _errorExitCode;
context.Console.ResetTerminalForegroundColor();
}
}
```
The main issue I ran into was that `ResetTerminalForegroundColor` and `ResetTerminalForegroundColor` are internal extension methods and those also rely on the internal `Platform` static class. I copied them into my code base for now.
Assuming those APIs were public, ErrorResult now works and you can use it like in the following trivial example:
```c#
void Handler(InvocationContext ctx, string filename)
{
try
{
Console.WriteLine(File.ReadAllText(filename));
}
catch (FileNotFoundException e)
{
ctx.InvocationResult = new ErrorResult(e.Message, 42);
}
}
```
Now when the handled exception occurs, it doesnt bubble up and kill the application but instead just prints (in red) to stderr.
I'm not familiar with the details of how System.CommandLine works. Do you think this is a valid approach for handling exceptions in a handler?
Beitragsleitfaden
Rechercherichtung
Beginne damit, den Invocation-Result-Pfad rund um InvocationContext, IInvocationResult und ParseErrorResult zu lesen, und untersuche anschließend die im Issue erwähnten internen Methoden für Terminalfarben und die Platform-Abhängigkeit. Die Arbeit ist abgeschlossen, wenn der vom Projekt unterstützte Umgang mit behandelten und unbehandelten Ausnahmen, stderr-Ausgabe und Exit-Codes klar definiert und validiert ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- csharp
- Bereich
- cli
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Muss geklärt werden
- Anfängerfreundlichkeit
- 30/100