Improved exception handling and logging in SearchdomainController

This commit is contained in:
2025-12-13 17:25:07 +01:00
parent 2f4a506077
commit dedac5da3a
2 changed files with 31 additions and 3 deletions

View File

@@ -1,5 +1,6 @@
using ElmahCore; using ElmahCore;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Server.Exceptions;
using Shared.Models; using Shared.Models;
namespace Server.Controllers; namespace Server.Controllers;
@@ -46,7 +47,7 @@ public class SearchdomainController : ControllerBase
} catch (Exception) } catch (Exception)
{ {
_logger.LogError("Unable to create Searchdomain {searchdomain}", [searchdomain]); _logger.LogError("Unable to create Searchdomain {searchdomain}", [searchdomain]);
return Ok(new SearchdomainCreateResults() { Id = null, Success = false }); return Ok(new SearchdomainCreateResults() { Id = null, Success = false, Message = $"Unable to create Searchdomain {searchdomain}" });
} }
} }
@@ -55,19 +56,29 @@ public class SearchdomainController : ControllerBase
{ {
bool success; bool success;
int deletedEntries; int deletedEntries;
string? message = null;
try try
{ {
success = true; success = true;
deletedEntries = _domainManager.DeleteSearchdomain(searchdomain); deletedEntries = _domainManager.DeleteSearchdomain(searchdomain);
} }
catch (SearchdomainNotFoundException ex)
{
_logger.LogError("Unable to delete searchdomain {searchdomain} - not found", [searchdomain]);
success = false;
deletedEntries = 0;
message = $"Unable to delete searchdomain {searchdomain} - not found";
ElmahExtensions.RaiseError(ex);
}
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError("Unable to delete searchdomain {searchdomain}", [searchdomain]); _logger.LogError("Unable to delete searchdomain {searchdomain}", [searchdomain]);
success = false; success = false;
deletedEntries = 0; deletedEntries = 0;
message = ex.Message;
ElmahExtensions.RaiseError(ex); ElmahExtensions.RaiseError(ex);
} }
return Ok(new SearchdomainDeleteResults(){Success = success, DeletedEntities = deletedEntries}); return Ok(new SearchdomainDeleteResults(){Success = success, DeletedEntities = deletedEntries, Message = message});
} }
[HttpGet("Update")] [HttpGet("Update")]
@@ -83,10 +94,14 @@ public class SearchdomainController : ControllerBase
{"id", searchdomain_.id} {"id", searchdomain_.id}
}; };
searchdomain_.helper.ExecuteSQLNonQuery("UPDATE searchdomain set name = @name, settings = @settings WHERE id = @id", parameters); searchdomain_.helper.ExecuteSQLNonQuery("UPDATE searchdomain set name = @name, settings = @settings WHERE id = @id", parameters);
} catch (SearchdomainNotFoundException)
{
_logger.LogError("Unable to update searchdomain {searchdomain} - not found", [searchdomain]);
return Ok(new SearchdomainUpdateResults() { Success = false, Message = $"Unable to update searchdomain {searchdomain} - not found" });
} catch (Exception) } catch (Exception)
{ {
_logger.LogError("Unable to update searchdomain {searchdomain}", [searchdomain]); _logger.LogError("Unable to update searchdomain {searchdomain}", [searchdomain]);
return Ok(new SearchdomainUpdateResults() { Success = false }); return Ok(new SearchdomainUpdateResults() { Success = false, Message = $"Unable to update searchdomain {searchdomain}" });
} }
return Ok(new SearchdomainUpdateResults(){Success = true}); return Ok(new SearchdomainUpdateResults(){Success = true});
} }

View File

@@ -6,6 +6,9 @@ public class SearchdomainListResults
{ {
[JsonPropertyName("Searchdomains")] // Otherwise the api returns {"searchdomains": [...]} and the client requires {"Searchdomains": [...]} [JsonPropertyName("Searchdomains")] // Otherwise the api returns {"searchdomains": [...]} and the client requires {"Searchdomains": [...]}
public required List<string> Searchdomains { get; set; } public required List<string> Searchdomains { get; set; }
[JsonPropertyName("Message")]
public string? Message { get; set; }
} }
public class SearchdomainCreateResults public class SearchdomainCreateResults
@@ -13,6 +16,9 @@ public class SearchdomainCreateResults
[JsonPropertyName("Success")] [JsonPropertyName("Success")]
public required bool Success { get; set; } public required bool Success { get; set; }
[JsonPropertyName("Message")]
public string? Message { get; set; }
[JsonPropertyName("Id")] [JsonPropertyName("Id")]
public int? Id { get; set; } public int? Id { get; set; }
} }
@@ -21,12 +27,19 @@ public class SearchdomainUpdateResults
{ {
[JsonPropertyName("Success")] [JsonPropertyName("Success")]
public required bool Success { get; set; } public required bool Success { get; set; }
[JsonPropertyName("Message")]
public string? Message { get; set; }
} }
public class SearchdomainDeleteResults public class SearchdomainDeleteResults
{ {
[JsonPropertyName("Success")] [JsonPropertyName("Success")]
public required bool Success { get; set; } public required bool Success { get; set; }
[JsonPropertyName("Message")]
public string? Message { get; set; }
[JsonPropertyName("DeletedEntities")] [JsonPropertyName("DeletedEntities")]
public required int DeletedEntities { get; set; } public required int DeletedEntities { get; set; }
} }