Exemple #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="body"></param>
        /// <param name="err"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        protected override bool TryExtractErrorFromBody <T>(T body, out FailBase err)
        {
            err = null;

            if (!(body is XDocument xBody))
            {
                return(false);
            }

            bool ExtractFromElement(out string errCode, out string errMsg)
            {
                errCode = xBody.XPathSelectElement("Exception/ErrorCode")?.Value ?? string.Empty;
                errMsg  = xBody.XPathSelectElement("Exception/ErrorMessage")?.Value ?? string.Empty;

                return(!string.IsNullOrEmpty(errCode + errMsg));
            }

            bool ExtractFromAttribute(out string errCode, out string errMsg)
            {
                var xData = xBody.Element("Data");

                errCode = xData?.Attribute("ErrorCode")?.Value ?? string.Empty;
                errMsg  = xData?.Attribute("ErrorText")?.Value ?? string.Empty;

                return(!string.IsNullOrEmpty(errCode + errMsg));
            }

            if (ExtractFromElement(out var sCode, out var sMsg) || ExtractFromAttribute(out sCode, out sMsg))
            {
                err = FailBase.CreateInstance(sCode, sMsg);
                return(true);
            }

            return(false);
        }
        /// <inheritdoc/>
        protected override bool TryExtractErrorFromBody <T>(T body, out FailBase err)
        {
            err = null;

            if (!(body is JToken jBody))
            {
                return(false);
            }

            if (!jBody.HasValues || jBody.First == null)
            {
                return(false);
            }
            var msg  = jBody["error"]?.Value <string>();
            var code = jBody["code"]?.Value <string>();

            if (string.IsNullOrEmpty(msg))
            {
                return(false);
            }

            err = FailBase.CreateInstance(code, msg);
            return(true);
        }