From 9e568911330cd8c1670eef01df3561820131cc97 Mon Sep 17 00:00:00 2001 From: Szymon Date: Mon, 21 Jul 2025 23:04:37 +0200 Subject: [PATCH 1/4] [#19] Add first part of custom exceptions --- .../Examples/Chat/ChatFromExistingExample.cs | 15 ++++++++++--- src/MaIN.Core/Hub/Contexts/ChatContext.cs | 22 ++++++++++++------- .../AgentContextNotFoundException.cs | 9 ++++++++ .../Exceptions/AgentNotFoundException.cs | 10 +++++++++ .../Exceptions/ChatNotFoundException.cs | 10 +++++++++ .../Exceptions/ChatNotInitializedException.cs | 9 ++++++++ .../Exceptions/EmptyChatException.cs | 9 ++++++++ .../Exceptions/MaINCustomException.cs | 10 +++++++++ .../Exceptions/ModelNotSupportedException.cs | 10 +++++++++ src/MaIN.Domain/Models/SupportedModels.cs | 8 +++---- src/MaIN.Services/Services/AgentService.cs | 18 ++++++++++----- src/MaIN.Services/Services/ChatService.cs | 8 ++++++- 12 files changed, 117 insertions(+), 21 deletions(-) create mode 100644 src/MaIN.Domain/Exceptions/AgentContextNotFoundException.cs create mode 100644 src/MaIN.Domain/Exceptions/AgentNotFoundException.cs create mode 100644 src/MaIN.Domain/Exceptions/ChatNotFoundException.cs create mode 100644 src/MaIN.Domain/Exceptions/ChatNotInitializedException.cs create mode 100644 src/MaIN.Domain/Exceptions/EmptyChatException.cs create mode 100644 src/MaIN.Domain/Exceptions/MaINCustomException.cs create mode 100644 src/MaIN.Domain/Exceptions/ModelNotSupportedException.cs diff --git a/Examples/Examples/Chat/ChatFromExistingExample.cs b/Examples/Examples/Chat/ChatFromExistingExample.cs index ce123f2..6b77ac7 100644 --- a/Examples/Examples/Chat/ChatFromExistingExample.cs +++ b/Examples/Examples/Chat/ChatFromExistingExample.cs @@ -1,5 +1,6 @@ using System.Text.Json; using MaIN.Core.Hub; +using MaIN.Domain.Exceptions; namespace Examples; @@ -18,8 +19,16 @@ public async Task Start() await result.WithMessage("And about physics?") .CompleteAsync(); - var chatNewContext = await AIHub.Chat().FromExisting(result.GetChatId()); - var messages = chatNewContext.GetChatHistory(); - Console.WriteLine(JsonSerializer.Serialize(messages)); + try + { + var chatNewContext = await AIHub.Chat().FromExisting(result.GetChatId()); + var messages = chatNewContext.GetChatHistory(); + Console.WriteLine(JsonSerializer.Serialize(messages)); + } + catch (ChatNotFoundException ex) + { + Console.WriteLine(ex.PublicErrorMessage); + } + } } \ No newline at end of file diff --git a/src/MaIN.Core/Hub/Contexts/ChatContext.cs b/src/MaIN.Core/Hub/Contexts/ChatContext.cs index 8e7dd25..967cc8a 100644 --- a/src/MaIN.Core/Hub/Contexts/ChatContext.cs +++ b/src/MaIN.Core/Hub/Contexts/ChatContext.cs @@ -1,5 +1,6 @@ using MaIN.Domain.Configuration; using MaIN.Domain.Entities; +using MaIN.Domain.Exceptions; using MaIN.Domain.Models; using MaIN.Services; using MaIN.Services.Constants; @@ -169,8 +170,9 @@ public async Task CompleteAsync( { if (_chat.Messages.Count == 0) { - throw new InvalidOperationException("Chat has no messages."); //TODO good candidate for domain exception + throw new EmptyChatException(_chat.Id); } + _chat.Messages.Last().Files = _files; if(_preProcess) { @@ -190,7 +192,9 @@ public async Task CompleteAsync( public async Task GetCurrentChat() { if (_chat.Id == null) - throw new InvalidOperationException("Chat has not been created yet. Call CompleteAsync first."); + { + throw new ChatNotInitializedException(); + } return await _chatService.GetById(_chat.Id); } @@ -203,8 +207,10 @@ public async Task> GetAllChats() public async Task DeleteChat() { if (_chat.Id == null) - throw new InvalidOperationException("Chat has not been created yet."); - + { + throw new ChatNotInitializedException(); + } + await _chatService.Delete(_chat.Id); } @@ -225,10 +231,10 @@ private async Task ChatExists(string id) public async Task FromExisting(string chatId) { var existingChat = await _chatService.GetById(chatId); - if (existingChat == null) - { - throw new Exception("Chat not found"); - } + // if (existingChat == null) + // { + // throw new Exception("Chat not found"); + // } return new ChatContext(_chatService, existingChat); } diff --git a/src/MaIN.Domain/Exceptions/AgentContextNotFoundException.cs b/src/MaIN.Domain/Exceptions/AgentContextNotFoundException.cs new file mode 100644 index 0000000..0126e2e --- /dev/null +++ b/src/MaIN.Domain/Exceptions/AgentContextNotFoundException.cs @@ -0,0 +1,9 @@ +using System.Net; + +namespace MaIN.Domain.Exceptions; + +public class AgentContextNotFoundException(string agentId) : MaINCustomException($"Context of agent with id: '{agentId}' not found.") +{ + public override string PublicErrorMessage => "Agent context not found."; + public override HttpStatusCode HttpStatusCode => HttpStatusCode.NotFound; +} \ No newline at end of file diff --git a/src/MaIN.Domain/Exceptions/AgentNotFoundException.cs b/src/MaIN.Domain/Exceptions/AgentNotFoundException.cs new file mode 100644 index 0000000..7a2556d --- /dev/null +++ b/src/MaIN.Domain/Exceptions/AgentNotFoundException.cs @@ -0,0 +1,10 @@ +using System.Net; + +namespace MaIN.Domain.Exceptions; + +public class AgentNotFoundException(string agentId) + : MaINCustomException($"Agent with id: '{agentId}' not found.") +{ + public override string PublicErrorMessage => "Agent not found."; + public override HttpStatusCode HttpStatusCode => HttpStatusCode.NotFound; +} \ No newline at end of file diff --git a/src/MaIN.Domain/Exceptions/ChatNotFoundException.cs b/src/MaIN.Domain/Exceptions/ChatNotFoundException.cs new file mode 100644 index 0000000..e01d301 --- /dev/null +++ b/src/MaIN.Domain/Exceptions/ChatNotFoundException.cs @@ -0,0 +1,10 @@ +using System.Net; + +namespace MaIN.Domain.Exceptions; + +public class ChatNotFoundException(string chatId) + : MaINCustomException($"Chat with id: '{chatId}' not found.") +{ + public override string PublicErrorMessage => "Chat not found."; + public override HttpStatusCode HttpStatusCode => HttpStatusCode.NotFound; +} \ No newline at end of file diff --git a/src/MaIN.Domain/Exceptions/ChatNotInitializedException.cs b/src/MaIN.Domain/Exceptions/ChatNotInitializedException.cs new file mode 100644 index 0000000..4750eb9 --- /dev/null +++ b/src/MaIN.Domain/Exceptions/ChatNotInitializedException.cs @@ -0,0 +1,9 @@ +using System.Net; + +namespace MaIN.Domain.Exceptions; + +public class ChatNotInitializedException() : MaINCustomException("Chat has not been created yet. Call 'CompleteAsync' operation first.") +{ + public override string PublicErrorMessage => Message; + public override HttpStatusCode HttpStatusCode => HttpStatusCode.Conflict; +} \ No newline at end of file diff --git a/src/MaIN.Domain/Exceptions/EmptyChatException.cs b/src/MaIN.Domain/Exceptions/EmptyChatException.cs new file mode 100644 index 0000000..5157132 --- /dev/null +++ b/src/MaIN.Domain/Exceptions/EmptyChatException.cs @@ -0,0 +1,9 @@ +using System.Net; + +namespace MaIN.Domain.Exceptions; + +public class EmptyChatException(string chatId) : MaINCustomException($"Chat with id: '{chatId}' is empty. Complete operation is impossible.") +{ + public override string PublicErrorMessage => "Complete operation is impossible, because chat has no message."; + public override HttpStatusCode HttpStatusCode => HttpStatusCode.Conflict; +} \ No newline at end of file diff --git a/src/MaIN.Domain/Exceptions/MaINCustomException.cs b/src/MaIN.Domain/Exceptions/MaINCustomException.cs new file mode 100644 index 0000000..7108432 --- /dev/null +++ b/src/MaIN.Domain/Exceptions/MaINCustomException.cs @@ -0,0 +1,10 @@ +using System.Net; + +namespace MaIN.Domain.Exceptions; + +public abstract class MaINCustomException(string message) : Exception(message) +{ + public string LogMessage { get; private set; } = message; + public abstract string PublicErrorMessage { get; } + public abstract HttpStatusCode HttpStatusCode { get; } +} \ No newline at end of file diff --git a/src/MaIN.Domain/Exceptions/ModelNotSupportedException.cs b/src/MaIN.Domain/Exceptions/ModelNotSupportedException.cs new file mode 100644 index 0000000..4ac460e --- /dev/null +++ b/src/MaIN.Domain/Exceptions/ModelNotSupportedException.cs @@ -0,0 +1,10 @@ +using System.Net; + +namespace MaIN.Domain.Exceptions; + +public class ModelNotSupportedException(string? modelName) + : MaINCustomException($"Given model {modelName ?? string.Empty} is not supported.") +{ + public override string PublicErrorMessage => Message; + public override HttpStatusCode HttpStatusCode => HttpStatusCode.BadRequest; +} \ No newline at end of file diff --git a/src/MaIN.Domain/Models/SupportedModels.cs b/src/MaIN.Domain/Models/SupportedModels.cs index 61354f5..dfe0703 100644 --- a/src/MaIN.Domain/Models/SupportedModels.cs +++ b/src/MaIN.Domain/Models/SupportedModels.cs @@ -1,3 +1,5 @@ +using MaIN.Domain.Exceptions; + namespace MaIN.Domain.Models; public class Model @@ -202,8 +204,7 @@ public static Model GetModel(string path, string? name) StringComparison.InvariantCultureIgnoreCase)); if (model is null) { - //todo support domain specific exceptions - throw new Exception($"Model {name} is not supported"); + throw new ModelNotSupportedException(name); } if (File.Exists(Path.Combine(path, model.FileName))) @@ -252,8 +253,7 @@ public static Model GetModel(string modelName) StringComparison.InvariantCultureIgnoreCase)); if (model is null) { - //todo support domain specific exceptions - throw new NotSupportedException($"Model {modelName} is not supported"); + throw new ModelNotSupportedException(modelName); } return model; diff --git a/src/MaIN.Services/Services/AgentService.cs b/src/MaIN.Services/Services/AgentService.cs index d0c0827..34f95f7 100644 --- a/src/MaIN.Services/Services/AgentService.cs +++ b/src/MaIN.Services/Services/AgentService.cs @@ -1,6 +1,7 @@ using MaIN.Domain.Configuration; using MaIN.Domain.Entities; using MaIN.Domain.Entities.Agents; +using MaIN.Domain.Exceptions; using MaIN.Infrastructure.Repositories.Abstract; using MaIN.Services.Constants; using MaIN.Services.Mappers; @@ -28,10 +29,15 @@ public class AgentService( public async Task Process(Chat chat, string agentId, bool translatePrompt = false) { var agent = await agentRepository.GetAgentById(agentId); - if (agent == null) - throw new ArgumentException("Agent not found."); //TODO candidate for NotFound domain exception - if (agent.Context == null) - throw new ArgumentException("Agent context not found."); + if (agent == null) + { + throw new AgentNotFoundException(agentId); + } + + if (agent.Context == null) + { + throw new AgentContextNotFoundException(agentId); + } await notificationService.DispatchNotification( NotificationMessageBuilder.ProcessingStarted(agentId, agent.CurrentBehaviour), "ReceiveAgentUpdate"); @@ -115,7 +121,9 @@ public async Task GetChatByAgent(string agentId) { var agent = await agentRepository.GetAgentById(agentId); if (agent == null) - throw new Exception("Agent not found."); //TODO good candidate for custom exception + { + throw new AgentNotFoundException(agentId); + } var chat = await chatRepository.GetChatById(agent.ChatId); return chat!.ToDomain(); diff --git a/src/MaIN.Services/Services/ChatService.cs b/src/MaIN.Services/Services/ChatService.cs index 20079b2..6e5dce7 100644 --- a/src/MaIN.Services/Services/ChatService.cs +++ b/src/MaIN.Services/Services/ChatService.cs @@ -1,5 +1,6 @@ using MaIN.Domain.Configuration; using MaIN.Domain.Entities; +using MaIN.Domain.Exceptions; using MaIN.Domain.Models; using MaIN.Infrastructure.Repositories.Abstract; using MaIN.Services.Mappers; @@ -89,7 +90,12 @@ public async Task Delete(string id) public async Task GetById(string id) { var chatDocument = await chatProvider.GetChatById(id); - if(chatDocument == null) throw new Exception("Chat not found"); //TODO good candidate for custom exception + if (chatDocument == null) + { + throw new ChatNotFoundException(id); + // throw new Exception("Chat not found"); + //TODO good candidate for custom exception + } return chatDocument.ToDomain(); } From 7979825be5d9804ac72c658bcb3e5ee0d2adb8bc Mon Sep 17 00:00:00 2001 From: Szymon Date: Thu, 24 Jul 2025 16:13:07 +0200 Subject: [PATCH 2/4] [#19] Add other custom exceptions --- .../Exceptions/ApiRequestFailedException.cs | 10 ++++++++++ src/MaIN.Domain/Exceptions/CommandFailedException.cs | 10 ++++++++++ src/MaIN.Domain/Models/SupportedModels.cs | 3 +-- src/MaIN.Services/Services/AgentService.cs | 4 +++- src/MaIN.Services/Services/ChatService.cs | 3 +-- src/MaIN.Services/Services/DataSourceProvider.cs | 7 +++++-- src/MaIN.Services/Services/Steps/AnswerStepHandler.cs | 8 +++++--- .../Services/Steps/FechDataStepHandler.cs | 3 ++- src/MaIN.Services/Services/Steps/McpStepHandler.cs | 3 ++- 9 files changed, 39 insertions(+), 12 deletions(-) create mode 100644 src/MaIN.Domain/Exceptions/ApiRequestFailedException.cs create mode 100644 src/MaIN.Domain/Exceptions/CommandFailedException.cs diff --git a/src/MaIN.Domain/Exceptions/ApiRequestFailedException.cs b/src/MaIN.Domain/Exceptions/ApiRequestFailedException.cs new file mode 100644 index 0000000..903136b --- /dev/null +++ b/src/MaIN.Domain/Exceptions/ApiRequestFailedException.cs @@ -0,0 +1,10 @@ +using System.Net; + +namespace MaIN.Domain.Exceptions; + +public class ApiRequestFailedException(HttpStatusCode statusCode, string requestUrl, string httpMethod) + : MaINCustomException($"API request failed with status code: {statusCode}. Request url: {requestUrl}. Http method: {httpMethod}.") +{ + public override string PublicErrorMessage => "An error occurred while processing an external API request"; + public override HttpStatusCode HttpStatusCode => HttpStatusCode.InternalServerError; +} \ No newline at end of file diff --git a/src/MaIN.Domain/Exceptions/CommandFailedException.cs b/src/MaIN.Domain/Exceptions/CommandFailedException.cs new file mode 100644 index 0000000..25ce01e --- /dev/null +++ b/src/MaIN.Domain/Exceptions/CommandFailedException.cs @@ -0,0 +1,10 @@ +using System.Net; + +namespace MaIN.Domain.Exceptions; + +public class CommandFailedException(string commandName) + : MaINCustomException($"{commandName} command execution failed.") +{ + public override string PublicErrorMessage => Message; + public override HttpStatusCode HttpStatusCode => HttpStatusCode.InternalServerError; +} \ No newline at end of file diff --git a/src/MaIN.Domain/Models/SupportedModels.cs b/src/MaIN.Domain/Models/SupportedModels.cs index dfe0703..cfb09cb 100644 --- a/src/MaIN.Domain/Models/SupportedModels.cs +++ b/src/MaIN.Domain/Models/SupportedModels.cs @@ -220,8 +220,7 @@ public static Model GetModel(string path, string? name) var isPresent = Models.Exists(x => x.FileName == fileName); if (!isPresent) { - //todo support domain specific exceptions - Console.WriteLine($"Model {fileName} is not supported"); + Console.WriteLine($"{new ModelNotSupportedException(fileName).PublicErrorMessage}"); return null; } diff --git a/src/MaIN.Services/Services/AgentService.cs b/src/MaIN.Services/Services/AgentService.cs index 34f95f7..6c991b5 100644 --- a/src/MaIN.Services/Services/AgentService.cs +++ b/src/MaIN.Services/Services/AgentService.cs @@ -133,7 +133,9 @@ public async Task Restart(string agentId) { var agent = await agentRepository.GetAgentById(agentId); if (agent == null) - throw new Exception("Agent not found."); //TODO good candidate for custom exception + { + throw new AgentNotFoundException(agentId); + } var chat = (await chatRepository.GetChatById(agent.ChatId))!.ToDomain(); var llmService = llmServiceFactory.CreateService(agent.Backend ?? maInSettings.BackendType); diff --git a/src/MaIN.Services/Services/ChatService.cs b/src/MaIN.Services/Services/ChatService.cs index 6e5dce7..c40cbae 100644 --- a/src/MaIN.Services/Services/ChatService.cs +++ b/src/MaIN.Services/Services/ChatService.cs @@ -93,9 +93,8 @@ public async Task GetById(string id) if (chatDocument == null) { throw new ChatNotFoundException(id); - // throw new Exception("Chat not found"); - //TODO good candidate for custom exception } + return chatDocument.ToDomain(); } diff --git a/src/MaIN.Services/Services/DataSourceProvider.cs b/src/MaIN.Services/Services/DataSourceProvider.cs index e92c5b3..b9bbd79 100644 --- a/src/MaIN.Services/Services/DataSourceProvider.cs +++ b/src/MaIN.Services/Services/DataSourceProvider.cs @@ -2,6 +2,7 @@ using System.Text; using System.Text.Json; using MaIN.Domain.Entities.Agents.AgentSource; +using MaIN.Domain.Exceptions; using MaIN.Services.Services.Abstract; using MaIN.Services.Utils; using MongoDB.Bson; @@ -76,8 +77,10 @@ public async Task FetchApiData(object? details, string? filter, var result = await httpClient.SendAsync(request); if (!result.IsSuccessStatusCode) { - throw new Exception( - $"API request failed with status code: {result.StatusCode}"); //TODO candidate for domain exception + throw new ApiRequestFailedException( + result.StatusCode, + apiDetails?.Url + apiDetails?.Query, + apiDetails?.Method ?? string.Empty); } var data = await result.Content.ReadAsStringAsync(); diff --git a/src/MaIN.Services/Services/Steps/AnswerStepHandler.cs b/src/MaIN.Services/Services/Steps/AnswerStepHandler.cs index a2fc7f8..18dc4aa 100644 --- a/src/MaIN.Services/Services/Steps/AnswerStepHandler.cs +++ b/src/MaIN.Services/Services/Steps/AnswerStepHandler.cs @@ -1,6 +1,7 @@ using System.Text.RegularExpressions; using DocumentFormat.OpenXml.Wordprocessing; using MaIN.Domain.Entities; +using MaIN.Domain.Exceptions; using MaIN.Services.Services.Abstract; using MaIN.Services.Services.Models; using MaIN.Services.Services.Models.Commands; @@ -24,9 +25,10 @@ public async Task Handle(StepContext context) }; var answerResponse = await commandDispatcher.DispatchAsync(answerCommand); - if (answerResponse == null) - throw new Exception("Answer command failed"); //TODO proper candidate for custom exception - + if (answerResponse == null) + { + throw new CommandFailedException(answerCommand.CommandName); + } var filterVal = GetFilter(answerResponse.Content); if (!string.IsNullOrEmpty(filterVal)) diff --git a/src/MaIN.Services/Services/Steps/FechDataStepHandler.cs b/src/MaIN.Services/Services/Steps/FechDataStepHandler.cs index 6c70818..f8745d3 100644 --- a/src/MaIN.Services/Services/Steps/FechDataStepHandler.cs +++ b/src/MaIN.Services/Services/Steps/FechDataStepHandler.cs @@ -1,5 +1,6 @@ using MaIN.Domain.Configuration; using MaIN.Domain.Entities; +using MaIN.Domain.Exceptions; using MaIN.Services.Mappers; using MaIN.Services.Services.Abstract; using MaIN.Services.Services.Models; @@ -35,7 +36,7 @@ public async Task Handle(StepContext context) var response = await commandDispatcher.DispatchAsync(fetchCommand); if (response == null) { - throw new InvalidOperationException("Data fetch command failed"); //TODO proper candidate for custom exception + throw new CommandFailedException(fetchCommand.CommandName); } if (context.StepName == "FETCH_DATA*") diff --git a/src/MaIN.Services/Services/Steps/McpStepHandler.cs b/src/MaIN.Services/Services/Steps/McpStepHandler.cs index b3cb686..9c501a7 100644 --- a/src/MaIN.Services/Services/Steps/McpStepHandler.cs +++ b/src/MaIN.Services/Services/Steps/McpStepHandler.cs @@ -1,6 +1,7 @@ using System.Text.RegularExpressions; using DocumentFormat.OpenXml.Wordprocessing; using MaIN.Domain.Entities; +using MaIN.Domain.Exceptions; using MaIN.Services.Services.Abstract; using MaIN.Services.Services.Models; using MaIN.Services.Services.Models.Commands; @@ -29,7 +30,7 @@ public async Task Handle(StepContext context) var mcpResponse = await commandDispatcher.DispatchAsync(mcpCommand); if (mcpResponse == null) { - throw new Exception("MCP command failed"); //TODO proper candidate for custom exception + throw new CommandFailedException(mcpCommand.CommandName); } var filterVal = GetFilter(mcpResponse.Content); From d1e4e62f69a751ffaaf2796309c8fcbaa825b630 Mon Sep 17 00:00:00 2001 From: Szymon Date: Thu, 24 Jul 2025 20:58:42 +0200 Subject: [PATCH 3/4] Add some improvements --- src/MaIN.Core/Hub/Contexts/ChatContext.cs | 8 ++++---- .../Exceptions/AgentFlowNotFoundException.cs | 9 +++++++++ src/MaIN.Domain/Exceptions/MaINCustomException.cs | 11 +++++++++++ .../Exceptions/ModelNotDownloadedException.cs | 10 ++++++++++ src/MaIN.Domain/Models/SupportedModels.cs | 4 ++-- 5 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 src/MaIN.Domain/Exceptions/AgentFlowNotFoundException.cs create mode 100644 src/MaIN.Domain/Exceptions/ModelNotDownloadedException.cs diff --git a/src/MaIN.Core/Hub/Contexts/ChatContext.cs b/src/MaIN.Core/Hub/Contexts/ChatContext.cs index 967cc8a..b4a6026 100644 --- a/src/MaIN.Core/Hub/Contexts/ChatContext.cs +++ b/src/MaIN.Core/Hub/Contexts/ChatContext.cs @@ -231,10 +231,10 @@ private async Task ChatExists(string id) public async Task FromExisting(string chatId) { var existingChat = await _chatService.GetById(chatId); - // if (existingChat == null) - // { - // throw new Exception("Chat not found"); - // } + if (existingChat == null) + { + throw new ChatNotFoundException(chatId); + } return new ChatContext(_chatService, existingChat); } diff --git a/src/MaIN.Domain/Exceptions/AgentFlowNotFoundException.cs b/src/MaIN.Domain/Exceptions/AgentFlowNotFoundException.cs new file mode 100644 index 0000000..bb27181 --- /dev/null +++ b/src/MaIN.Domain/Exceptions/AgentFlowNotFoundException.cs @@ -0,0 +1,9 @@ +using System.Net; + +namespace MaIN.Domain.Exceptions; + +public class AgentFlowNotFoundException(string flowId) : MaINCustomException($"Agent flow with id: '{flowId}' not found.") +{ + public override string PublicErrorMessage => "Agent flow not found."; + public override HttpStatusCode HttpStatusCode => HttpStatusCode.NotFound; +} \ No newline at end of file diff --git a/src/MaIN.Domain/Exceptions/MaINCustomException.cs b/src/MaIN.Domain/Exceptions/MaINCustomException.cs index 7108432..2cf5084 100644 --- a/src/MaIN.Domain/Exceptions/MaINCustomException.cs +++ b/src/MaIN.Domain/Exceptions/MaINCustomException.cs @@ -1,10 +1,21 @@ using System.Net; +using System.Text.RegularExpressions; namespace MaIN.Domain.Exceptions; public abstract class MaINCustomException(string message) : Exception(message) { + public string ErrorCode => GenerateErrorCode(); public string LogMessage { get; private set; } = message; public abstract string PublicErrorMessage { get; } public abstract HttpStatusCode HttpStatusCode { get; } + + private string GenerateErrorCode() + { + var typeName = GetType().Name; + var snakeCaseTypeName = Regex.Replace(typeName, "(? Message; + public override HttpStatusCode HttpStatusCode => HttpStatusCode.NotFound; +} \ No newline at end of file diff --git a/src/MaIN.Domain/Models/SupportedModels.cs b/src/MaIN.Domain/Models/SupportedModels.cs index cfb09cb..b0459ad 100644 --- a/src/MaIN.Domain/Models/SupportedModels.cs +++ b/src/MaIN.Domain/Models/SupportedModels.cs @@ -212,7 +212,7 @@ public static Model GetModel(string path, string? name) return model; } - throw new Exception($"Model {name} is not downloaded"); + throw new ModelNotDownloadedException(name); } public static Model? GetModelByFileName(string path, string fileName) @@ -229,7 +229,7 @@ public static Model GetModel(string path, string? name) return Models.First(x => x.FileName == fileName); } - throw new Exception($"Model {fileName} is not downloaded"); + throw new ModelNotDownloadedException(fileName); } public static void AddModel(string model, string path, string? mmProject = null) From d45740d2e00efabc87cb9ce5423bcfbcdf746b9c Mon Sep 17 00:00:00 2001 From: Szymon Date: Thu, 24 Jul 2025 21:01:35 +0200 Subject: [PATCH 4/4] Tweaks --- src/MaIN.Domain/Exceptions/MaINCustomException.cs | 1 - src/MaIN.Services/Services/AgentFlowService.cs | 7 +++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/MaIN.Domain/Exceptions/MaINCustomException.cs b/src/MaIN.Domain/Exceptions/MaINCustomException.cs index 2cf5084..2530f8b 100644 --- a/src/MaIN.Domain/Exceptions/MaINCustomException.cs +++ b/src/MaIN.Domain/Exceptions/MaINCustomException.cs @@ -17,5 +17,4 @@ private string GenerateErrorCode() return snakeCaseTypeName.Replace("_exception", string.Empty); } - } \ No newline at end of file diff --git a/src/MaIN.Services/Services/AgentFlowService.cs b/src/MaIN.Services/Services/AgentFlowService.cs index 762a166..1ab7e8c 100644 --- a/src/MaIN.Services/Services/AgentFlowService.cs +++ b/src/MaIN.Services/Services/AgentFlowService.cs @@ -1,4 +1,5 @@ using MaIN.Domain.Entities.Agents.AgentSource; +using MaIN.Domain.Exceptions; using MaIN.Infrastructure.Models; using MaIN.Infrastructure.Repositories.Abstract; using MaIN.Services.Mappers; @@ -11,8 +12,10 @@ public class AgentFlowService(IAgentFlowRepository flowRepository, IAgentService public async Task GetFlowById(string id) { var flow = await flowRepository.GetFlowById(id); - if(flow is null) - throw new Exception("Flow not found"); + if (flow is null) + { + throw new AgentFlowNotFoundException(id); + } return flow.ToDomain(); }