using System;
using System.Linq;
namespace Hcs.ClientApi
{
///
/// Сообщение об ошибке возникшей на удаленном сервере ГИС ЖКХ
///
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);
}
///
/// Возвращает true если ошибка @e или ее вложенные ошибки модержат @errorCode
///
public static bool ContainsErrorCode(Exception e, string errorCode)
{
if (e == null) return false;
return HcsUtil.EnumerateInnerExceptions(e).OfType().Where(x => x.ErrorCode == errorCode).Any();
}
}
}