help_text.cpp 10.1 KB
Newer Older
A
Andrianto Lie 已提交
1 2 3
/**
 *  @file
 *  @copyright defined in eos/LICENSE.txt
4 5
 */
#include "help_text.hpp"
6
#include "localize.hpp"
7 8
#include <regex>

P
Pravin 已提交
9
using namespace eosio::client::localize;
10

11 12 13
const char* transaction_help_text_header = _("An error occurred while submitting the transaction for this command!");

const char* duplicate_transaction_help_text = _(R"text(The transaction is a duplicate of one already pushed to the producers.  If this
14 15 16 17 18 19
is an intentionally repeated transaction there are a few ways to resolve the
issue:
  - wait for the next block
  - combine duplicate transactions into a single transaction
  - adjust the expiration time using the `--expiration <milliseconds>` option
  - use the `--force-unique` option to add additional nonce data
20
    Please note, this will consume more bandwidth than the base transaction )text");
21

22
const char* missing_perms_help_text = _(R"text(The transaction requires permissions that were not granted by the transaction.
23 24 25 26
Missing permission from:
  - ${1}

Please use the `-p,--permissions` option to add the missing accounts!
27
Note: you will need an unlocked wallet that can authorized these permissions.)text");
28

29
const char* missing_sigs_help_text = _(R"text(The transaction requires permissions that could not be authorized by the wallet.
30 31 32
Missing authrizations:
  - ${1}@${2}

33
Please make sure the proper keys are imported into an unlocked wallet and try again!)text");
34

35
const char* missing_scope_help_text = _(R"text(The transaction requires scopes that were not listed by the transaction.
36 37 38
Missing scope(s):
  - ${1}

39
Please use the `-S,--scope` option to add the missing accounts!)text");
40 41


42
const char* tx_unknown_account_help_text = _("The transaction references an account which does not exist.");
43

44
const char* unknown_account_help_text = _(R"text(Unknown accounts:
45 46
  - ${1}

47
Please check the account names and try again!)text");
48

49
const char* missing_abi_help_text = _(R"text(The ABI for action "${2}" on code account "${1}" is unknown.
50 51
The payload cannot be automatically serialized.

52
You can push an arbitrary transaction using the 'push action' subcommand)text");
53

54
const char* unknown_wallet_help_text = _("Unable to find a wallet named \"${1}\", are you sure you typed the name correctly?");
55

56
const char* bad_wallet_password_help_text = _("Invalid password for wallet named \"${1}\"");
57

58
const char* locked_wallet_help_text = _("The wallet named \"${1}\" is locked.  Please unlock it and try again.");
59

60
const char* duplicate_key_import_help_text = _("This key is already imported into the wallet named \"${1}\".");
61

62
const char* unknown_abi_table_help_text = _(R"text(The ABI for the code on account "${1}" does not specify table "${2}".
63 64

Please check the account and table name, and verify that the account has the expected code using:
M
Matt Witherspoon 已提交
65
  eosioc get code ${1})text");
66 67

const char* help_regex_error = _("Error locating help text: ${code} ${what}");
68

69 70
const std::vector<std::pair<const char*, std::vector<const char *>>> error_help_text {
   {"Error\n: 3030011", {transaction_help_text_header, duplicate_transaction_help_text}},
71
   {"Error\n: 3030001[^\\x00]*\\{\"acct\":\"([^\"]*)\"\\}", {transaction_help_text_header, missing_perms_help_text}},
72
   {"Error\n: 3030002[^\\x00]*Transaction declares authority.*account\":\"([^\"]*)\",\"permission\":\"([^\"]*)\"", {transaction_help_text_header, missing_sigs_help_text}},
73 74
   {"Error\n: 3030008[^\\x00]*\\{\"scope\":\"([^\"]*)\"\\}", {transaction_help_text_header, missing_scope_help_text}},
   {"Account not found: ([\\S]*)", {transaction_help_text_header, tx_unknown_account_help_text, unknown_account_help_text}},
75 76
   {"Error\n: 303", {transaction_help_text_header}},
   {"unknown key[^\\x00]*abi_json_to_bin.*code\":\"([^\"]*)\".*action\":\"([^\"]*)\"", {missing_abi_help_text}},
77
   {"unknown key[^\\x00]*chain/get_code.*name\":\"([^\"]*)\"", {unknown_account_help_text}},
78 79 80
   {"Unable to open file[^\\x00]*wallet/open.*postdata\":\"([^\"]*)\"", {unknown_wallet_help_text}},
   {"AES error[^\\x00]*wallet/unlock.*postdata\":\\[\"([^\"]*)\"", {bad_wallet_password_help_text}},
   {"Wallet is locked: ([\\S]*)", {locked_wallet_help_text}},
81
   {"Key already in wallet[^\\x00]*wallet/import_key.*postdata\":\\[\"([^\"]*)\"", {duplicate_key_import_help_text}},
82
   {"ABI does not define table[^\\x00]*get_table_rows.*code\":\"([^\"]*)\",\"table\":\"([^\"]*)\"", {unknown_abi_table_help_text}}
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
};

auto smatch_to_variant(const std::smatch& smatch) {
   auto result = fc::mutable_variant_object();
   for(size_t index = 0; index < smatch.size(); index++) {
      auto name = boost::lexical_cast<std::string>(index);
      if (smatch[index].matched) {
         result = result(name, smatch.str(index));
      } else {
         result = result(name, "");
      }
   }

   return result;
};

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
const char* error_advice_3120001 = R"=====(Name should be less than 13 characters and only contains the following symbol .12345abcdefghijklmnopqrstuvwxyz)=====";

const char* error_advice_3120002 = R"=====(Public key should be encoded in base58 and starts with EOS prefix)=====";

const char* error_advice_3120003 = R"=====(Ensure that your authority JSON follows the following format!
{
  "threshold":"uint32_t",
  "keys":[{ "key":"public_key", "weight":"uint16_t" }],
  "accounts":[{
    "permission":{ "actor":"account_name", "permission":"permission_name" },
    "weight":"uint16_t"
  }]
}
e.g.
{
  "threshold":"1",
  "keys":[{ "key":"EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV", "weight":"1" }],
  "accounts":[{
    "permission":{ "actor":"initb", "permission":"social" },
    "weight":"1
  }]
})=====";

const char* error_advice_3120004 = R"=====(Ensure that your action JSON follows the contract's abi!)=====";

const char* error_advice_3120005 = R"=====(Ensure that your transaction JSON follows the following format!\n"
{
  "ref_block_num":"uint16_t",
  "ref_block_prefix":"uint32_t",
  "expiration":"YYYY-MM-DDThh:mm",
  "region": "uint16_t",
  "read_scope":[ "account_name" ],
  "write_scope":[ "account_name" ],
  "actions":[{ 
    "account":"account_name",
    "name":"action_name",
    "authorization":[{ "actor":"account_name","permission":"permission_name" }],
    "data":"bytes"
  }]
}"
e.g.
{
  "ref_block_num":"1000",
  "ref_block_prefix":"3463702842",
  "expiration":"2018-01-23T01:51:05",
  "region": "0",
  "read_scope":[ "initb", "initc" ],
  "write_scope":[ "initb", "initc" ],
  "actions":[{ 
    "account":"eosio",
    "name":"transfer",
    "authorization":[{ "actor":"initb","permission":"active" }],
    "data":"000000008093dd74000000000094dd74e80300000000000000"
  }]
})=====";

const char* error_advice_3120006 =  R"=====(Ensure that your abi JSON follows the following format!
{
  "types" : [{ "new_type_name":"type_name", "type":"type_name" }],
  "structs" : [{ "name":"type_name", "base":"type_name", "fields": [{ "name":"field_name", "type": "type_name" }] }],
  "actions" : [{ "name":"action_name","type":"type_name"}],
  "tables" : [{
    "name":"table_name",
    "index_type":"type_name",
    "key_names":[ "field_name" ],
    "key_types":[ "type_name" ],
    "type":"type_name" "
  }]
}
e.g.
{
  "types" : [{ "new_type_name":"account_name", "type":"name" }],
  "structs" : [
    { "name":"foo", "base":"", "fields": [{ "name":"by", "type": "account_name" }] },\n "
    { "name":"foobar", "base":"", "fields": [{ "name":"by", "type": "account_name" }] }
  ],
  "actions" : [{ "name":"foo","type":"foo"}],
  "tables" : [{
    "name":"foobar_table",
    "index_type":"i64",
    "key_names":[ "by" ],
    "key_types":[ "account_name" ],
    "type":"foobar" "
  }]
})=====";

185
const std::map<int64_t, std::string> error_advice = {
186 187 188 189 190 191
   { 3120001, error_advice_3120001 },
   { 3120002, error_advice_3120002 },
   { 3120003, error_advice_3120003 },
   { 3120004, error_advice_3120004 },
   { 3120005, error_advice_3120005 },
   { 3120006, error_advice_3120006 }
192 193 194
};


P
Pravin 已提交
195
namespace eosio { namespace client { namespace help {
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
bool print_recognized_error_code(const fc::exception& e) {
   // eos recognized error code is from 3000000 to 3999999
   // refer to libraries/chain/include/eosio/chain/exceptions.hpp
   if (e.code() >= 3000000 && e.code() <= 3999999) {
      std::string advice, explanation;

      // Get advice, if any
      const auto advice_itr = error_advice.find(e.code());
      if (advice_itr != error_advice.end()) advice = advice_itr->second;

      // Get explanation from log, if any
      for (auto &log : e.get_log()) {
         // Check if there's a log to display
         if (!log.get_format().empty()) {
            // Localize the message as needed
            explanation += "\n  " + localized_with_variant(log.get_format().data(), log.get_data());
         }
      }
      if (!explanation.empty()) explanation = std::string("Error Details:") + explanation;

      std::cerr << "\033[31m" << "Error " << e.code() << ": " << e.what() << "\033[0m";
      if (!advice.empty()) std::cerr << "\n" << "\033[32m" << advice << "\033[0m";
      if (!explanation.empty()) std::cerr  << "\n" << "\033[33m" << explanation << "\033[0m" << std::endl;
      return true;
   }
   return false;
}
223 224

bool print_help_text(const fc::exception& e) {
225 226
   // Check if the exception has recognized error code
   if (print_recognized_error_code(e)) return true;
227
   bool result = false;
228 229
   // Large input strings to std::regex can cause SIGSEGV, this is a known bug in libstdc++.
   // See https://stackoverflow.com/questions/36304204/%D0%A1-regex-segfault-on-long-sequences
230
   auto detail_str = e.to_detail_string();
231 232
   // 2048 nice round number. Picked for no particular reason. Bug above was reported for 22K+ strings.
   if (detail_str.size() > 2048) return result;
233 234 235 236 237 238 239
   try {
      for (const auto& candidate : error_help_text) {
         auto expr = std::regex {candidate.first};
         std::smatch matches;
         if (std::regex_search(detail_str, matches, expr)) {
            auto args = smatch_to_variant(matches);
            for (const auto& msg: candidate.second) {
240
               std::cerr << localized_with_variant(msg, args) << std::endl;
241 242 243 244 245 246
            }               
            result = true;
            break;
         }
      }
   } catch (const std::regex_error& e ) {
247
      std::cerr << localized(help_regex_error, ("code", e.code())("what", e.what())) << std::endl;
248 249 250 251 252 253
   }

   return result;   
}

}}}