Skip to content

Commit

Permalink
Exception enhancement (#2418)
Browse files Browse the repository at this point in the history
Add new exception type that allows use to determine if it is a loggable
error better....
  • Loading branch information
MrHinsh authored Oct 3, 2024
2 parents 6294190 + 275bec8 commit d589bdd
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 10 deletions.
2 changes: 1 addition & 1 deletion configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"Target": {
"EndpointType": "TfsTeamProjectEndpoint",
"Collection": "https://dev.azure.com/nkdagility-preview/",
"Project": "migrationTest5",
"Project": "migrationTest55",
"TfsVersion": "AzureDevOps",
"Authentication": {
"AuthenticationMode": "AccessToken",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.TeamFoundation.Common;
using Microsoft.TeamFoundation.Core.WebApi;
using Microsoft.TeamFoundation.Server;
using Microsoft.TeamFoundation.Work.WebApi;
using MigrationTools.Clients;
using MigrationTools.DataContracts;
using MigrationTools.Endpoints;
using MigrationTools.Enrichers;
using MigrationTools.Exceptions;
using MigrationTools.FieldMaps;
using MigrationTools.Processors;
using MigrationTools.Processors.Infrastructure;
Expand Down Expand Up @@ -54,13 +56,14 @@ public class TfsNodeStructureTool : Tool<TfsNodeStructureToolOptions>
private TfsLanguageMapOptions _sourceLanguageMaps;
private TfsLanguageMapOptions _targetLanguageMaps;

private ProjectInfo _sourceProjectInfo;
private Microsoft.TeamFoundation.Server.ProjectInfo _sourceProjectInfo;

private string _sourceProjectName;
private NodeInfo[] _sourceRootNodes;
private ICommonStructureService4 _targetCommonStructureService;

private string _targetProjectName;
private Microsoft.TeamFoundation.Server.ProjectInfo _targetProjectInfo;
private KeyValuePair<string, string>? _lastResortRemapRule;

public WorkItemMetrics workItemMetrics { get; private set; }
Expand Down Expand Up @@ -312,6 +315,15 @@ protected void EntryForProcessorType(TfsProcessor processor)
_targetCommonStructureService = processor.Target.GetService<ICommonStructureService4>();
_targetLanguageMaps = processor.Target.Options.LanguageMaps;
_targetProjectName = processor.Target.Options.Project;
try
{
_targetProjectInfo = _targetCommonStructureService.GetProjectFromName(_targetProjectName);
}
catch (ProjectException ex)
{
throw new MigrationToolsException(ex, MigrationToolsException.ExceptionSource.Configuration);
}

}
}
}
Expand Down Expand Up @@ -403,6 +415,8 @@ private static string GetUserFriendlyPath(string systemNodePath)
private void MigrateAllNodeStructures()
{
Log.LogDebug("NodeStructureEnricher.MigrateAllNodeStructures(@{areaMaps}, @{iterationMaps})", Options.Areas, Options.Iterations);


//////////////////////////////////////////////////
ProcessCommonStructure(_sourceLanguageMaps.AreaPath, _targetLanguageMaps.AreaPath, _targetProjectName, TfsNodeStructureType.Area);
//////////////////////////////////////////////////
Expand Down
25 changes: 25 additions & 0 deletions src/MigrationTools/Exceptions/MigrationToolsException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Text;
using Elmah.Io.Client;

namespace MigrationTools.Exceptions
{


public class MigrationToolsException : Exception
{
public MigrationToolsException(Exception ex, ExceptionSource errorSource) : base(ex.Message)
{
this.ErrorSource = errorSource;
}

public ExceptionSource ErrorSource { get; }

public enum ExceptionSource
{
Configuration,
Internal
}
}
}
33 changes: 25 additions & 8 deletions src/MigrationTools/Processors/Infrastructure/Processor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Diagnostics;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -64,13 +65,13 @@ public IEndpoint GetEndpoint(string name)
{
throw new ArgumentException("Endpoint name cannot be null or empty", nameof(name));
}
// Assuming GetRequiredKeyedService throws an exception if the service is not found
IEndpoint endpoint = Services.GetKeyedService<IEndpoint>(name);
if (endpoint == null)
{
throw new ConfigurationValidationException( Options, ValidateOptionsResult.Fail($"The Endpoint '{name}' specified for `{this.GetType().Name}` was not found."));
}
return endpoint;
// Assuming GetRequiredKeyedService throws an exception if the service is not found
IEndpoint endpoint = Services.GetKeyedService<IEndpoint>(name);
if (endpoint == null)
{
throw new ConfigurationValidationException(Options, ValidateOptionsResult.Fail($"The Endpoint '{name}' specified for `{this.GetType().Name}` was not found."));
}
return endpoint;
}

public void Execute()
Expand Down Expand Up @@ -113,12 +114,28 @@ public void Execute()
ProcessorActivity.SetStatus(ActivityStatusCode.Error);
Log.LogCritical(ex, "Validation of your configuration failed:");
}
catch (MigrationToolsException ex)
{
Status = ProcessingStatus.Failed;
ProcessorActivity.SetStatus(ActivityStatusCode.Error);
switch (ex.ErrorSource)
{
case MigrationToolsException.ExceptionSource.Configuration:
Log.LogCritical(ex, "An error occurred in the Migration Tools causing it to stop! This is likley due to a configuration issue and is not being logged remotely. You can always ask on https://github.com/nkdAgility/azure-devops-migration-tools/discussions ");
break;
case MigrationToolsException.ExceptionSource.Internal:
default:
Log.LogCritical(ex, "An error occurred in the Migration Tools causing it to stop!");
Telemetry.TrackException(ex, ProcessorActivity.Tags);
break;
}
}
catch (Exception ex)
{
Status = ProcessingStatus.Failed;
ProcessorActivity.SetStatus(ActivityStatusCode.Error);
Telemetry.TrackException(ex, ProcessorActivity.Tags);
Log.LogCritical(ex, "Error while running {MigrationContextname}", Name);
Telemetry.TrackException(ex, ProcessorActivity.Tags);
}
finally
{
Expand Down

0 comments on commit d589bdd

Please sign in to comment.