Skip to content

Commit

Permalink
Exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
zijianhuang committed Jun 23, 2024
1 parent 7aae3c0 commit b644515
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 63 deletions.
6 changes: 3 additions & 3 deletions WebApiClientGenCore/CodeGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ public static void GenerateClientAPIs(string webRootPath, CodeGenSettings settin
tsGen.CreateCodeDom(webApiDescriptions);
tsGen.Save();
}
catch (Exception ex) when (ex is System.IO.FileLoadException || ex is BadImageFormatException || ex is System.IO.FileNotFoundException || ex is ArgumentException)
catch (Exception ex) when (ex is CodeGenLoadPluginException || ex is CodeGenReadPluginException)
{
var s = $"When loading plugin {assemblyName}, {ex.GetType().FullName}: {ex.Message}";
throw new CodeGenException(s, ex);
Console.Error.WriteLine(ex);
throw;
}
}
}
Expand Down
120 changes: 60 additions & 60 deletions WebApiClientGenCore/PluginFactory.cs
Original file line number Diff line number Diff line change
@@ -1,74 +1,74 @@
using Fonlow.CodeDom.Web.Ts;
using Fonlow.Web.Meta;
using System;
using System.Reflection;
using System.Text;

namespace Fonlow.CodeDom.Web
using Fonlow.CodeDom.Web.Ts;
using Fonlow.Web.Meta;
using System;
using System.Reflection;
using System.Text;

namespace Fonlow.CodeDom.Web
{
public static class PluginFactory
public static class PluginFactory
{
/// <summary>
/// Load the first ICommand type found in the assembly and instantiate it.
/// </summary>
/// <param name="assemblyName">The assembly must have a concrete class derived from ICommand, and generally from CommandBase, CommandWithOptions or CommandWithParametersAndOptions;
/// and the class must have a constructor without parameter that calls a base constructor with proper options type and parameters type.</param>
/// <param name="jsOutput"></param>
/// <param name="handleHttpRequestHeaders"></param>
/// <param name="docCommentTranslate"></param>
/// <returns>ICommand object. Null if not found</returns>
/// <exception cref="CodeGenLoadPluginException">Throws when doing loading plugin assembly</exception>
/// <exception cref="CodeGenReadPluginException">Throws when reading plugin assembly.</exception>
public static ControllersTsClientApiGenBase CreateImplementationsFromAssembly(string assemblyName, JSOutput jsOutput, bool handleHttpRequestHeaders, Poco2Client.IDocCommentTranslate docCommentTranslate)
{
Assembly assembly;
try
{
assembly = Assembly.Load(assemblyName);
Console.WriteLine("Assembly {0} is loaded for type {1}.", assemblyName, "ICommand");
/// <summary>
/// Load the first ICommand type found in the assembly and instantiate it.
/// </summary>
/// <param name="assemblyName">The assembly must have a concrete class derived from ICommand, and generally from CommandBase, CommandWithOptions or CommandWithParametersAndOptions;
/// and the class must have a constructor without parameter that calls a base constructor with proper options type and parameters type.</param>
/// <param name="jsOutput"></param>
/// <param name="handleHttpRequestHeaders"></param>
/// <param name="docCommentTranslate"></param>
/// <returns>ICommand object. Null if not found</returns>
/// <exception cref="CodeGenLoadPluginException">Throws when doing loading plugin assembly</exception>
/// <exception cref="CodeGenReadPluginException">Throws when reading plugin assembly.</exception>
public static ControllersTsClientApiGenBase CreateImplementationsFromAssembly(string assemblyName, JSOutput jsOutput, bool handleHttpRequestHeaders, Poco2Client.IDocCommentTranslate docCommentTranslate)
{
Assembly assembly;
try
{
assembly = Assembly.Load(assemblyName);
Console.WriteLine("Assembly {0} is loaded for type {1}.", assemblyName, "ICommand");
}
catch (Exception ex) when (ex is System.IO.FileLoadException || ex is BadImageFormatException || ex is System.IO.FileNotFoundException || ex is ArgumentException)
{
var s = $"When loading plugin {assemblyName}, {ex.GetType().FullName}: {ex.Message}";
throw new CodeGenLoadPluginException(s, ex);
}

ControllersTsClientApiGenBase controllersTsClientApiGen = null;
try
{
foreach (Type type in assembly.GetTypes())
{
if (type.IsClass && type.IsSubclassOf(typeof(ControllersTsClientApiGenBase)))
{
controllersTsClientApiGen = (ControllersTsClientApiGenBase)Activator.CreateInstance(type, jsOutput, handleHttpRequestHeaders, docCommentTranslate);
break;
}
}

if (controllersTsClientApiGen == null)
{
throw new ArgumentException($"Cannot find derived class of ControllersTsClientApiGenBase from {assemblyName}");
}

return controllersTsClientApiGen;

}
catch (ReflectionTypeLoadException reflectionTypeLoadException)
{
var sb = new StringBuilder();
foreach (Exception ex in reflectionTypeLoadException.LoaderExceptions)
{
sb.AppendLine(String.Format("When reading plugin {0}, GetTypes errors occur: {1}", assemblyName, ex.Message));
}

ControllersTsClientApiGenBase controllersTsClientApiGen = null;
try
{
foreach (Type type in assembly.GetTypes())
{
if (type.IsClass && type.IsSubclassOf(typeof(ControllersTsClientApiGenBase)))
{
controllersTsClientApiGen = (ControllersTsClientApiGenBase)Activator.CreateInstance(type, jsOutput, handleHttpRequestHeaders, docCommentTranslate);
break;
}
}

if (controllersTsClientApiGen == null)
{
throw new ArgumentException($"Cannot find derived class of ControllersTsClientApiGenBase from {assemblyName}");
}

return controllersTsClientApiGen;

}
catch (ReflectionTypeLoadException reflectionTypeLoadException)
{
var sb = new StringBuilder();
foreach (Exception ex in reflectionTypeLoadException.LoaderExceptions)
{
sb.AppendLine(String.Format("When reading plugin {0}, GetTypes errors occur: {1}", assemblyName, ex.Message));
}

var s = $"When reading plugin {assemblyName}, ReflectionTypeLoadException: {sb.ToString()}";
throw new CodeGenReadPluginException(s, reflectionTypeLoadException);
}
catch (TargetInvocationException ex)
{
catch (TargetInvocationException ex)
{
var s = $"When reading plugin {assemblyName}, {ex.GetType().FullName}: {ex}";
throw new CodeGenReadPluginException(s, ex);
}
}
}
}
}
}
}
}

0 comments on commit b644515

Please sign in to comment.