Adding a exception for invalid ServiceProviders #62

- adding an internal exception handler to catch InvalidOperationException and throw a new explicit one
This commit is contained in:
Florian Zevedei
2024-03-10 17:09:13 +01:00
parent 6e7acdbdba
commit b829f43c5d
3 changed files with 31 additions and 1 deletions
@@ -1,6 +1,7 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using TelegramBotBase.DependencyInjection;
using TelegramBotBase.Exceptions;
using TelegramBotBase.Form;
using TelegramBotBase.Interfaces;
@@ -24,7 +25,16 @@ public class ServiceProviderStartFormFactory : IStartFormFactory
public FormBase CreateForm()
{
var fb = (FormBase)ActivatorUtilities.CreateInstance(_serviceProvider, _startFormClass);
FormBase fb = null;
try
{
fb = (FormBase)ActivatorUtilities.CreateInstance(_serviceProvider, _startFormClass);
}
catch(InvalidOperationException ex)
{
throw new InvalidServiceProviderConfiguration(ex.Message, ex);
}
//Sets an internal field for future ServiceProvider navigation
fb.SetServiceProvider(_serviceProvider);