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

View File

@ -8,6 +8,7 @@ using Telegram.Bot.Types;
using TelegramBotBase.Args;
using TelegramBotBase.Base;
using TelegramBotBase.Enums;
using TelegramBotBase.Exceptions;
using TelegramBotBase.Interfaces;
using TelegramBotBase.Sessions;
using Console = TelegramBotBase.Tools.Console;
@ -139,6 +140,13 @@ public sealed class BotBase
mr.IsFirstHandler = false;
} while (ds.FormSwitched && i < GetSetting(ESettings.NavigationMaximum, 10));
}
catch (InvalidServiceProviderConfiguration ex)
{
var ds = Sessions.GetSession(e.DeviceId);
OnException(new SystemExceptionEventArgs(e.Message.Text, e.DeviceId, ds, ex));
throw;
}
catch (Exception ex)
{
var ds = Sessions.GetSession(e.DeviceId);

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace TelegramBotBase.Exceptions
{
public sealed class InvalidServiceProviderConfiguration : Exception
{
public InvalidServiceProviderConfiguration(string message, Exception innerException) : base(message, innerException) { }
}
}

View File

@ -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);