DepartmentProject/DepartmentPortal/Common/CommonTools/Extensions/OperationResultExtensions.cs

66 lines
2.5 KiB
C#
Raw Normal View History

2021-03-25 20:50:19 +04:00
using CommonTools.Enums;
using CommonTools.OperationResultModels;
using System;
namespace CommonTools.Extensions
{
/// <summary>
/// Расширения для результата операции
/// </summary>
public static class OperationResultExtensions
{
/// <summary>
/// Добавление простой ошибки
/// </summary>
/// <param name="model"></param>
/// <param name="key"></param>
/// <param name="value"></param>
public static void AddError(this OperationResultModel model, string key, string value) => model.AddError(key, value, ResultServiceStatusCode.Error);
/// <summary>
/// Добавление простой ошибки со сменой статуса
/// </summary>
/// <param name="model"></param>
/// <param name="key"></param>
/// <param name="error"></param>
/// <param name="statusCode"></param>
public static void AddError(this OperationResultModel model, string key, string error, ResultServiceStatusCode statusCode)
{
model.Errors.Add((key, error));
model.StatusCode = statusCode;
}
/// <summary>
/// Добавление ошибки
/// </summary>
/// <param name="model"></param>
/// <param name="error"></param>
public static void AddError(this OperationResultModel model, Exception error) => model.AddError(error, ResultServiceStatusCode.Error);
/// <summary>
/// Добавление ошибки
/// </summary>
/// <param name="model"></param>
/// <param name="error"></param>
/// <param name="statusCode"></param>
public static void AddError(this OperationResultModel model, Exception error, ResultServiceStatusCode statusCode) => model.AddError("Ошибка", error, statusCode);
/// <summary>
/// Добавление ошибки (включая вложеннные)
/// </summary>
/// <param name="model"></param>
/// <param name="key"></param>
/// <param name="error"></param>
/// <param name="statusCode"></param>
public static void AddError(this OperationResultModel model, string key, Exception error, ResultServiceStatusCode statusCode)
{
model.Errors.Add((key, error.Message));
while (error.InnerException != null)
{
error = error.InnerException;
model.Errors.Add(("Inner error:", error.Message));
}
model.StatusCode = statusCode;
}
}
}