|
6 | 6 | using Microsoft.PowerShell.EditorServices.Utility; |
7 | 7 | using Nito.AsyncEx; |
8 | 8 | using System; |
| 9 | +using System.Collections; |
| 10 | +using System.Globalization; |
9 | 11 | using System.Collections.Generic; |
10 | 12 | using System.Collections.ObjectModel; |
11 | 13 | using System.Linq; |
|
15 | 17 |
|
16 | 18 | namespace Microsoft.PowerShell.EditorServices |
17 | 19 | { |
18 | | - using System.Collections; |
19 | 20 | using System.Management.Automation; |
20 | 21 | using System.Management.Automation.Host; |
21 | 22 | using System.Management.Automation.Runspaces; |
@@ -322,10 +323,12 @@ await Task.Factory.StartNew<IEnumerable<TResult>>( |
322 | 323 | } |
323 | 324 | catch (RuntimeException e) |
324 | 325 | { |
325 | | - // TODO: Return an error |
326 | 326 | Logger.Write( |
327 | 327 | LogLevel.Error, |
328 | | - "Exception occurred while attempting to execute command:\r\n\r\n" + e.ToString()); |
| 328 | + "Runtime exception occurred while executing command:\r\n\r\n" + e.ToString()); |
| 329 | + |
| 330 | + // Write the error to the host |
| 331 | + this.WriteExceptionToHost(e); |
329 | 332 | } |
330 | 333 | } |
331 | 334 |
|
@@ -522,6 +525,75 @@ private void OnSessionStateChanged(object sender, SessionStateChangedEventArgs e |
522 | 525 |
|
523 | 526 | #region Private Methods |
524 | 527 |
|
| 528 | + private void WriteExceptionToHost(Exception e) |
| 529 | + { |
| 530 | + const string ExceptionFormat = |
| 531 | + "{0}\r\n{1}\r\n + CategoryInfo : {2}\r\n + FullyQualifiedErrorId : {3}"; |
| 532 | + |
| 533 | + IContainsErrorRecord containsErrorRecord = e as IContainsErrorRecord; |
| 534 | + |
| 535 | + if (containsErrorRecord == null || |
| 536 | + containsErrorRecord.ErrorRecord == null) |
| 537 | + { |
| 538 | + this.WriteError(e.Message, null, 0, 0); |
| 539 | + return; |
| 540 | + } |
| 541 | + |
| 542 | + ErrorRecord errorRecord = containsErrorRecord.ErrorRecord; |
| 543 | + if (errorRecord.InvocationInfo == null) |
| 544 | + { |
| 545 | + this.WriteError(errorRecord.ToString(), String.Empty, 0, 0); |
| 546 | + return; |
| 547 | + } |
| 548 | + |
| 549 | + string errorRecordString = errorRecord.ToString(); |
| 550 | + if ((errorRecord.InvocationInfo.PositionMessage != null) && |
| 551 | + errorRecordString.IndexOf(errorRecord.InvocationInfo.PositionMessage, StringComparison.Ordinal) != -1) |
| 552 | + { |
| 553 | + this.WriteError(errorRecordString); |
| 554 | + return; |
| 555 | + } |
| 556 | + |
| 557 | + string message = |
| 558 | + string.Format( |
| 559 | + CultureInfo.InvariantCulture, |
| 560 | + ExceptionFormat, |
| 561 | + errorRecord.ToString(), |
| 562 | + errorRecord.InvocationInfo.PositionMessage, |
| 563 | + errorRecord.CategoryInfo, |
| 564 | + errorRecord.FullyQualifiedErrorId); |
| 565 | + |
| 566 | + this.WriteError(message); |
| 567 | + } |
| 568 | + |
| 569 | + private void WriteError( |
| 570 | + string errorMessage, |
| 571 | + string filePath, |
| 572 | + int lineNumber, |
| 573 | + int columnNumber) |
| 574 | + { |
| 575 | + const string ErrorLocationFormat = "At {0}:{1} char:{2}"; |
| 576 | + |
| 577 | + this.WriteError( |
| 578 | + errorMessage + |
| 579 | + Environment.NewLine + |
| 580 | + string.Format( |
| 581 | + ErrorLocationFormat, |
| 582 | + String.IsNullOrEmpty(filePath) ? "line" : filePath, |
| 583 | + lineNumber, |
| 584 | + columnNumber)); |
| 585 | + } |
| 586 | + |
| 587 | + private void WriteError(string errorMessage) |
| 588 | + { |
| 589 | + ((IConsoleHost)this).WriteOutput( |
| 590 | + errorMessage, |
| 591 | + true, |
| 592 | + OutputType.Error, |
| 593 | + ConsoleColor.Red, |
| 594 | + ConsoleColor.Black); |
| 595 | + } |
| 596 | + |
525 | 597 | void powerShell_InvocationStateChanged(object sender, PSInvocationStateChangedEventArgs e) |
526 | 598 | { |
527 | 599 | SessionStateChangedEventArgs eventArgs = TranslateInvocationStateInfo(e.InvocationStateInfo); |
|
0 commit comments