From 94252fe63e6139a02450e0672f644439e70ec7f3 Mon Sep 17 00:00:00 2001 From: LD-Reborn Date: Sat, 13 Dec 2025 16:12:57 +0100 Subject: [PATCH] Improved exception handling for indexing --- src/Server/Controllers/EntityController.cs | 51 +++++++++++-------- .../Exceptions/SearchdomainExceptions.cs | 5 ++ src/Server/Helper/SearchdomainHelper.cs | 5 +- src/Server/Probmethods.cs | 4 +- 4 files changed, 40 insertions(+), 25 deletions(-) create mode 100644 src/Server/Exceptions/SearchdomainExceptions.cs diff --git a/src/Server/Controllers/EntityController.cs b/src/Server/Controllers/EntityController.cs index f8a17ce..4e13c5c 100644 --- a/src/Server/Controllers/EntityController.cs +++ b/src/Server/Controllers/EntityController.cs @@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Mvc; using System.Text.Json; using Shared.Models; using Server.Helper; +using Server.Exceptions; namespace Server.Controllers; [ApiController] @@ -43,35 +44,43 @@ public class EntityController : ControllerBase [HttpPost("Index")] public ActionResult Index([FromBody] List? jsonEntities) { - List? entities = SearchdomainHelper.EntitiesFromJSON( - [], - _domainManager.embeddingCache, - _domainManager.aIProvider, - _domainManager.helper, - _logger, - JsonSerializer.Serialize(jsonEntities)); - if (entities is not null && jsonEntities is not null) + try { - List invalidatedSearchdomains = []; - foreach (var jsonEntity in jsonEntities) + List? entities = SearchdomainHelper.EntitiesFromJSON( + [], + _domainManager.embeddingCache, + _domainManager.aIProvider, + _domainManager.helper, + _logger, + JsonSerializer.Serialize(jsonEntities)); + if (entities is not null && jsonEntities is not null) { - string jsonEntityName = jsonEntity.Name; - string jsonEntitySearchdomainName = jsonEntity.Searchdomain; - if (entities.Select(x => x.name == jsonEntityName).Any() - && !invalidatedSearchdomains.Contains(jsonEntitySearchdomainName)) + List invalidatedSearchdomains = []; + foreach (var jsonEntity in jsonEntities) { - invalidatedSearchdomains.Add(jsonEntitySearchdomainName); - _domainManager.InvalidateSearchdomainCache(jsonEntitySearchdomainName); + string jsonEntityName = jsonEntity.Name; + string jsonEntitySearchdomainName = jsonEntity.Searchdomain; + if (entities.Select(x => x.name == jsonEntityName).Any() + && !invalidatedSearchdomains.Contains(jsonEntitySearchdomainName)) + { + invalidatedSearchdomains.Add(jsonEntitySearchdomainName); + _domainManager.InvalidateSearchdomainCache(jsonEntitySearchdomainName); + } } + return Ok(new EntityIndexResult() { Success = true }); } - return Ok(new EntityIndexResult() { Success = true }); - } - else + else + { + _logger.LogError("Unable to deserialize an entity"); + return Ok(new EntityIndexResult() { Success = false, Message = "Unable to deserialize an entity"}); + } + } catch (Exception ex) { - _logger.LogError("Unable to deserialize an entity"); + if (ex.InnerException is not null) ex = ex.InnerException; + _logger.LogError("Unable to index the provided entities. {ex.Message}", [ex.Message]); + return Ok(new EntityIndexResult() { Success = false, Message = ex.Message }); } - return Ok(new EntityIndexResult() { Success = false }); } [HttpGet("List")] diff --git a/src/Server/Exceptions/SearchdomainExceptions.cs b/src/Server/Exceptions/SearchdomainExceptions.cs new file mode 100644 index 0000000..7384532 --- /dev/null +++ b/src/Server/Exceptions/SearchdomainExceptions.cs @@ -0,0 +1,5 @@ +namespace Server.Exceptions; + +public class ProbMethodNotFoundException(string probMethod) : Exception($"Unknown probMethod name {probMethod}") { } + +public class SimilarityMethodNotFoundException(string similarityMethod) : Exception($"Unknown similarityMethod name \"{similarityMethod}\"") { } \ No newline at end of file diff --git a/src/Server/Helper/SearchdomainHelper.cs b/src/Server/Helper/SearchdomainHelper.cs index fdccd11..e34ac1e 100644 --- a/src/Server/Helper/SearchdomainHelper.cs +++ b/src/Server/Helper/SearchdomainHelper.cs @@ -2,6 +2,7 @@ using System.Collections.Concurrent; using System.Security.Cryptography; using System.Text; using System.Text.Json; +using Server.Exceptions; using Shared.Models; namespace Server.Helper; @@ -140,8 +141,8 @@ public static class SearchdomainHelper { embeddings = Datapoint.GenerateEmbeddings(jsonDatapoint.Text, [.. jsonDatapoint.Model], aIProvider, embeddingCache); } - var probMethod_embedding = new ProbMethod(jsonDatapoint.Probmethod_embedding, logger) ?? throw new Exception($"Unknown probMethod name {jsonDatapoint.Probmethod_embedding}"); - var similarityMethod = new SimilarityMethod(jsonDatapoint.SimilarityMethod, logger) ?? throw new Exception($"Unknown similarityMethod name {jsonDatapoint.SimilarityMethod}"); + var probMethod_embedding = new ProbMethod(jsonDatapoint.Probmethod_embedding, logger) ?? throw new ProbMethodNotFoundException(jsonDatapoint.Probmethod_embedding); + var similarityMethod = new SimilarityMethod(jsonDatapoint.SimilarityMethod, logger) ?? throw new SimilarityMethodNotFoundException(jsonDatapoint.SimilarityMethod); Datapoint datapoint = new(jsonDatapoint.Name, probMethod_embedding, similarityMethod, hash, [.. embeddings.Select(kv => (kv.Key, kv.Value))]); int id_datapoint = DatabaseHelper.DatabaseInsertDatapoint(helper, jsonDatapoint.Name, jsonDatapoint.Probmethod_embedding, jsonDatapoint.SimilarityMethod, hash, id_entity); // TODO make this a bulk add action to reduce number of queries List<(string model, byte[] embedding)> data = []; diff --git a/src/Server/Probmethods.cs b/src/Server/Probmethods.cs index d094ff2..5655f40 100644 --- a/src/Server/Probmethods.cs +++ b/src/Server/Probmethods.cs @@ -1,5 +1,5 @@ -using System.Numerics.Tensors; using System.Text.Json; +using Server.Exceptions; namespace Server; @@ -15,7 +15,7 @@ public class ProbMethod if (probMethod is null) { logger.LogError("Unable to retrieve probMethod {name}", [name]); - throw new Exception("Unable to retrieve probMethod"); + throw new ProbMethodNotFoundException(name); } method = probMethod; }