コード例 #1
0
        public void MsalUiRequiredException()
        {
            // Arrange
            string innerExMsg     = "innerExMsg";
            var    innerException = new NotImplementedException(innerExMsg);

            // Act
            var msalException = MsalExceptionFactory.GetUiRequiredException(ExCode, ExMessage, innerException, null);

            // Assert
            var msalServiceException = msalException as MsalUiRequiredException;

            Assert.AreEqual(innerException, msalServiceException.InnerException);
            Assert.AreEqual(ExCode, msalServiceException.ErrorCode);
            Assert.IsNull(msalServiceException.Claims);
            Assert.IsNull(msalServiceException.ResponseBody);
            Assert.AreEqual(ExMessage, msalServiceException.Message);
            Assert.AreEqual(0, msalServiceException.StatusCode);

            // Act
            string piiMessage = MsalExceptionFactory.GetPiiScrubbedDetails(msalException);

            // Assert
            Assert.IsFalse(string.IsNullOrEmpty(piiMessage));
            Assert.IsTrue(
                piiMessage.Contains(typeof(MsalUiRequiredException).Name),
                "The pii message should contain the exception type");
            Assert.IsTrue(
                piiMessage.Contains(typeof(NotImplementedException).Name),
                "The pii message should have the inner exception type");
            Assert.IsTrue(piiMessage.Contains(ExCode));
            Assert.IsFalse(piiMessage.Contains(ExMessage));
            Assert.IsFalse(piiMessage.Contains(innerExMsg));
        }
コード例 #2
0
        public static void CreateErrorResponse(HttpResponse response, RequestContext requestContext)
        {
            bool shouldLogAsError = true;

            Exception serviceEx;

            try
            {
                var msalTokenResponse = JsonHelper.DeserializeFromJson <MsalTokenResponse>(response.Body);

                if (CoreErrorCodes.InvalidGrantError.Equals(msalTokenResponse.Error, StringComparison.OrdinalIgnoreCase))
                {
                    throw MsalExceptionFactory.GetUiRequiredException(
                              CoreErrorCodes.InvalidGrantError,
                              msalTokenResponse.ErrorDescription,
                              null,
                              ExceptionDetail.FromHttpResponse(response));
                }

                serviceEx = MsalExceptionFactory.GetServiceException(
                    msalTokenResponse.Error,
                    msalTokenResponse.ErrorDescription,
                    response);

                // For device code flow, AuthorizationPending can occur a lot while waiting
                // for the user to auth via browser and this causes a lot of error noise in the logs.
                // So suppress this particular case to an Info so we still see the data but don't
                // log it as an error since it's expected behavior while waiting for the user.
                if (string.Compare(msalTokenResponse.Error, OAuth2Error.AuthorizationPending,
                                   StringComparison.OrdinalIgnoreCase) == 0)
                {
                    shouldLogAsError = false;
                }
            }
            catch (SerializationException ex)
            {
                serviceEx = MsalExceptionFactory.GetClientException(CoreErrorCodes.UnknownError, response.Body, ex);
            }

            if (shouldLogAsError)
            {
                requestContext.Logger.ErrorPii(serviceEx);
            }
            else
            {
                requestContext.Logger.InfoPii(serviceEx);
            }

            throw serviceEx;
        }
コード例 #3
0
        private static Exception ExtractErrorsFromTheResponse(HttpResponse response, ref bool shouldLogAsError)
        {
            Exception exceptionToThrow = null;

            // In cases where the end-point is not found (404) response.body will be empty.
            if (string.IsNullOrWhiteSpace(response.Body))
            {
                return(null);
            }

            var msalTokenResponse = JsonHelper.DeserializeFromJson <MsalTokenResponse>(response.Body);

            if (msalTokenResponse?.Error == null)
            {
                return(null);
            }

            if (CoreErrorCodes.InvalidGrantError.Equals(msalTokenResponse.Error, StringComparison.OrdinalIgnoreCase))
            {
                exceptionToThrow = MsalExceptionFactory.GetUiRequiredException(
                    CoreErrorCodes.InvalidGrantError,
                    msalTokenResponse.ErrorDescription,
                    null,
                    ExceptionDetail.FromHttpResponse(response));
            }
            else
            {
                exceptionToThrow = MsalExceptionFactory.GetServiceException(
                    msalTokenResponse.Error,
                    msalTokenResponse.ErrorDescription,
                    response);
            }

            // For device code flow, AuthorizationPending can occur a lot while waiting
            // for the user to auth via browser and this causes a lot of error noise in the logs.
            // So suppress this particular case to an Info so we still see the data but don't
            // log it as an error since it's expected behavior while waiting for the user.
            if (string.Compare(msalTokenResponse.Error, OAuth2Error.AuthorizationPending,
                               StringComparison.OrdinalIgnoreCase) == 0)
            {
                shouldLogAsError = false;
            }

            return(exceptionToThrow);
        }