コード例 #1
0
        public void SharePointRestErrorToStringDeserialization()
        {
            bool sharePointRestServiceExceptionThrown = false;
            SharePointRestError error = null;

            Dictionary <string, string> headers = new Dictionary <string, string>
            {
                { PnPConstants.SPRequestGuidHeader, "E91534F0-D3B4-4062-B872-41301FC1EC86" },
                { PnPConstants.XSPServerStateHeader, "0" },
                { PnPConstants.XSharePointHealthScoreHeader, "2" },
                { PnPConstants.SPClientServiceRequestDurationHeader, "13" }
            };

            try
            {
                SharePointRestServiceException restException = new SharePointRestServiceException(ErrorType.SharePointRestServiceError, 400, sampleRestSimpleError, headers);
                restException.Error.AdditionalData.Add("Extra1", "extra error data");
                restException.Error.AdditionalData.Add("Extra2", true);

                throw restException;
            }
            catch (ServiceException ex)
            {
                if (ex is SharePointRestServiceException)
                {
                    error = ex.Error as SharePointRestError;
                    sharePointRestServiceExceptionThrown = true;
                }
            }

            Assert.IsTrue(error.ClientRequestId == "E91534F0-D3B4-4062-B872-41301FC1EC86");

            var restErrorString = error.ToString();

            Assert.IsTrue(restErrorString.Contains("HttpResponseCode:"));
            Assert.IsTrue(restErrorString.Contains("Code:"));
            Assert.IsTrue(restErrorString.Contains("Message:"));
            Assert.IsTrue(restErrorString.Contains("ClientRequestId:"));
            Assert.IsTrue(restErrorString.Contains("Extra1: extra error data"));
            Assert.IsTrue(restErrorString.Contains("Extra2: True"));
            Assert.IsTrue(restErrorString.Contains("SPClientServiceRequestDuration: 13"));
            Assert.IsTrue(restErrorString.Contains("X-SharePointHealthScore: 2"));
            Assert.IsTrue(restErrorString.Contains("X-SP-SERVERSTATE: 0"));

            Assert.IsTrue(sharePointRestServiceExceptionThrown);
            Assert.IsTrue(string.IsNullOrEmpty(error.Code));
            Assert.IsTrue(!string.IsNullOrEmpty(error.Message));
            Assert.IsTrue(error.Type == ErrorType.SharePointRestServiceError);
        }
コード例 #2
0
ファイル: ExceptionTests.cs プロジェクト: teepbee/pnpcore
        public async Task ThrowSharePointRestServiceException()
        {
            var listTitle = "Fail list";

            //TestCommon.Instance.Mocking = false;
            using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite))
            {
                try
                {
                    bool SharePointRestServiceExceptionThrown = false;
                    SharePointRestError error = null;
                    try
                    {
                        // try adding the same list twice, will always result in an error
                        await context.Web.Lists.AddAsync(listTitle, ListTemplateType.GenericList);

                        await context.Web.Lists.AddAsync(listTitle, ListTemplateType.GenericList);
                    }
                    catch (ServiceException ex)
                    {
                        if (ex is SharePointRestServiceException)
                        {
                            error = ex.Error as SharePointRestError;
                            SharePointRestServiceExceptionThrown = true;
                        }
                    }

                    Assert.IsTrue(SharePointRestServiceExceptionThrown);
                    Assert.IsTrue(!string.IsNullOrEmpty(error.Code));
                    Assert.IsTrue(!string.IsNullOrEmpty(error.Message));
                    Assert.IsTrue(error.Type == ErrorType.SharePointRestServiceError);
                }
                finally
                {
                    // Clean up
                    var listToDelete = context.Web.Lists.FirstOrDefault(l => l.Title == listTitle);
                    if (listToDelete != null)
                    {
                        await listToDelete.DeleteAsync();
                    }
                }
            }
        }
コード例 #3
0
ファイル: ExceptionTests.cs プロジェクト: teepbee/pnpcore
        public void SharePointRestErrorToStringDeserialization()
        {
            bool sharePointRestServiceExceptionThrown = false;
            SharePointRestError error = null;

            try
            {
                SharePointRestServiceException restException = new SharePointRestServiceException(ErrorType.SharePointRestServiceError, 400, sampleRestSimpleError);
                restException.Error.AdditionalData = new Dictionary <string, object>
                {
                    { "Extra1", "extra error data" },
                    { "Extra2", true }
                };

                throw restException;
            }
            catch (ServiceException ex)
            {
                if (ex is SharePointRestServiceException)
                {
                    error = ex.Error as SharePointRestError;
                    sharePointRestServiceExceptionThrown = true;
                }
            }

            var restErrorString = error.ToString();

            Assert.IsTrue(restErrorString.Contains("HttpResponseCode:"));
            Assert.IsTrue(restErrorString.Contains("Code:"));
            Assert.IsTrue(restErrorString.Contains("Message:"));
            Assert.IsTrue(restErrorString.Contains("ClientRequestId:"));
            Assert.IsTrue(restErrorString.Contains("Extra1: extra error data"));
            Assert.IsTrue(restErrorString.Contains("Extra2: True"));

            Assert.IsTrue(sharePointRestServiceExceptionThrown);
            Assert.IsTrue(string.IsNullOrEmpty(error.Code));
            Assert.IsTrue(!string.IsNullOrEmpty(error.Message));
            Assert.IsTrue(error.Type == ErrorType.SharePointRestServiceError);
        }
コード例 #4
0
        public void SharePointRestBadErrorDeserialization()
        {
            bool sharePointRestServiceExceptionThrown = false;
            SharePointRestError error = null;

            try
            {
                throw new SharePointRestServiceException(ErrorType.SharePointRestServiceError, 400, sampleRestErrorBad);
            }
            catch (ServiceException ex)
            {
                if (ex is SharePointRestServiceException)
                {
                    error = ex.Error as SharePointRestError;
                    sharePointRestServiceExceptionThrown = true;
                }
            }

            Assert.IsTrue(sharePointRestServiceExceptionThrown);
            Assert.IsTrue(!string.IsNullOrEmpty(error.Message));
            Assert.IsTrue(error.Message == sampleRestErrorBad);
        }