Skip to content

Commit

Permalink
Merge pull request #1031 from AntelopeIO/GH-980-fc-except
Browse files Browse the repository at this point in the history
Remove fc dynamic_rethrow_exception
  • Loading branch information
heifner authored Nov 15, 2024
2 parents be54b82 + 6fe3e3f commit 07d65b8
Show file tree
Hide file tree
Showing 8 changed files with 9 additions and 124 deletions.
4 changes: 0 additions & 4 deletions libraries/chain/include/eosio/chain/exceptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,6 @@
\
virtual std::shared_ptr<fc::exception> dynamic_copy_exception()const\
{ return std::make_shared<TYPE>( *this ); } \
virtual NO_RETURN void dynamic_rethrow_exception()const \
{ if( code() == CODE ) throw *this;\
else fc::exception::dynamic_rethrow_exception(); \
} \
std::optional<uint64_t> error_code; \
};

Expand Down
2 changes: 1 addition & 1 deletion libraries/chain/snapshot_scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ void snapshot_scheduler::execute_snapshot(uint32_t srid, chain::controller& chai
auto next = [srid, this](const chain::next_function_variant<snapshot_information>& result) {
if(std::holds_alternative<fc::exception_ptr>(result)) {
try {
std::get<fc::exception_ptr>(result)->dynamic_rethrow_exception();
throw *std::get<fc::exception_ptr>(result);
} catch(const fc::exception& e) {
EOS_THROW(snapshot_execution_exception,
"Snapshot creation error: ${details}",
Expand Down
62 changes: 0 additions & 62 deletions libraries/libfc/include/fc/exception/exception.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,6 @@ namespace fc
*/
std::string top_message( )const;

/**
* Throw this exception as its most derived type.
*
* @note does not return.
*/
virtual NO_RETURN void dynamic_rethrow_exception()const;

/**
* This is equivalent to:
* @code
Expand Down Expand Up @@ -163,7 +156,6 @@ namespace fc

std::exception_ptr get_inner_exception()const;

virtual NO_RETURN void dynamic_rethrow_exception()const;
virtual std::shared_ptr<exception> dynamic_copy_exception()const;
private:
std::exception_ptr _inner;
Expand All @@ -188,7 +180,6 @@ namespace fc

static std_exception_wrapper from_current_exception(const std::exception& e);

virtual NO_RETURN void dynamic_rethrow_exception()const;
virtual std::shared_ptr<exception> dynamic_copy_exception()const;
private:
std::exception_ptr _inner;
Expand All @@ -207,55 +198,6 @@ namespace fc
}


class exception_factory
{
public:
struct base_exception_builder
{
virtual NO_RETURN void rethrow( const exception& e )const = 0;
};

template<typename T>
struct exception_builder : public base_exception_builder
{
virtual NO_RETURN void rethrow( const exception& e )const override
{
throw T( e );
}
};

template<typename T>
void register_exception()
{
static exception_builder<T> builder;
auto itr = _registered_exceptions.find( T::code_value );
assert( itr == _registered_exceptions.end() );
(void)itr; // in release builds this hides warnings
_registered_exceptions[T::code_value] = &builder;
}

void NO_RETURN rethrow( const exception& e )const;

static exception_factory& instance()
{
static exception_factory once;
return once;
}

private:
std::unordered_map<int64_t,base_exception_builder*> _registered_exceptions;
};
#define FC_REGISTER_EXCEPTION(r, unused, base) \
fc::exception_factory::instance().register_exception<base>();

#define FC_REGISTER_EXCEPTIONS( SEQ )\
\
static bool exception_init = []()->bool{ \
BOOST_PP_SEQ_FOR_EACH( FC_REGISTER_EXCEPTION, v, SEQ ) \
return true; \
}(); \


#define FC_DECLARE_DERIVED_EXCEPTION( TYPE, BASE, CODE, WHAT ) \
class TYPE : public BASE \
{ \
Expand Down Expand Up @@ -285,10 +227,6 @@ namespace fc
\
virtual std::shared_ptr<fc::exception> dynamic_copy_exception()const\
{ return std::make_shared<TYPE>( *this ); } \
virtual NO_RETURN void dynamic_rethrow_exception()const \
{ if( code() == CODE ) throw *this;\
else fc::exception::dynamic_rethrow_exception(); \
} \
};

#define FC_DECLARE_EXCEPTION( TYPE, CODE, WHAT ) \
Expand Down
49 changes: 0 additions & 49 deletions libraries/libfc/src/exception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,6 @@

namespace fc
{
FC_REGISTER_EXCEPTIONS( (timeout_exception)
(file_not_found_exception)
(parse_error_exception)
(invalid_arg_exception)
(invalid_operation_exception)
(key_not_found_exception)
(bad_cast_exception)
(out_of_range_exception)
(canceled_exception)
(assert_exception)
(eof_exception)
(unknown_host_exception)
(null_optional)
(udt_exception)
(aes_exception)
(overflow_exception)
(underflow_exception)
(divide_by_zero_exception)
)

namespace detail
{
class exception_impl
Expand Down Expand Up @@ -77,12 +57,6 @@ namespace fc

std::exception_ptr unhandled_exception::get_inner_exception()const { return _inner; }

NO_RETURN void unhandled_exception::dynamic_rethrow_exception()const
{
if( !(_inner == std::exception_ptr()) ) std::rethrow_exception( _inner );
else { fc::exception::dynamic_rethrow_exception(); }
}

std::shared_ptr<exception> unhandled_exception::dynamic_copy_exception()const
{
auto e = std::make_shared<unhandled_exception>( *this );
Expand Down Expand Up @@ -244,23 +218,6 @@ namespace fc
return std::string();
}

void NO_RETURN exception_factory::rethrow( const exception& e )const
{
auto itr = _registered_exceptions.find( e.code() );
if( itr != _registered_exceptions.end() )
itr->second->rethrow( e );
throw e;
}
/**
* Rethrows the exception restoring the proper type based upon
* the error code. This is used to propagate exception types
* across conversions to/from JSON
*/
NO_RETURN void exception::dynamic_rethrow_exception()const
{
exception_factory::instance().rethrow( *this );
}

exception_ptr exception::dynamic_copy_exception()const
{
return std::make_shared<exception>(*this);
Expand Down Expand Up @@ -342,12 +299,6 @@ namespace fc

std::exception_ptr std_exception_wrapper::get_inner_exception()const { return _inner; }

NO_RETURN void std_exception_wrapper::dynamic_rethrow_exception()const
{
if( !(_inner == std::exception_ptr()) ) std::rethrow_exception( _inner );
else { fc::exception::dynamic_rethrow_exception(); }
}

std::shared_ptr<exception> std_exception_wrapper::dynamic_copy_exception()const
{
auto e = std::make_shared<std_exception_wrapper>( *this );
Expand Down
4 changes: 2 additions & 2 deletions libraries/testing/tester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ namespace eosio::testing {
auto trace = control->push_transaction( itr->trx_meta, fc::time_point::maximum(), fc::microseconds::maximum(), DEFAULT_BILLED_CPU_TIME_US, true, 0 );
if(!no_throw && trace->except) {
// this always throws an fc::exception, since the original exception is copied into an fc::exception
trace->except->dynamic_rethrow_exception();
throw *trace->except;
}
itr = unapplied_transactions.erase( itr );
res.unapplied_transaction_traces.emplace_back( std::move(trace) );
Expand All @@ -495,7 +495,7 @@ namespace eosio::testing {
auto trace = control->push_scheduled_transaction( trx, fc::time_point::maximum(), fc::microseconds::maximum(), DEFAULT_BILLED_CPU_TIME_US, true );
if( !no_throw && trace->except ) {
// this always throws an fc::exception, since the original exception is copied into an fc::exception
trace->except->dynamic_rethrow_exception();
throw *trace->except;
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions plugins/http_plugin/include/eosio/http_plugin/macros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
(const chain::next_function_variant<call_result>& result) mutable { \
if (std::holds_alternative<fc::exception_ptr>(result)) { \
try { \
std::get<fc::exception_ptr>(result)->dynamic_rethrow_exception(); \
throw *std::get<fc::exception_ptr>(result); \
} catch (...) { \
http_plugin::handle_exception(#api_name, #call_name, body, cb); \
} \
Expand All @@ -28,7 +28,7 @@
chain::t_or_exception<call_result> result = http_fwd(); \
if (std::holds_alternative<fc::exception_ptr>(result)) { \
try { \
std::get<fc::exception_ptr>(result)->dynamic_rethrow_exception(); \
throw *std::get<fc::exception_ptr>(result); \
} catch (...) { \
http_plugin::handle_exception(#api_name, #call_name, body, cb); \
} \
Expand Down Expand Up @@ -65,7 +65,7 @@
chain::t_or_exception<call_result> result = http_fwd(); \
if (std::holds_alternative<fc::exception_ptr>(result)) { \
try { \
std::get<fc::exception_ptr>(result)->dynamic_rethrow_exception(); \
throw *std::get<fc::exception_ptr>(result); \
} catch (...) { \
http_plugin::handle_exception(#api_name, #call_name, body, cb); \
} \
Expand Down
2 changes: 1 addition & 1 deletion plugins/producer_api_plugin/producer_api_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ using namespace eosio;
auto next = [cb=std::move(cb), body=std::move(body)](const chain::next_function_variant<call_result>& result){ \
if (std::holds_alternative<fc::exception_ptr>(result)) {\
try {\
std::get<fc::exception_ptr>(result)->dynamic_rethrow_exception();\
throw *std::get<fc::exception_ptr>(result);\
} catch (...) {\
http_plugin::handle_exception(#api_name, #call_name, body, cb);\
}\
Expand Down
4 changes: 2 additions & 2 deletions tests/chain_test_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ inline auto push_input_trx(appbase::scoped_app& app, eosio::chain::controller& c
[trx_promise](const next_function_variant<transaction_trace_ptr>& result) {
if( std::holds_alternative<fc::exception_ptr>( result ) ) {
try {
std::get<fc::exception_ptr>(result)->dynamic_rethrow_exception();
throw *std::get<fc::exception_ptr>(result);
} catch(...) {
trx_promise->set_exception(std::current_exception());
}
} else if ( std::get<chain::transaction_trace_ptr>( result )->except ) {
try {
std::get<chain::transaction_trace_ptr>(result)->except->dynamic_rethrow_exception();
throw *std::get<chain::transaction_trace_ptr>(result)->except;
} catch(...) {
trx_promise->set_exception(std::current_exception());
}
Expand Down

0 comments on commit 07d65b8

Please sign in to comment.