#pragma once /** * @file exception.hpp * @brief Defines exception's used by fc */ #include #include #include #include namespace fc { namespace detail { class exception_impl; } enum exception_code { /** for exceptions we threw that don't have an assigned code */ unspecified_exception_code = 0, unhandled_exception_code = 1, ///< for unhandled 3rd party exceptions timeout_exception_code = 2, ///< timeout exceptions file_not_found_exception_code = 3, parse_error_exception_code = 4, invalid_arg_exception_code = 5, key_not_found_exception_code = 6, bad_cast_exception_code = 7, out_of_range_exception_code = 8, canceled_exception_code = 9, assert_exception_code = 10, eof_exception_code = 11, std_exception_code = 13, invalid_operation_exception_code = 14 }; /** * @brief Used to generate a useful error report when an exception is thrown. * @ingroup serializable * * At each level in the stack where the exception is caught and rethrown a * new log_message is added to the exception. * * exception's are designed to be serialized to a variant and * deserialized from an variant. * * @see FC_THROW_EXCEPTION * @see FC_RETHROW_EXCEPTION * @see FC_RETHROW_EXCEPTIONS */ class exception { public: enum code_enum { code_value = unspecified_exception_code }; exception( int64_t code = unspecified_exception_code, const std::string& name_value = "exception", const std::string& what_value = "unspecified"); exception( log_message&&, int64_t code = unspecified_exception_code, const std::string& name_value = "exception", const std::string& what_value = "unspecified"); exception( log_messages&&, int64_t code = unspecified_exception_code, const std::string& name_value = "exception", const std::string& what_value = "unspecified"); exception( const exception& e ); exception( exception&& e ); ~exception(); const char* name()const throw(); int64_t code()const throw(); virtual const char* what()const throw(); /** * @return a reference to log messages that have * been added to this log. */ const log_messages& get_log()const; void append_log( log_message m ); /** * Generates a detailed string including file, line, method, * and other information that is generally only useful for * developers. */ std::string to_detail_string( log_level ll = log_level::all )const; /** * Generates a user-friendly error report. */ std::string to_string( log_level ll = log_level::info )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 * try { throwAsDynamic_exception(); } * catch( ... ) { return std::current_exception(); } * @endcode */ virtual std::shared_ptr dynamic_copy_exception()const; friend void to_variant( const exception& e, variant& v ); friend void from_variant( const variant& e, exception& ll ); protected: std::unique_ptr my; }; void to_variant( const exception& e, variant& v ); void from_variant( const variant& e, exception& ll ); typedef std::shared_ptr exception_ptr; typedef optional oexception; /** * @brief re-thrown whenever an unhandled exception is caught. * @ingroup serializable * Any exceptions thrown by 3rd party libraries that are not * caught get wrapped in an unhandled_exception exception. * * The original exception is captured as a std::exception_ptr * which may be rethrown. The std::exception_ptr does not * propgate across process boundaries. */ class unhandled_exception : public exception { public: enum code_enum { code_value = unhandled_exception_code, }; unhandled_exception( log_message&& m, std::exception_ptr e = std::current_exception() ); unhandled_exception( log_messages ); unhandled_exception( const exception& ); std::exception_ptr get_inner_exception()const; virtual NO_RETURN void dynamic_rethrow_exception()const; virtual std::shared_ptr dynamic_copy_exception()const; private: std::exception_ptr _inner; }; template fc::exception_ptr copy_exception( T&& e ) { #if defined(_MSC_VER) && (_MSC_VER < 1700) return std::make_shared( log_message(), std::copy_exception(fc::forward(e)) ); #else return std::make_shared( log_message(), std::make_exception_ptr(fc::forward(e)) ); #endif } class exception_factory { public: struct base_exception_builder { virtual NO_RETURN void rethrow( const exception& e )const = 0; }; template struct exception_builder : public base_exception_builder { virtual NO_RETURN void rethrow( const exception& e )const override { throw T( e ); } }; template void register_exception() { static exception_builder 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 _registered_exceptions; }; #define FC_REGISTER_EXCEPTION(r, unused, base) \ fc::exception_factory::instance().register_exception(); #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_EXCEPTION( TYPE, CODE, WHAT ) \ class TYPE : public fc::exception \ { \ public: \ enum code_enum { \ code_value = CODE, \ }; \ TYPE( fc::log_message&& m ) \ :exception( fc::move(m), CODE, BOOST_PP_STRINGIZE(TYPE), WHAT ){}\ TYPE( fc::log_messages msgs ) \ :exception( fc::move( msgs ), CODE, BOOST_PP_STRINGIZE(TYPE), WHAT ) {} \ TYPE( const TYPE& c ) \ :exception(c){} \ TYPE( const exception& c ) \ :exception(c){} \ TYPE():exception(){}\ \ virtual std::shared_ptr dynamic_copy_exception()const\ { return std::make_shared( *this ); } \ virtual NO_RETURN void dynamic_rethrow_exception()const \ { if( code() == CODE ) throw *this;\ else fc::exception::dynamic_rethrow_exception(); \ } \ }; FC_DECLARE_EXCEPTION( timeout_exception, timeout_exception_code, "Timeout" ); FC_DECLARE_EXCEPTION( file_not_found_exception, file_not_found_exception_code, "File Not Found" ); /** * @brief report's parse errors */ FC_DECLARE_EXCEPTION( parse_error_exception, parse_error_exception_code, "Parse Error" ); FC_DECLARE_EXCEPTION( invalid_arg_exception, invalid_arg_exception_code, "Invalid Argument" ); /** * @brief reports when a key, guid, or other item is not found. */ FC_DECLARE_EXCEPTION( key_not_found_exception, key_not_found_exception_code, "Key Not Found" ); FC_DECLARE_EXCEPTION( bad_cast_exception, bad_cast_exception_code, "Bad Cast" ); FC_DECLARE_EXCEPTION( out_of_range_exception, out_of_range_exception_code, "Out of Range" ); /** @brief if an operation is unsupported or not valid this may be thrown */ FC_DECLARE_EXCEPTION( invalid_operation_exception, invalid_operation_exception_code, "Invalid Operation" ); /** * @brief used to report a canceled Operation */ FC_DECLARE_EXCEPTION( canceled_exception, canceled_exception_code, "Canceled" ); /** * @brief used inplace of assert() to report violations of pre conditions. */ FC_DECLARE_EXCEPTION( assert_exception, assert_exception_code, "Assert Exception" ); FC_DECLARE_EXCEPTION( eof_exception, eof_exception_code, "End Of File" ); std::string except_str(); } // namespace fc /** *@brief: Workaround for varying preprocessing behavior between MSVC and gcc */ #define FC_EXPAND_MACRO( x ) x /** * @brief Checks a condition and throws an assert_exception if the test is FALSE */ #define FC_ASSERT( TEST, ... ) \ FC_EXPAND_MACRO( \ do { if( !(TEST) ) { FC_THROW_EXCEPTION( fc::assert_exception, #TEST ": " __VA_ARGS__ ); } } while(0); \ ) #define FC_THROW( FORMAT, ... ) \ do { \ throw fc::exception( FC_LOG_MESSAGE( error, FORMAT, __VA_ARGS__ ) ); \ } while(0) #define FC_EXCEPTION( EXCEPTION_TYPE, FORMAT, ... ) \ EXCEPTION_TYPE( FC_LOG_MESSAGE( error, FORMAT, __VA_ARGS__ ) ) /** * @def FC_THROW_EXCEPTION( EXCEPTION, FORMAT, ... ) * @param EXCEPTION a class in the Phoenix::Athena::API namespace that inherits * @param format - a const char* string with "${keys}" */ #define FC_THROW_EXCEPTION( EXCEPTION, FORMAT, ... ) \ do { \ throw EXCEPTION( FC_LOG_MESSAGE( error, FORMAT, __VA_ARGS__ ) ); \ } while(0) /** * @def FC_RETHROW_EXCEPTION(ER,LOG_LEVEL,FORMAT,...) * @brief Appends a log_message to the exception ER and rethrows it. */ #define FC_RETHROW_EXCEPTION( ER, LOG_LEVEL, FORMAT, ... ) \ do { \ ER.append_log( FC_LOG_MESSAGE( LOG_LEVEL, FORMAT, __VA_ARGS__ ) ); \ throw;\ } while(false) /** * @def FC_RETHROW_EXCEPTIONS(LOG_LEVEL,FORMAT,...) * @brief Catchs all exception's, std::exceptions, and ... and rethrows them after * appending the provided log message. */ #define FC_RETHROW_EXCEPTIONS( LOG_LEVEL, FORMAT, ... ) \ catch( fc::exception& er ) { \ FC_RETHROW_EXCEPTION( er, LOG_LEVEL, FORMAT, __VA_ARGS__ ); \ } catch( const std::exception& e ) { \ fc::exception fce( \ FC_LOG_MESSAGE( LOG_LEVEL, "${what}" FORMAT,__VA_ARGS__("what",e.what())), \ fc::std_exception_code,\ typeid(e).name(), \ e.what() ) ; throw fce;\ } catch( ... ) { \ throw fc::unhandled_exception( \ FC_LOG_MESSAGE( LOG_LEVEL, FORMAT,__VA_ARGS__), \ std::current_exception() ); \ } #define FC_CAPTURE_AND_RETHROW( ... ) \ catch( fc::exception& er ) { \ FC_RETHROW_EXCEPTION( er, warn, "", FC_FORMAT_ARG_PARAMS(__VA_ARGS__) ); \ } catch( const std::exception& e ) { \ fc::exception fce( \ FC_LOG_MESSAGE( warn, "${what}",FC_FORMAT_ARG_PARAMS(__VA_ARGS__)("what",e.what())), \ fc::std_exception_code,\ typeid(e).name(), \ e.what() ) ; throw fce;\ } catch( ... ) { \ throw fc::unhandled_exception( \ FC_LOG_MESSAGE( warn, "",FC_FORMAT_ARG_PARAMS(__VA_ARGS__)), \ std::current_exception() ); \ }