using System;

namespace Quadarax.Foundation.Core.Exceptions
{
    public static class ExceptionExc
    {
        public static int GetCode(this Exception e)
        {
            if (!e.Data.Contains("Code"))
                return -1;

            return int.Parse(e.Data["Code"]?.ToString() ?? "-1");
        }

        public static void SetCode(this Exception e, int code)
        {
            e.Data.Add("Code", code);
        }

        public static string Dump(this Exception e)
        {
            var level = 0;
            var sb = new System.Text.StringBuilder();
            sb.Append("[#").Append(level).Append("]:").AppendLine(e.Message);
            var inner = e.InnerException;
            while (inner != null)
            {
                sb.Append("[#").Append(level).Append("]:").AppendLine(inner.Message);
                inner = inner.InnerException;
            }

            if (e.StackTrace != null)
            {
                sb.AppendLine("[#0] Stack list:");
                sb.AppendLine(e.StackTrace);
            }
            return sb.ToString();
        }
    }
}
