From 2eb047a6f4d61704478376a88f12205fe12dde69 Mon Sep 17 00:00:00 2001 From: Kevin Heifner Date: Tue, 22 May 2018 10:59:42 -0400 Subject: [PATCH] Add exception handling to to_string and to_detail_string --- src/exception.cpp | 48 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/src/exception.cpp b/src/exception.cpp index cc93009..bc72783 100644 --- a/src/exception.cpp +++ b/src/exception.cpp @@ -183,9 +183,17 @@ namespace fc string exception::to_detail_string( log_level ll )const { std::stringstream ss; - ss << variant(my->_code).as_string() <<" " << my->_name << ": " <_what<<"\n"; - for( auto itr = my->_elog.begin(); itr != my->_elog.end(); ) - { + try { + try { + ss << variant( my->_code ).as_string(); + } catch( std::bad_alloc& ) { + throw; + } catch( ... ) { + ss << "<- exception in to_detail_string."; + } + ss << " " << my->_name << ": " << my->_what << "\n"; + for( auto itr = my->_elog.begin(); itr != my->_elog.end(); ) { + try { ss << itr->get_message() <<"\n"; //fc::format_string( itr->get_format(), itr->get_data() ) <<"\n"; try { @@ -197,8 +205,18 @@ namespace fc } ss << " " << itr->get_context().to_string(); ++itr; + } catch( std::bad_alloc& ) { + throw; + } catch( ... ) { + ss << "<- exception in to_detail_string."; + } if( itr != my->_elog.end() ) ss<<"\n"; } + } catch( std::bad_alloc& ) { + throw; + } catch( ... ) { + ss << "<- exception in to_detail_string.\n"; + } return ss.str(); } @@ -208,13 +226,31 @@ namespace fc string exception::to_string( log_level ll )const { std::stringstream ss; - ss << what() << " (" << variant(my->_code).as_string() <<")\n"; - for( auto itr = my->_elog.begin(); itr != my->_elog.end(); ++itr ) - { + try { + ss << my->_what; + try { + ss << " (" << variant( my->_code ).as_string() << ")\n"; + } catch( std::bad_alloc& ) { + throw; + } catch( ... ) { + ss << "<- exception in to_string.\n"; + } + for( auto itr = my->_elog.begin(); itr != my->_elog.end(); ++itr ) { + try { ss << fc::format_string( itr->get_format(), itr->get_data() ) << "\n"; // ss << " " << itr->get_context().to_string() <<"\n"; + } catch( std::bad_alloc& ) { + throw; + } catch( ... ) { + ss << "<- exception in to_string.\n"; + } } return ss.str(); + } catch( std::bad_alloc& ) { + throw; + } catch( ... ) { + ss << "<- exception in to_string.\n"; + } } [[noreturn]] void exception_factory::rethrow( const exception& e )const