using CommonTools.Enums;
using CommonTools.OperationResultModels;
using System;
namespace CommonTools.Extensions
{
///
/// Расширения для результата операции
///
public static class OperationResultExtensions
{
///
/// Добавление простой ошибки
///
///
///
///
public static void AddError(this OperationResultModel model, string key, string value) => model.AddError(key, value, ResultServiceStatusCode.Error);
///
/// Добавление простой ошибки со сменой статуса
///
///
///
///
///
public static void AddError(this OperationResultModel model, string key, string error, ResultServiceStatusCode statusCode)
{
model.Errors.Add((key, error));
model.StatusCode = statusCode;
}
///
/// Добавление ошибки
///
///
///
public static void AddError(this OperationResultModel model, Exception error) => model.AddError(error, ResultServiceStatusCode.Error);
///
/// Добавление ошибки
///
///
///
///
public static void AddError(this OperationResultModel model, Exception error, ResultServiceStatusCode statusCode) => model.AddError("Ошибка", error, statusCode);
///
/// Добавление ошибки (включая вложеннные)
///
///
///
///
///
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;
}
}
}