Пример #1
0
        public static bool TryParseError(JToken token, out HwiException error)
        {
            error = null;
            if (token is JArray)
            {
                return(false);
            }

            var errToken     = token["error"];
            var codeToken    = token["code"];
            var successToken = token["success"];

            string err = "";

            if (errToken != null)
            {
                err = Guard.Correct(errToken.Value <string>());
            }

            HwiErrorCode?code = null;

            if (TryParseErrorCode(codeToken, out HwiErrorCode c))
            {
                code = c;
            }
            // HWI bug: it does not give error code.
            // https://github.com/bitcoin-core/HWI/issues/216
            else if (err == "Not initialized")
            {
                code = HwiErrorCode.DeviceNotInitialized;
            }

            if (code.HasValue)
            {
                error = new HwiException(code.Value, err);
            }
            else if (err.Length != 0)
            {
                error = new HwiException(HwiErrorCode.UnknownError, err);
            }
            else if (successToken != null && successToken.Value <bool>() == false)
            {
                error = new HwiException(HwiErrorCode.UnknownError, "");
            }

            return(error != null);
        }
Пример #2
0
 public static bool TryParseErrors(string text, out HwiException error)
 {
     error = null;
     if (JsonHelpers.TryParseJToken(text, out JToken token) && TryParseError(token, out HwiException e))
     {
         error = e;
     }
     else
     {
         var subString = "error:";
         if (text.Contains(subString, StringComparison.OrdinalIgnoreCase))
         {
             int startIndex = text.IndexOf(subString, StringComparison.OrdinalIgnoreCase) + subString.Length;
             var err        = text.Substring(startIndex);
             error = new HwiException(HwiErrorCode.UnknownError, err);
         }
     }
     return(error != null);
 }