Better error handling on module load

This commit is contained in:
Noikoio 2018-06-06 13:54:51 -07:00
parent 2e178a2f2d
commit 4ca38fa881

View file

@ -19,27 +19,35 @@ namespace Kerobot
var path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + Path.DirectorySeparatorChar; var path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + Path.DirectorySeparatorChar;
var modules = new List<ModuleBase>(); var modules = new List<ModuleBase>();
try foreach (var file in conf.EnabledAssemblies)
{ {
foreach (var file in conf.EnabledAssemblies) Assembly a = null;
try
{ {
var a = Assembly.LoadFile(path + file); a = Assembly.LoadFile(path + file);
modules.AddRange(LoadModulesFromAssembly(a, k));
} }
return modules.AsReadOnly(); catch (Exception ex)
} {
catch (Exception ex) Console.WriteLine("An error occurred when attempting to load a module assembly.");
{ Console.WriteLine($"File: {file}");
// TODO better (not lazy) exception handling Console.WriteLine(ex.ToString());
// Possible exceptions: Environment.Exit(2);
// - Errors loading assemblies }
// - Errors finding module paths
// - Errors creating module instances IEnumerable<ModuleBase> amods = null;
// - Unknown errors try
Console.WriteLine("Module load failed."); {
Console.WriteLine(ex.ToString()); amods = LoadModulesFromAssembly(a, k);
return null; }
catch (Exception ex)
{
Console.WriteLine("An error occurred when attempting to create a module instance.");
Console.WriteLine(ex.ToString());
Environment.Exit(2);
}
modules.AddRange(LoadModulesFromAssembly(a, k));
} }
return modules.AsReadOnly();
} }
static IEnumerable<ModuleBase> LoadModulesFromAssembly(Assembly asm, Kerobot k) static IEnumerable<ModuleBase> LoadModulesFromAssembly(Assembly asm, Kerobot k)
@ -49,7 +57,7 @@ namespace Kerobot
where type.GetCustomAttribute<KerobotModuleAttribute>() != null where type.GetCustomAttribute<KerobotModuleAttribute>() != null
select type; select type;
k.InstanceLogAsync(false, LogName, k.InstanceLogAsync(false, LogName,
$"{asm.FullName} has {eligibleTypes.Count()} usable types:"); $"{asm.GetName().Name} has {eligibleTypes.Count()} usable types");
var newmods = new List<ModuleBase>(); var newmods = new List<ModuleBase>();
foreach (var t in eligibleTypes) foreach (var t in eligibleTypes)