Skip to content

Commit

Permalink
GH-591 Change from 500 to 400 http error code for exceptions during p…
Browse files Browse the repository at this point in the history
…rocessing of http requests
  • Loading branch information
heifner committed Mar 27, 2023
1 parent f07e688 commit 6d9dff6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 29 deletions.
2 changes: 2 additions & 0 deletions plugins/chain_plugin/chain_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2374,6 +2374,7 @@ read_only::get_raw_abi_results read_only::get_raw_abi( const get_raw_abi_params&
}

read_only::get_account_results read_only::get_account( const get_account_params& params, const fc::time_point& deadline )const {
try {
get_account_results result;
result.account_name = params.account_name;

Expand Down Expand Up @@ -2541,6 +2542,7 @@ read_only::get_account_results read_only::get_account( const get_account_params&
}
}
return result;
} EOS_RETHROW_EXCEPTIONS(chain::account_query_exception, "unable to retrieve account info")
}

read_only::get_required_keys_result read_only::get_required_keys( const get_required_keys_params& params, const fc::time_point& deadline )const {
Expand Down
8 changes: 4 additions & 4 deletions plugins/http_plugin/http_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,13 +403,13 @@ namespace eosio {
fc_elog( logger(), "Unable to parse arguments to ${api}.${call}", ("api", api_name)( "call", call_name ) );
fc_dlog( logger(), "Bad arguments: ${args}", ("args", body) );
} catch (fc::exception& e) {
error_results results{500, "Internal Service Error", error_results::error_info(e, verbose_http_errors)};
cb( 500, fc::time_point::maximum(), fc::variant( results ));
error_results results{400, "Exception", error_results::error_info(e, verbose_http_errors)};
cb( 400, fc::time_point::maximum(), fc::variant( results ));
fc_dlog( logger(), "Exception while processing ${api}.${call}: ${e}",
("api", api_name)( "call", call_name )("e", e.to_detail_string()) );
} catch (std::exception& e) {
error_results results{500, "Internal Service Error", error_results::error_info(fc::exception( FC_LOG_MESSAGE( error, e.what())), verbose_http_errors)};
cb( 500, fc::time_point::maximum(), fc::variant( results ));
error_results results{400, "Exception", error_results::error_info(fc::exception( FC_LOG_MESSAGE( error, e.what())), verbose_http_errors)};
cb( 400, fc::time_point::maximum(), fc::variant( results ));
fc_dlog( logger(), "STD Exception encountered while processing ${api}.${call}: ${e}",
("api", api_name)("call", call_name)("e", e.what()) );
} catch (...) {
Expand Down
50 changes: 25 additions & 25 deletions tests/plugin_http_api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ def test_ChainApi(self) :
# get_account with valid parameter
payload = {"account_name":"default"}
ret_json = self.nodeos.processUrllibRequest(resource, command, payload)
self.assertEqual(ret_json["code"], 500)
self.assertEqual(ret_json["code"], 400)

# get_code with empty parameter
command = "get_code"
Expand All @@ -355,7 +355,7 @@ def test_ChainApi(self) :
# get_code with valid parameter
payload = {"account_name":"default"}
ret_json = self.nodeos.processUrllibRequest(resource, command, payload)
self.assertEqual(ret_json["code"], 500)
self.assertEqual(ret_json["code"], 400)

# get_code_hash with empty parameter
command = "get_code_hash"
Expand All @@ -373,7 +373,7 @@ def test_ChainApi(self) :
# get_code_hash with valid parameter
payload = {"account_name":"default"}
ret_json = self.nodeos.processUrllibRequest(resource, command, payload)
self.assertEqual(ret_json["code"], 500)
self.assertEqual(ret_json["code"], 400)

# get_abi with empty parameter
command = "get_abi"
Expand All @@ -391,7 +391,7 @@ def test_ChainApi(self) :
# get_abi with valid parameter
payload = {"account_name":"default"}
ret_json = self.nodeos.processUrllibRequest(resource, command, payload)
self.assertEqual(ret_json["code"], 500)
self.assertEqual(ret_json["code"], 400)

# get_raw_code_and_abi with empty parameter
command = "get_raw_code_and_abi"
Expand All @@ -409,7 +409,7 @@ def test_ChainApi(self) :
# get_raw_code_and_abi with valid parameter
payload = {"account_name":"default"}
ret_json = self.nodeos.processUrllibRequest(resource, command, payload)
self.assertEqual(ret_json["code"], 500)
self.assertEqual(ret_json["code"], 400)

# get_raw_abi with empty parameter
command = "get_raw_abi"
Expand All @@ -427,7 +427,7 @@ def test_ChainApi(self) :
# get_raw_abi with valid parameter
payload = {"account_name":"default"}
ret_json = self.nodeos.processUrllibRequest(resource, command, payload)
self.assertEqual(ret_json["code"], 500)
self.assertEqual(ret_json["code"], 400)

# get_table_rows with empty parameter
command = "get_table_rows"
Expand All @@ -452,7 +452,7 @@ def test_ChainApi(self) :
"lower_bound":"0x0000000000000000D0F2A472A8EB6A57",
"upper_bound":"0xFFFFFFFFFFFFFFFFD0F2A472A8EB6A57"}
ret_json = self.nodeos.processUrllibRequest(resource, command, payload)
self.assertEqual(ret_json["code"], 500)
self.assertEqual(ret_json["code"], 400)

# get_table_by_scope with empty parameter
command = "get_table_by_scope"
Expand All @@ -474,7 +474,7 @@ def test_ChainApi(self) :
"lower_bound":"0x0000000000000000D0F2A472A8EB6A57",
"upper_bound":"0xFFFFFFFFFFFFFFFFD0F2A472A8EB6A57"}
ret_json = self.nodeos.processUrllibRequest(resource, command, payload)
self.assertEqual(ret_json["code"], 500)
self.assertEqual(ret_json["code"], 400)

# get_currency_balance with empty parameter
command = "get_currency_balance"
Expand All @@ -492,7 +492,7 @@ def test_ChainApi(self) :
# get_currency_balance with valid parameter
payload = {"code":"eosio.token", "account":"unknown"}
ret_json = self.nodeos.processUrllibRequest(resource, command, payload)
self.assertEqual(ret_json["code"], 500)
self.assertEqual(ret_json["code"], 400)

# get_currency_stats with empty parameter
command = "get_currency_stats"
Expand All @@ -510,7 +510,7 @@ def test_ChainApi(self) :
# get_currency_stats with valid parameter
payload = {"code":"eosio.token","symbol":"SYS"}
ret_json = self.nodeos.processUrllibRequest(resource, command, payload)
self.assertEqual(ret_json["code"], 500)
self.assertEqual(ret_json["code"], 400)

# get_producers with empty parameter
command = "get_producers"
Expand Down Expand Up @@ -585,7 +585,7 @@ def test_ChainApi(self) :
"EOS7d9A3uLe6As66jzN8j44TXJUqJSK3bFjjEEqR4oTvNAB3iM9SA",
"EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV"]}
ret_json = self.nodeos.processUrllibRequest(resource, command, payload)
self.assertEqual(ret_json["code"], 500)
self.assertEqual(ret_json["code"], 400)

# get_transaction_id with empty parameter
command = "get_transaction_id"
Expand Down Expand Up @@ -652,7 +652,7 @@ def test_ChainApi(self) :
"packed_context_free_data": "context_free_data",
"packed_trx": "packed_trx"}
ret_json = self.nodeos.processUrllibRequest(resource, command, payload)
self.assertEqual(ret_json["code"], 500)
self.assertEqual(ret_json["code"], 400)

# push_transactions with empty parameter
command = "push_transactions"
Expand Down Expand Up @@ -694,7 +694,7 @@ def test_ChainApi(self) :
"packed_context_free_data": "context_free_data",
"packed_trx": "packed_trx"}
ret_json = self.nodeos.processUrllibRequest(resource, command, payload)
self.assertEqual(ret_json["code"], 500)
self.assertEqual(ret_json["code"], 400)


# test all net api
Expand Down Expand Up @@ -1091,7 +1091,7 @@ def test_WalletApi(self) :
["EOS696giL6VxeJhtEgKtWPK8aQeT8YXNjw2a7vE5wHunffhfa5QSQ"],
"cf057bbfb72640471fd910bcb67639c22df9f92470936cddc1ade0e2f2e7dc4f"]
ret_json = self.nodeos.processUrllibRequest(resource, command, payload, endpoint=endpoint)
self.assertEqual(ret_json["code"], 500)
self.assertEqual(ret_json["code"], 400)
self.assertEqual(ret_json["error"]["code"], 3120004)

# create with empty parameter
Expand All @@ -1105,7 +1105,7 @@ def test_WalletApi(self) :
self.assertEqual(ret_json["error"]["code"], 3200006)
# create with invalid parameter
ret_json = self.nodeos.processUrllibRequest(resource, command, self.http_post_invalid_param, endpoint=endpoint)
self.assertEqual(ret_json["code"], 500)
self.assertEqual(ret_json["code"], 400)
self.assertEqual(ret_json["error"]["code"], 3120000)
# create with valid parameter
payload = "test1"
Expand All @@ -1123,12 +1123,12 @@ def test_WalletApi(self) :
self.assertEqual(ret_json["error"]["code"], 3200006)
# create with invalid parameter
ret_json = self.nodeos.processUrllibRequest(resource, command, self.http_post_invalid_param, endpoint=endpoint)
self.assertEqual(ret_json["code"], 500)
self.assertEqual(ret_json["code"], 400)
self.assertEqual(ret_json["error"]["code"], 3120000)
# create with valid parameter
payload = "fakeacct"
ret_json = self.nodeos.processUrllibRequest(resource, command, payload, endpoint=endpoint)
self.assertEqual(ret_json["code"], 500)
self.assertEqual(ret_json["code"], 400)

# lock_all with empty parameter
command = "lock_all"
Expand All @@ -1153,7 +1153,7 @@ def test_WalletApi(self) :
self.assertEqual(ret_json["error"]["code"], 3200006)
# lock with invalid parameter
ret_json = self.nodeos.processUrllibRequest(resource, command, self.http_post_invalid_param, endpoint=endpoint)
self.assertEqual(ret_json["code"], 500)
self.assertEqual(ret_json["code"], 400)
self.assertEqual(ret_json["error"]["code"], 3120002)
# lock with valid parameter
payload = {"name":"auser"}
Expand All @@ -1176,7 +1176,7 @@ def test_WalletApi(self) :
# unlock with valid parameter
payload = ["auser", "nopassword"]
ret_json = self.nodeos.processUrllibRequest(resource, command, payload, endpoint=endpoint)
self.assertEqual(ret_json["code"], 500)
self.assertEqual(ret_json["code"], 400)

# import_key with empty parameter
command = "import_key"
Expand All @@ -1194,7 +1194,7 @@ def test_WalletApi(self) :
# import_key with valid parameter
payload = ["auser", "nokey"]
ret_json = self.nodeos.processUrllibRequest(resource, command, payload, endpoint=endpoint)
self.assertEqual(ret_json["code"], 500)
self.assertEqual(ret_json["code"], 400)

# remove_key with empty parameter
command = "remove_key"
Expand All @@ -1212,7 +1212,7 @@ def test_WalletApi(self) :
# remove_key with valid parameter
payload = ["auser", "none", "none"]
ret_json = self.nodeos.processUrllibRequest(resource, command, payload, endpoint=endpoint)
self.assertEqual(ret_json["code"], 500)
self.assertEqual(ret_json["code"], 400)

# create_key with empty parameter
command = "create_key"
Expand All @@ -1230,7 +1230,7 @@ def test_WalletApi(self) :
# create_key with valid parameter
payload = ["auser", "none"]
ret_json = self.nodeos.processUrllibRequest(resource, command, payload, endpoint=endpoint)
self.assertEqual(ret_json["code"], 500)
self.assertEqual(ret_json["code"], 400)

# list_wallets with empty parameter
command = "list_wallets"
Expand Down Expand Up @@ -1260,15 +1260,15 @@ def test_WalletApi(self) :
# list_keys with valid parameter
payload = ["auser", "unknownkey"]
ret_json = self.nodeos.processUrllibRequest(resource, command, payload, endpoint=endpoint)
self.assertEqual(ret_json["code"], 500)
self.assertEqual(ret_json["code"], 400)

# get_public_keys with empty parameter
command = "get_public_keys"
ret_json = self.nodeos.processUrllibRequest(resource, command, endpoint=endpoint)
self.assertEqual(ret_json["code"], 500)
self.assertEqual(ret_json["code"], 400)
# get_public_keys with empty content parameter
ret_json = self.nodeos.processUrllibRequest(resource, command, self.empty_content_dict, endpoint=endpoint)
self.assertEqual(ret_json["code"], 500)
self.assertEqual(ret_json["code"], 400)
# list_wallets with invalid parameter
ret_json = self.nodeos.processUrllibRequest(resource, command, self.http_post_invalid_param, endpoint=endpoint)
self.assertEqual(ret_json["code"], 400)
Expand Down

0 comments on commit 6d9dff6

Please sign in to comment.