﻿using System;
using System.ServiceModel;
using System.Runtime.Serialization;

namespace Talos.Common
{
    [DataContract]
    public class TalosServiceFault
    {
        public static FaultException<TalosServiceFault> FaultException(Exception e)
        {
            return new FaultException<TalosServiceFault>(new TalosServiceFault(e.Message, e));
        }

        public static FaultException<TalosServiceFault> FaultException(Exception e, string format, params object[] args)
        {
            return new FaultException<TalosServiceFault>(new TalosServiceFault(string.Format(format, args), e));
        }

        public static FaultException<TalosServiceFault> FaultException(string format, params object[] args)
        {
            return new FaultException<TalosServiceFault>(new TalosServiceFault(string.Format(format, args)));
        }

        public TalosServiceFault(string message, Exception innerException = null)
        {
            Message = message;
            if (innerException != null)
                Message += " " + innerException.Message;

            #if DEBUG
            StackTrace = innerException?.StackTrace; // unsecure, but OK for debug
            #else
            StackTrace = null;
            #endif

            Log.Error(Message + Environment.NewLine + StackTrace);
        }

        [DataMember]
        public string Message { get; private set; }

        [DataMember]
        public string StackTrace { get; private set; }
    }
}

