Skip to content

Commit ee74e72

Browse files
committed
[#19] Add another part of custom exceptions, add error handling in examples
1 parent 662cbd2 commit ee74e72

24 files changed

+136
-31
lines changed

Examples/Examples/Program.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Examples.Chat;
55
using Examples.Mcp;
66
using MaIN.Core;
7+
using MaIN.Domain.Exceptions;
78
using Microsoft.Extensions.Configuration;
89
using Microsoft.Extensions.DependencyInjection;
910

@@ -121,7 +122,20 @@ async Task RunSelectedExample(IServiceProvider serviceProvider)
121122
Console.ResetColor();
122123

123124
var selectedExample = examples[selection - 1].Instance;
124-
await selectedExample.Start();
125+
try
126+
{
127+
await selectedExample.Start();
128+
}
129+
catch (Exception ex)
130+
{
131+
Console.ForegroundColor = ConsoleColor.Red;
132+
Console.WriteLine("╔════════════════════════════════════════════════════════════════════╗");
133+
Console.WriteLine("║ Error ║");
134+
Console.WriteLine("╚════════════════════════════════════════════════════════════════════╝");
135+
Console.ResetColor();
136+
137+
Console.WriteLine(ex.Message);
138+
}
125139
}
126140
else
127141
{

src/MaIN.Core/Hub/AiHub.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using MaIN.Core.Hub.Contexts;
33
using MaIN.Core.Interfaces;
44
using MaIN.Domain.Configuration;
5+
using MaIN.Domain.Exceptions;
56
using MaIN.Services.Services.Abstract;
67

78
namespace MaIN.Core.Hub;
@@ -23,8 +24,7 @@ internal static void Initialize(IAIHubServices services,
2324

2425
private static IAIHubServices Services =>
2526
_services ??
26-
throw new InvalidOperationException(
27-
"AIHub has not been initialized. Make sure to call AddAIHub() in your service configuration.");
27+
throw new AIHubNotInitializedException();
2828

2929
public static ModelContext Model() => new ModelContext(_settings, _httpClientFactory);
3030
public static ChatContext Chat() => new(Services.ChatService);

src/MaIN.Core/Hub/Contexts/AgentContext.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using MaIN.Core.Hub.Utils;
99
using MaIN.Domain.Entities.Agents.Knowledge;
1010
using MaIN.Domain.Entities.Tools;
11+
using MaIN.Domain.Exceptions;
1112
using MaIN.Services.Constants;
1213

1314
namespace MaIN.Core.Hub.Contexts;
@@ -341,7 +342,9 @@ public static async Task<AgentContext> FromExisting(IAgentService agentService,
341342
{
342343
var existingAgent = await agentService.GetAgentById(agentId);
343344
if (existingAgent == null)
344-
throw new ArgumentException("Agent not found", nameof(agentId));
345+
{
346+
throw new AgentNotFoundException(agentId);
347+
}
345348

346349
var context = new AgentContext(agentService, existingAgent);
347350
context.LoadExistingKnowledgeIfExists();

src/MaIN.Core/Hub/Contexts/FlowContext.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using MaIN.Domain.Entities;
55
using MaIN.Domain.Entities.Agents;
66
using MaIN.Domain.Entities.Agents.AgentSource;
7+
using MaIN.Domain.Exceptions;
78
using MaIN.Services.Dtos;
89
using MaIN.Services.Mappers;
910
using MaIN.Services.Services.Abstract;
@@ -210,7 +211,7 @@ public async Task<AgentFlow> CreateAsync()
210211
public async Task Delete()
211212
{
212213
if (_flow.Id == null)
213-
throw new InvalidOperationException("Flow has not been created yet.");
214+
throw new FlowNotInitializedException();
214215

215216
await _flowService.DeleteFlow(_flow.Id);
216217
}
@@ -219,7 +220,7 @@ public async Task Delete()
219220
public async Task<AgentFlow> GetCurrentFlow()
220221
{
221222
if (_flow.Id == null)
222-
throw new InvalidOperationException("Flow has not been created yet.");
223+
throw new FlowNotInitializedException();
223224

224225
return await _flowService.GetFlowById(_flow.Id);
225226
}
@@ -233,9 +234,8 @@ public async Task<List<AgentFlow>> GetAllFlows()
233234
public async Task<FlowContext> FromExisting(string flowId)
234235
{
235236
var existingFlow = await _flowService.GetFlowById(flowId);
236-
if (existingFlow == null)
237-
throw new ArgumentException("Flow not found", nameof(flowId));
238-
239-
return this;
237+
return existingFlow == null
238+
? throw new FlowFoundException(flowId)
239+
: this;
240240
}
241241
}

src/MaIN.Core/Hub/Contexts/McpContext.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
using MaIN.Domain.Configuration;
33
using MaIN.Domain.Entities;
4+
using MaIN.Domain.Exceptions;
45
using MaIN.Services.Constants;
56
using MaIN.Services.Services;
67
using MaIN.Services.Services.Abstract;
@@ -35,7 +36,7 @@ public async Task<McpResult> PromptAsync(string prompt)
3536
{
3637
if (_mcpConfig == null)
3738
{
38-
throw new InvalidOperationException("MCP config not found");
39+
throw new MPCConfigNotFoundException();
3940
}
4041

4142
return await _mcpService.Prompt(_mcpConfig!, [new Message()
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Net;
2+
3+
namespace MaIN.Domain.Exceptions;
4+
5+
public class AIHubNotInitializedException()
6+
: MaINCustomException("AIHub has not been initialized. Make sure to call 'AddAIHub' in your service configuration.")
7+
{
8+
public override string PublicErrorMessage => LogMessage;
9+
public override HttpStatusCode HttpStatusCode => HttpStatusCode.Conflict;
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.Net;
2+
3+
namespace MaIN.Domain.Exceptions;
4+
5+
public class APIKeyNotConfiguredException(string apiName) : MaINCustomException($"The API key of '{apiName}' has not been configured.")
6+
{
7+
public override string PublicErrorMessage => "Agent not found.";
8+
public override HttpStatusCode HttpStatusCode => HttpStatusCode.InternalServerError;
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Net;
2+
3+
namespace MaIN.Domain.Exceptions;
4+
5+
public class AgentAlreadyExistsException(string agentId)
6+
: MaINCustomException($"Agent with id: '{agentId}' already exists.")
7+
{
8+
public override string PublicErrorMessage => "Agent already exists.";
9+
public override HttpStatusCode HttpStatusCode => HttpStatusCode.Conflict;
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Net;
2+
3+
namespace MaIN.Domain.Exceptions;
4+
5+
public class ChatAlreadyExistsException(string chatId)
6+
: MaINCustomException($"Chat with id: '{chatId}' already exists.")
7+
{
8+
public override string PublicErrorMessage => "Chat already exists.";
9+
public override HttpStatusCode HttpStatusCode => HttpStatusCode.Conflict;
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Net;
2+
3+
namespace MaIN.Domain.Exceptions;
4+
5+
public class FlowAlreadyExistsException(string flowId)
6+
: MaINCustomException($"Flow with id: '{flowId}' already exists.")
7+
{
8+
public override string PublicErrorMessage => "Flow already exists.";
9+
public override HttpStatusCode HttpStatusCode => HttpStatusCode.Conflict;
10+
}

0 commit comments

Comments
 (0)