Example #1
0
        public static bool TryParseErrors(string text, IEnumerable <HwiOption> options, 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);
                }
            }

            // Help text has error in it, so if help command is requested, then don't throw the error.
            // https://github.com/bitcoin-core/HWI/issues/252
            if (error != null &&
                options != null &&
                options.Contains(HwiOption.Help) &&
                error.ErrorCode == HwiErrorCode.HelpText)
            {
                error = null;
            }

            return(error != null);
        }
Example #2
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);
        }
 public static bool TryParseErrors(string text, IEnumerable <HwiOption> options, 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[startIndex..];