Skip to content

Commit

Permalink
Catch and report errors in LegacyCodeAnalysisVisualStudioSuppressionF…
Browse files Browse the repository at this point in the history
…ixServiceAccessor

These operations are invoked directly by menu commands, so exceptions
thrown by the methods will crash the application. Update each command
handler to catch the exception and report a gold bar plus telemetry
rather than crash.

Fixes #53160
  • Loading branch information
sharwell committed May 5, 2021
1 parent 04e1a12 commit 44be224
Show file tree
Hide file tree
Showing 16 changed files with 141 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

using System;
using System.Composition;
using Microsoft.CodeAnalysis.Extensions;
using Microsoft.CodeAnalysis.ExternalAccess.LegacyCodeAnalysis.Api;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.VisualStudio.LanguageServices;
using Microsoft.VisualStudio.LanguageServices.Implementation.Suppression;
using Microsoft.VisualStudio.Shell.Interop;

Expand All @@ -18,20 +18,77 @@ namespace Microsoft.CodeAnalysis.ExternalAccess.LegacyCodeAnalysis
internal sealed class LegacyCodeAnalysisVisualStudioSuppressionFixServiceAccessor
: ILegacyCodeAnalysisVisualStudioSuppressionFixServiceAccessor
{
private readonly VisualStudioWorkspace _workspace;
private readonly IVisualStudioSuppressionFixService _implementation;

[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public LegacyCodeAnalysisVisualStudioSuppressionFixServiceAccessor(IVisualStudioSuppressionFixService implementation)
=> _implementation = implementation;
public LegacyCodeAnalysisVisualStudioSuppressionFixServiceAccessor(
VisualStudioWorkspace workspace,
IVisualStudioSuppressionFixService implementation)
{
_workspace = workspace;
_implementation = implementation;
}

public bool AddSuppressions(IVsHierarchy? projectHierarchy)
{
var errorReportingService = _workspace.Services.GetRequiredService<IErrorReportingService>();

try
{
return _implementation.AddSuppressions(projectHierarchy);
}
catch (Exception ex)
{
errorReportingService.ShowGlobalErrorInfo(
string.Format(ServicesVSResources.Error_updating_suppressions_0, ex.Message),
new InfoBarUI(
WorkspacesResources.Show_Stack_Trace,
InfoBarUI.UIKind.HyperLink,
() => errorReportingService.ShowDetailedErrorInfo(ex), closeAfterAction: true));
return false;
}
}

public bool AddSuppressions(bool selectedErrorListEntriesOnly, bool suppressInSource, IVsHierarchy? projectHierarchy)
{
var errorReportingService = _workspace.Services.GetRequiredService<IErrorReportingService>();

public bool AddSuppressions(IVsHierarchy projectHierarchyOpt)
=> _implementation.AddSuppressions(projectHierarchyOpt);
try
{
return _implementation.AddSuppressions(selectedErrorListEntriesOnly, suppressInSource, projectHierarchy);
}
catch (Exception ex)
{
errorReportingService.ShowGlobalErrorInfo(
string.Format(ServicesVSResources.Error_updating_suppressions_0, ex.Message),
new InfoBarUI(
WorkspacesResources.Show_Stack_Trace,
InfoBarUI.UIKind.HyperLink,
() => errorReportingService.ShowDetailedErrorInfo(ex), closeAfterAction: true));
return false;
}
}

public bool AddSuppressions(bool selectedErrorListEntriesOnly, bool suppressInSource, IVsHierarchy projectHierarchyOpt)
=> _implementation.AddSuppressions(selectedErrorListEntriesOnly, suppressInSource, projectHierarchyOpt);
public bool RemoveSuppressions(bool selectedErrorListEntriesOnly, IVsHierarchy? projectHierarchy)
{
var errorReportingService = _workspace.Services.GetRequiredService<IErrorReportingService>();

public bool RemoveSuppressions(bool selectedErrorListEntriesOnly, IVsHierarchy projectHierarchyOpt)
=> _implementation.RemoveSuppressions(selectedErrorListEntriesOnly, projectHierarchyOpt);
try
{
return _implementation.RemoveSuppressions(selectedErrorListEntriesOnly, projectHierarchy);
}
catch (Exception ex)
{
errorReportingService.ShowGlobalErrorInfo(
string.Format(ServicesVSResources.Error_updating_suppressions_0, ex.Message),
new InfoBarUI(
WorkspacesResources.Show_Stack_Trace,
InfoBarUI.UIKind.HyperLink,
() => errorReportingService.ShowDetailedErrorInfo(ex), closeAfterAction: true));
return false;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

using Microsoft.VisualStudio.Shell.Interop;

namespace Microsoft.VisualStudio.LanguageServices.Implementation.Suppression
Expand All @@ -17,22 +15,22 @@ internal interface IVisualStudioSuppressionFixService
/// <summary>
/// Adds source suppressions for all the diagnostics in the error list, i.e. baseline all active issues.
/// </summary>
/// <param name="projectHierarchyOpt">An optional project hierarchy object in the solution explorer. If non-null, then only the diagnostics from the project will be suppressed.</param>
bool AddSuppressions(IVsHierarchy projectHierarchyOpt);
/// <param name="projectHierarchy">An optional project hierarchy object in the solution explorer. If non-null, then only the diagnostics from the project will be suppressed.</param>
bool AddSuppressions(IVsHierarchy? projectHierarchy);

/// <summary>
/// Adds source suppressions for diagnostics.
/// </summary>
/// <param name="selectedErrorListEntriesOnly">If true, then only the currently selected entries in the error list will be suppressed. Otherwise, all suppressable entries in the error list will be suppressed.</param>
/// <param name="suppressInSource">If true, then suppressions will be generated inline in the source file. Otherwise, they will be generated in a separate global suppressions file.</param>
/// <param name="projectHierarchyOpt">An optional project hierarchy object in the solution explorer. If non-null, then only the diagnostics from the project will be suppressed.</param>
bool AddSuppressions(bool selectedErrorListEntriesOnly, bool suppressInSource, IVsHierarchy projectHierarchyOpt);
/// <param name="projectHierarchy">An optional project hierarchy object in the solution explorer. If non-null, then only the diagnostics from the project will be suppressed.</param>
bool AddSuppressions(bool selectedErrorListEntriesOnly, bool suppressInSource, IVsHierarchy? projectHierarchy);

/// <summary>
/// Removes source suppressions for suppressed diagnostics.
/// </summary>
/// <param name="selectedErrorListEntriesOnly">If true, then only the currently selected entries in the error list will be unsuppressed. Otherwise, all unsuppressable entries in the error list will be unsuppressed.</param>
/// <param name="projectHierarchyOpt">An optional project hierarchy object in the solution explorer. If non-null, then only the diagnostics from the project will be unsuppressed.</param>
bool RemoveSuppressions(bool selectedErrorListEntriesOnly, IVsHierarchy projectHierarchyOpt);
/// <param name="projectHierarchy">An optional project hierarchy object in the solution explorer. If non-null, then only the diagnostics from the project will be unsuppressed.</param>
bool RemoveSuppressions(bool selectedErrorListEntriesOnly, IVsHierarchy? projectHierarchy);
}
}
3 changes: 3 additions & 0 deletions src/VisualStudio/Core/Def/ServicesVSResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1710,4 +1710,7 @@ I agree to all of the foregoing:</value>
<data name="Search_Settings" xml:space="preserve">
<value>Search Settings</value>
</data>
<data name="Error_updating_suppressions_0" xml:space="preserve">
<value>Error updating suppressions: {0}</value>
</data>
</root>
5 changes: 5 additions & 0 deletions src/VisualStudio/Core/Def/xlf/ServicesVSResources.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/VisualStudio/Core/Def/xlf/ServicesVSResources.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/VisualStudio/Core/Def/xlf/ServicesVSResources.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/VisualStudio/Core/Def/xlf/ServicesVSResources.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/VisualStudio/Core/Def/xlf/ServicesVSResources.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/VisualStudio/Core/Def/xlf/ServicesVSResources.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/VisualStudio/Core/Def/xlf/ServicesVSResources.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/VisualStudio/Core/Def/xlf/ServicesVSResources.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/VisualStudio/Core/Def/xlf/ServicesVSResources.pt-BR.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/VisualStudio/Core/Def/xlf/ServicesVSResources.ru.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/VisualStudio/Core/Def/xlf/ServicesVSResources.tr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/VisualStudio/Core/Def/xlf/ServicesVSResources.zh-Hans.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/VisualStudio/Core/Def/xlf/ServicesVSResources.zh-Hant.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 44be224

Please sign in to comment.