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:
parent
c350c63b1f
commit
052448b6bf
Binary file not shown.
Binary file not shown.
@ -1,4 +1,5 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
namespace DigitalData.Core.API
|
namespace DigitalData.Core.API
|
||||||
{
|
{
|
||||||
@ -22,5 +23,26 @@ namespace DigitalData.Core.API
|
|||||||
viewResult.ViewData[index] = value;
|
viewResult.ViewData[index] = value;
|
||||||
return viewResult;
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Binary file not shown.
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user