Files
hcs/Hcs.Client/ClientApi/HcsRemoteException.cs
HOME-LAPTOP\kshkulev 33ab055b43 Add project
Basic formatting applied. Unnecessary comments have been removed. Suspicious code is covered by TODO.
2025-08-12 11:21:10 +09:00

61 lines
2.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Linq;
namespace Hcs.ClientApi
{
/// <summary>
/// Сообщение об ошибке возникшей на удаленном сервере ГИС ЖКХ
/// </summary>
public class HcsRemoteException : HcsException
{
public string ErrorCode { get; private set; }
public string Description { get; private set; }
public class KnownCodes
{
public const string НетОбъектовДляЭкспорта = "INT002012";
public const string ОтсутствуетВРеестре = "INT002000";
public const string ДоступЗапрещен = "AUT011003";
}
public HcsRemoteException(string errorCode, string description)
: base(MakeMessage(errorCode, description))
{
this.ErrorCode = errorCode;
this.Description = description;
}
public HcsRemoteException(string errorCode, string description, Exception nestedException)
: base(MakeMessage(errorCode, description), nestedException)
{
this.ErrorCode = errorCode;
this.Description = description;
}
private static string MakeMessage(string errorCode, string description)
=> $"Удаленная система вернула ошибку: [{errorCode}] {description}";
public static HcsRemoteException CreateNew(string errorCode, string description, Exception nested = null)
{
if (string.Compare(errorCode, KnownCodes.НетОбъектовДляЭкспорта) == 0)
return new HcsNoResultsRemoteException(errorCode, description, nested);
return new HcsRemoteException(errorCode, description);
}
public static HcsRemoteException CreateNew(HcsRemoteException nested)
{
if (nested == null) throw new ArgumentNullException("nested exception");
return CreateNew(nested.ErrorCode, nested.Description, nested);
}
/// <summary>
/// Возвращает true если ошибка @e или ее вложенные ошибки модержат @errorCode
/// </summary>
public static bool ContainsErrorCode(Exception e, string errorCode)
{
if (e == null) return false;
return HcsUtil.EnumerateInnerExceptions(e).OfType<HcsRemoteException>().Where(x => x.ErrorCode == errorCode).Any();
}
}
}