Skip to content

Commit

Permalink
Update Exceptions for v16 (#2316)
Browse files Browse the repository at this point in the history
Checking the Elmah.io logs for exceptions.

- [x] Updated Invalid Processor Errors! Now gives a message on what is
not valid, and gives the user a list of valid options.
  • Loading branch information
MrHinsh authored Aug 29, 2024
2 parents f1a1868 + b045a9a commit 803de11
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 21 deletions.
20 changes: 10 additions & 10 deletions docs/Reference/Generated/MigrationTools.xml

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

Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ internal override async Task<int> ExecuteInternalAsync(CommandContext context, E
{
Telemetery.TrackException(ex, null, null);
_logger.LogError(ex, "Unhandled exception!");

_exitCode = 1;
}
finally
Expand Down
7 changes: 5 additions & 2 deletions src/MigrationTools.Host/MigrationToolHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,12 @@ public static IHostBuilder CreateDefaultBuilder(string[] args, Action<IConfigura
extraCommands?.Invoke(config);
config.PropagateExceptions();
});
hostBuilder.UseConsoleLifetime();

hostBuilder.UseConsoleLifetime(configureOptions =>
{
configureOptions.SuppressStatusMessages = true;
} );



return hostBuilder;
}
Expand Down
4 changes: 0 additions & 4 deletions src/MigrationTools/MigrationTools.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,4 @@
<PackageReference Update="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

<ItemGroup>
<Folder Include="Processors\Infrastructure\" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace MigrationTools.Processors.Infrastructure
{
public class InvalidProcessorException : Exception
{

public InvalidProcessorException() : base()
{
}
public InvalidProcessorException(string message) : base(message)
{
}
public InvalidProcessorException(string message, Exception innerException) : base(message, innerException)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using MigrationTools._EngineV1.Configuration;
Expand Down Expand Up @@ -34,20 +35,24 @@ public void Configure(ProcessorContainerOptions options)
private void BindProcessorOptions(ProcessorContainerOptions options, string sectionName, string objectTypePropertyName)
{
_configuration.GetSection(sectionName).Bind(options);
var validProcessors = AppDomain.CurrentDomain.GetMigrationToolsTypes().WithInterface<IProcessorOptions>();

foreach (var processorSection in _configuration.GetSection(sectionName).GetChildren())
{
var processorTypeString = processorSection.GetValue<string>(objectTypePropertyName);
if (processorTypeString == null)
{
Log.Warning("There was no value for {optionTypeName} from {sectionKey}", objectTypePropertyName, processorSection.Key);
throw new Exception();
Log.Fatal("Your processor at `{path}` in the config does not have a property called {objectTypePropertyName} that is required to sucessfully detect the type and load it. ", processorSection.Path, objectTypePropertyName);
throw new InvalidProcessorException($"`{objectTypePropertyName}` missing");
}
var processorType = AppDomain.CurrentDomain.GetMigrationToolsTypes().WithInterface<IProcessorOptions>().WithNameString(processorTypeString);


var processorType = validProcessors.WithNameString(processorTypeString);
if (processorType == null)
{
Log.Warning("There was no match for {optionTypeName} from {sectionKey}", objectTypePropertyName, processorSection.Key);
throw new Exception();
Log.Fatal("The value of {objectTypePropertyName} for your processor at `{path}` may have an error as were were unable to find a type that matches {processorTypeString}! Please check the spelling, and that its a processor listed in the documentation.", objectTypePropertyName, processorSection.Path, processorTypeString);
Log.Information("Valid options are @{validProcessors}", validProcessors.Select(type => type.Name).ToList());
throw new InvalidProcessorException($"`{processorTypeString}` is not valid");
}

IProcessorOptions processorOption = Activator.CreateInstance(processorType) as IProcessorOptions;
Expand Down

0 comments on commit 803de11

Please sign in to comment.