示例#1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PartnerException"/> class.
 /// </summary>
 /// <param name="info">The <see cref="SerializationInfo" /> that holds the serialized object data about the exception being thrown.</param>
 /// <param name="context">The <see cref="StreamingContext" /> that contains contextual information about the source or destination.</param>
 protected PartnerException(SerializationInfo info, StreamingContext context)
     : base(info, context)
 {
     errorCategory = (PartnerErrorCategory)info.GetInt32(nameof(ErrorCategory));
     requestContex = info.GetValue(PartnerContextFieldName, typeof(IRequestContext)) as IRequestContext;
     serviceError  = info.GetValue(ServiceErrorPayloadFieldName, typeof(ApiFault)) as ApiFault;
 }
        private FaultException GetFault(Exception e)
        {
            if (e == null)
            {
                throw new ArgumentNullException(nameof(e));
            }
            if (e is PreConditionNotFulfilledException)
            {
                return(PreConditionNotFulfilledFault.GetFault(e));
            }
            if (e is InvalidStateTransitionException)
            {
                return(InvalidStateTransitionFault.GetFault(e));
            }
            if (e is COMException)
            {
                return(ApiFault.GetFault(e as COMException));
            }
            if (e is UpdateNotFoundException)
            {
                return(UpdateNotFoundFault.GetFault(e, ((UpdateNotFoundException)e).UpdateId));
            }
            //if (e is ArgumentOutOfRangeException) return BadArgumentFault.GetFault(e as ArgumentOutOfRangeException);
            if (e is ArgumentException)
            {
                return(BadArgumentFault.GetFault(e as ArgumentException));
            }

            // Other exceptions should never happen. Do not wrap them in to faults.
            Log.Error("An unexpected expection was thrown.", e);
            ExceptionDispatchInfo.Capture(e).Throw(); // Keep the stack trace and rethrow
            throw new NotImplementedException($"Avoid compiler error. Should never be reached, because {nameof(ExceptionDispatchInfo)} rethrows the exception. But the compiler does not know that.");
        }
 public void OutputApiFault(ApiFault dataObject)
 {
     if (null != dataObject)
     {
         OutputArrayOfOperationError(dataObject.OperationErrors);
     }
 }
示例#4
0
        private async Task <TResource> HandleResponseAsync <TResource>(HttpResponseMessage response, JsonConverter converter = null)
        {
            string content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

            if (response.IsSuccessStatusCode)
            {
                if (string.IsNullOrEmpty(content))
                {
                    content = string.Empty;
                }

                return(JsonConvert.DeserializeObject <TResource>(content, GetSerializationSettings(converter)));
            }

            PartnerErrorCategory errorCategory = GetErrorCategory(response.StatusCode);

            if (string.IsNullOrEmpty(content))
            {
                throw new PartnerException(
                          response.ReasonPhrase,
                          rootPartnerOperations.RequestContext,
                          errorCategory);
            }

            ApiFault fault = JsonConvert.DeserializeObject <ApiFault>(content, GetSerializationSettings(converter));

            throw new PartnerException(
                      fault,
                      rootPartnerOperations.RequestContext,
                      errorCategory);
        }
        public void Should_ContainHResult_When_CreateApiFault()
        {
            string message = "UnitTest";
            int    code    = 5555;
            var    comex   = new COMException(message, code);
            var    fault   = new ApiFault(comex);

            Assert.AreEqual(fault.HResult, code);
        }
示例#6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PartnerException" /> class.
 /// </summary>
 /// <param name="apiFault">The API fault object returned by the partner service.</param>
 /// <param name="context">The context for the request.</param>
 /// <param name="errorCategory">The category for the error.</param>
 /// <param name="innerException">
 /// The exception that is the cause of the current exception, or a null reference
 /// (Nothing in Visual Basic) if no inner exception is specified.
 /// </param>
 public PartnerException(
     ApiFault apiFault,
     IRequestContext context,
     PartnerErrorCategory errorCategory = PartnerErrorCategory.NotSpecified,
     Exception innerException           = null)
     : this(apiFault != null ? apiFault.ErrorMessage : string.Empty, context, errorCategory, innerException)
 {
     serviceError = apiFault;
 }
 public void OutputApiFault(ApiFault dataObject)
 {
     if (null != dataObject)
     {
         OutputArrayOfOperationError(dataObject.OperationErrors);
         var apibatchfault = dataObject as ApiBatchFault;
         if (apibatchfault != null)
         {
             OutputApiBatchFault((ApiBatchFault)dataObject);
         }
     }
 }
 public void OutputApiFault(ApiFault dataObject)
 {
     if (null != dataObject)
     {
         OutputStatusMessage("* * * Begin OutputApiFault * * *");
         OutputStatusMessage("OperationErrors:");
         OutputArrayOfOperationError(dataObject.OperationErrors);
         var apibatchfault = dataObject as ApiBatchFault;
         if (null != apibatchfault)
         {
             OutputApiBatchFault((ApiBatchFault)dataObject);
         }
         OutputStatusMessage("* * * End OutputApiFault * * *");
     }
 }
示例#9
0
        private async Task HandleResponseAsync(HttpResponseMessage response)
        {
            string content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

            if (response.IsSuccessStatusCode)
            {
                return;
            }

            ApiFault             fault         = JsonConvert.DeserializeObject <ApiFault>(content, GetSerializationSettings());
            PartnerErrorCategory errorCategory = GetErrorCategory(response.StatusCode);

            throw new PartnerException(
                      fault,
                      rootPartnerOperations.RequestContext,
                      errorCategory);
        }
        public void Should_ContainExpectedFaultCode_When_CreateFault()
        {
            Exception         ex  = new Exception();
            ArgumentException aex = new ArgumentException();
            var comex             = new COMException();

            var fault1 = InvalidStateTransitionFault.GetFault(ex);
            var fault2 = PreConditionNotFulfilledFault.GetFault(ex);
            var fault3 = BadArgumentFault.GetFault(aex);
            var fault4 = ApiFault.GetFault(comex);
            var fault5 = UpdateNotFoundFault.GetFault(ex, "update1");

            Assert.AreEqual(fault1.Code.Name, "InvalidTransition");
            Assert.AreEqual(fault2.Code.Name, "PreConditionNotFulfilled");
            Assert.AreEqual(fault3.Code.Name, "BadArgument");
            Assert.AreEqual(fault4.Code.Name, "ApiFault");
            Assert.AreEqual(fault5.Code.Name, "UpdateNotFound");
        }
示例#11
0
        private async Task HandleResponseAsync(HttpRequestMessage request, CancellationToken cancellationToken = default)
        {
            HttpResponseMessage response = await HttpClient.SendAsync(request, cancellationToken).ConfigureAwait(false);

            string responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

            if (response.IsSuccessStatusCode)
            {
                return;
            }

            ApiFault             fault         = JsonConvert.DeserializeObject <ApiFault>(responseContent, GetSerializationSettings());
            PartnerErrorCategory errorCategory = GetErrorCategory(response.StatusCode);

            throw new PartnerException(fault, requestContext, errorCategory)
                  {
                      Request  = new HttpRequestMessageWrapper(request, null),
                      Response = new HttpResponseMessageWrapper(response, responseContent)
                  };
        }
示例#12
0
        private async Task <TResource> HandleResponseAsync <TResource>(HttpRequestMessage request, JsonConverter converter = null, CancellationToken cancellationToken = default)
        {
            HttpResponseMessage response = await HttpClient.SendAsync(request, cancellationToken).ConfigureAwait(false);

            string responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

            if (response.IsSuccessStatusCode)
            {
                if (typeof(TResource) == typeof(HttpResponseMessage))
                {
                    return((TResource)Convert.ChangeType(response, typeof(TResource)));
                }

                if (string.IsNullOrEmpty(responseContent))
                {
                    responseContent = string.Empty;
                }

                return(JsonConvert.DeserializeObject <TResource>(responseContent, GetSerializationSettings(converter)));
            }

            PartnerErrorCategory errorCategory = GetErrorCategory(response.StatusCode);

            if (string.IsNullOrEmpty(responseContent))
            {
                throw new PartnerException(response.ReasonPhrase, requestContext, errorCategory)
                      {
                          Request  = new HttpRequestMessageWrapper(request, null),
                          Response = new HttpResponseMessageWrapper(response, responseContent)
                      };
            }

            ApiFault fault = JsonConvert.DeserializeObject <ApiFault>(responseContent, GetSerializationSettings(converter));

            throw new PartnerException(fault, requestContext, errorCategory)
                  {
                      Request  = new HttpRequestMessageWrapper(request, null),
                      Response = new HttpResponseMessageWrapper(response, responseContent)
                  };
        }
        public void Should_ContainExceptionMessage_When_CreateFault()
        {
            string            message = "UnitTest";
            Exception         ex      = new Exception(message);
            ArgumentException aex     = new ArgumentException(message);
            var comex = new COMException(message);

            var fault1 = InvalidStateTransitionFault.GetFault(ex);
            var fault2 = PreConditionNotFulfilledFault.GetFault(ex);
            var fault3 = BadArgumentFault.GetFault(aex);
            var fault4 = ApiFault.GetFault(comex);
            var fault5 = UpdateNotFoundFault.GetFault(ex, "update1");

            Assert.AreEqual(fault1.Message, message);
            Assert.AreEqual(fault2.Message, message);
            Assert.AreEqual(fault3.Message, message);
            Assert.AreEqual(fault4.Message, message);
            Assert.AreEqual(fault5.Message, message);

            var fault9 = new TestFault(new Exception(message));

            Assert.AreEqual(fault9.Message, message);
        }