Fügen Sie die InnerServiceError-Erweiterung für die Behandlung von 500-Fehlern hinzu

Führt die Methode InnerServiceError ein, um die Fehlerbehandlung in ControllerBase mit optionalen detaillierten Nachrichten oder Ausnahmen zu verbessern. Enthält Dokumentationskommentare.
This commit is contained in:
Developer 02 2024-04-18 16:44:11 +02:00
parent c350c63b1f
commit 052448b6bf
6 changed files with 22 additions and 0 deletions

Binary file not shown.

View File

@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc;
using System.Text;
namespace DigitalData.Core.API
{
@ -22,5 +23,26 @@ namespace DigitalData.Core.API
viewResult.ViewData[index] = value;
return viewResult;
}
/// <summary>
/// Returns an ObjectResult representing an internal server error (status code 500) with optional exception and message details.
/// </summary>
/// <param name="controllerBase">The ControllerBase instance representing the controller.</param>
/// <param name="ex">Optional. The exception that occurred, if any.</param>
/// <param name="message">Optional. A custom error message to include in the response.</param>
/// /// <param name="messageKey">Optional. A custom error message key to include in the response.</param>
/// <returns>An ObjectResult representing an internal server error (status code 500).</returns>
public static ObjectResult InnerServiceError(this ControllerBase controllerBase, Exception? ex = null, string? message = null, Enum? messageKey = null)
{
var sb = new StringBuilder();
if (ex is not null)
sb.AppendLine(ex.Message);
if (message is not null)
sb.AppendLine(message);
if (messageKey is not null)
sb.AppendLine(messageKey.ToString());
return controllerBase.StatusCode(500, sb.Length > 0 ? sb.ToString() : null);
}
}
}