9#include <unordered_map>
10#include <boost/core/typeinfo.hpp>
11#include <boost/interprocess/exceptions.hpp>
15 namespace detail {
class exception_impl; }
68 const std::string& name_value =
"exception",
69 const std::string& what_value =
"unspecified");
71 const std::string& name_value =
"exception",
72 const std::string& what_value =
"unspecified");
74 const std::string& name_value =
"exception",
75 const std::string& what_value =
"unspecified");
78 const std::string& name_value =
"exception",
79 const std::string& what_value =
"unspecified");
84 const char*
name()
const throw();
86 const
char*
what()const noexcept override;
164 std::exception_ptr get_inner_exception()
const;
169 std::exception_ptr _inner;
183 std::exception_ptr e = std::current_exception(),
184 const std::string& name_value =
"exception",
185 const std::string& what_value =
"unspecified");
187 std::exception_ptr get_inner_exception()
const;
191 virtual NO_RETURN void dynamic_rethrow_exception()
const;
192 virtual std::shared_ptr<exception> dynamic_copy_exception()
const;
194 std::exception_ptr _inner;
200#if defined(_MSC_VER) && (_MSC_VER < 1700)
201 return std::make_shared<unhandled_exception>(
log_message(),
202 std::copy_exception(fc::forward<T>(e)) );
204 return std::make_shared<unhandled_exception>(
log_message(),
205 std::make_exception_ptr(fc::forward<T>(e)) );
231 auto itr = _registered_exceptions.find( T::code_value );
232 assert( itr == _registered_exceptions.end() );
234 _registered_exceptions[T::code_value] = &builder;
246 std::unordered_map<int64_t,base_exception_builder*> _registered_exceptions;
248#define FC_REGISTER_EXCEPTION(r, unused, base) \
249 fc::exception_factory::instance().register_exception<base>();
251#define FC_REGISTER_EXCEPTIONS( SEQ )\
253 static bool exception_init = []()->bool{ \
254 BOOST_PP_SEQ_FOR_EACH( FC_REGISTER_EXCEPTION, v, SEQ ) \
259#define FC_DECLARE_DERIVED_EXCEPTION( TYPE, BASE, CODE, WHAT ) \
260 class TYPE : public BASE \
266 explicit TYPE( int64_t code, const std::string& name_value, const std::string& what_value ) \
267 :BASE( code, name_value, what_value ){} \
268 explicit TYPE( fc::log_message&& m, int64_t code, const std::string& name_value, const std::string& what_value ) \
269 :BASE( std::move(m), code, name_value, what_value ){} \
270 explicit TYPE( fc::log_messages&& m, int64_t code, const std::string& name_value, const std::string& what_value )\
271 :BASE( std::move(m), code, name_value, what_value ){}\
272 explicit TYPE( const fc::log_messages& m, int64_t code, const std::string& name_value, const std::string& what_value )\
273 :BASE( m, code, name_value, what_value ){}\
274 TYPE( const std::string& what_value, const fc::log_messages& m ) \
275 :BASE( m, CODE, BOOST_PP_STRINGIZE(TYPE), what_value ){} \
276 TYPE( fc::log_message&& m ) \
277 :BASE( fc::move(m), CODE, BOOST_PP_STRINGIZE(TYPE), WHAT ){}\
278 TYPE( fc::log_messages msgs ) \
279 :BASE( fc::move( msgs ), CODE, BOOST_PP_STRINGIZE(TYPE), WHAT ) {} \
280 TYPE( const TYPE& c ) \
282 TYPE( const BASE& c ) \
284 TYPE():BASE(CODE, BOOST_PP_STRINGIZE(TYPE), WHAT){}\
286 virtual std::shared_ptr<fc::exception> dynamic_copy_exception()const\
287 { return std::make_shared<TYPE>( *this ); } \
288 virtual NO_RETURN void dynamic_rethrow_exception()const \
289 { if( code() == CODE ) throw *this;\
290 else fc::exception::dynamic_rethrow_exception(); \
294 #define FC_DECLARE_EXCEPTION( TYPE, CODE, WHAT ) \
295 FC_DECLARE_DERIVED_EXCEPTION( TYPE, fc::exception, CODE, WHAT )
314 "Invalid Operation" );
339 const char* filename,
348 #define LIKELY(x) __builtin_expect((long)!!(x), 1L)
349 #define UNLIKELY(x) __builtin_expect((long)!!(x), 0L)
351 #define LIKELY(x) (x)
352 #define UNLIKELY(x) (x)
358#define FC_EXPAND_MACRO( x ) x
362#define FC_ASSERT( TEST, ... ) \
364 FC_MULTILINE_MACRO_BEGIN \
365 if( UNLIKELY(!(TEST)) ) \
367 if( fc::enable_record_assert_trip ) \
368 fc::record_assert_trip( __FILE__, __LINE__, #TEST ); \
369 FC_THROW_EXCEPTION( fc::assert_exception, #TEST ": " __VA_ARGS__ ); \
371 FC_MULTILINE_MACRO_END \
374#define FC_CAPTURE_AND_THROW( EXCEPTION_TYPE, ... ) \
375 FC_MULTILINE_MACRO_BEGIN \
376 throw EXCEPTION_TYPE( FC_LOG_MESSAGE( error, "", FC_FORMAT_ARG_PARAMS(__VA_ARGS__) ) ); \
377 FC_MULTILINE_MACRO_END
382#define FC_INDIRECT_EXPAND(MACRO, ARGS) MACRO ARGS
383#define FC_THROW( ... ) \
384 FC_MULTILINE_MACRO_BEGIN \
385 throw fc::exception( FC_INDIRECT_EXPAND(FC_LOG_MESSAGE, ( error, __VA_ARGS__ )) ); \
386 FC_MULTILINE_MACRO_END
388#define FC_EXCEPTION( EXCEPTION_TYPE, FORMAT, ... ) \
389 EXCEPTION_TYPE( FC_LOG_MESSAGE( error, FORMAT, __VA_ARGS__ ) )
395#define FC_THROW_EXCEPTION( EXCEPTION, FORMAT, ... ) \
396 FC_MULTILINE_MACRO_BEGIN \
397 throw EXCEPTION( FC_LOG_MESSAGE( error, FORMAT, __VA_ARGS__ ) ); \
398 FC_MULTILINE_MACRO_END
405#define FC_RETHROW_EXCEPTION( ER, LOG_LEVEL, FORMAT, ... ) \
406 FC_MULTILINE_MACRO_BEGIN \
407 ER.append_log( FC_LOG_MESSAGE( LOG_LEVEL, FORMAT, __VA_ARGS__ ) ); \
409 FC_MULTILINE_MACRO_END
411#define FC_LOG_AND_RETHROW( ) \
412 catch( const boost::interprocess::bad_alloc& ) {\
414 } catch( fc::exception& er ) { \
415 wlog( "${details}", ("details",er.to_detail_string()) ); \
416 FC_RETHROW_EXCEPTION( er, warn, "rethrow" ); \
417 } catch( const std::exception& e ) { \
418 fc::std_exception_wrapper sew( \
419 FC_LOG_MESSAGE( warn, "rethrow ${what}: ", ("what",e.what())), \
420 std::current_exception(), \
421 BOOST_CORE_TYPEID(e).name(), \
423 wlog( "${details}", ("details",sew.to_detail_string()) ); \
426 fc::unhandled_exception e( \
427 FC_LOG_MESSAGE( warn, "rethrow"), \
428 std::current_exception() ); \
429 wlog( "${details}", ("details",e.to_detail_string()) ); \
433#define FC_CAPTURE_LOG_AND_RETHROW( ... ) \
434 catch( const boost::interprocess::bad_alloc& ) {\
436 } catch( fc::exception& er ) { \
437 wlog( "${details}", ("details",er.to_detail_string()) ); \
438 wdump( __VA_ARGS__ ); \
439 FC_RETHROW_EXCEPTION( er, warn, "rethrow", FC_FORMAT_ARG_PARAMS(__VA_ARGS__) ); \
440 } catch( const std::exception& e ) { \
441 fc::std_exception_wrapper sew( \
442 FC_LOG_MESSAGE( warn, "rethrow ${what}: ", FC_FORMAT_ARG_PARAMS( __VA_ARGS__ )("what",e.what())), \
443 std::current_exception(), \
444 BOOST_CORE_TYPEID(e).name(), \
446 wlog( "${details}", ("details",sew.to_detail_string()) ); \
447 wdump( __VA_ARGS__ ); \
450 fc::unhandled_exception e( \
451 FC_LOG_MESSAGE( warn, "rethrow", FC_FORMAT_ARG_PARAMS( __VA_ARGS__) ), \
452 std::current_exception() ); \
453 wlog( "${details}", ("details",e.to_detail_string()) ); \
454 wdump( __VA_ARGS__ ); \
458#define FC_CAPTURE_AND_LOG( ... ) \
459 catch( const boost::interprocess::bad_alloc& ) {\
461 } catch( fc::exception& er ) { \
462 wlog( "${details}", ("details",er.to_detail_string()) ); \
463 wdump( __VA_ARGS__ ); \
464 } catch( const std::exception& e ) { \
465 fc::std_exception_wrapper sew( \
466 FC_LOG_MESSAGE( warn, "rethrow ${what}: ",FC_FORMAT_ARG_PARAMS( __VA_ARGS__ )("what",e.what()) ), \
467 std::current_exception(), \
468 BOOST_CORE_TYPEID(e).name(), \
470 wlog( "${details}", ("details",sew.to_detail_string()) ); \
471 wdump( __VA_ARGS__ ); \
473 fc::unhandled_exception e( \
474 FC_LOG_MESSAGE( warn, "rethrow", FC_FORMAT_ARG_PARAMS( __VA_ARGS__) ), \
475 std::current_exception() ); \
476 wlog( "${details}", ("details",e.to_detail_string()) ); \
477 wdump( __VA_ARGS__ ); \
480#define FC_LOG_AND_DROP( ... ) \
481 catch( const boost::interprocess::bad_alloc& ) {\
483 } catch( fc::exception& er ) { \
484 wlog( "${details}", ("details",er.to_detail_string()) ); \
485 } catch( const std::exception& e ) { \
486 fc::std_exception_wrapper sew( \
487 FC_LOG_MESSAGE( warn, "rethrow ${what}: ",FC_FORMAT_ARG_PARAMS( __VA_ARGS__ )("what",e.what()) ), \
488 std::current_exception(), \
489 BOOST_CORE_TYPEID(e).name(), \
491 wlog( "${details}", ("details",sew.to_detail_string()) ); \
493 fc::unhandled_exception e( \
494 FC_LOG_MESSAGE( warn, "rethrow", FC_FORMAT_ARG_PARAMS( __VA_ARGS__) ), \
495 std::current_exception() ); \
496 wlog( "${details}", ("details",e.to_detail_string()) ); \
505#define FC_RETHROW_EXCEPTIONS( LOG_LEVEL, FORMAT, ... ) \
506 catch( const boost::interprocess::bad_alloc& ) {\
508 } catch( fc::exception& er ) { \
509 FC_RETHROW_EXCEPTION( er, LOG_LEVEL, FORMAT, __VA_ARGS__ ); \
510 } catch( const std::exception& e ) { \
511 fc::std_exception_wrapper sew( \
512 FC_LOG_MESSAGE( LOG_LEVEL, "${what}: " FORMAT,__VA_ARGS__("what",e.what())), \
513 std::current_exception(), \
514 BOOST_CORE_TYPEID(e).name(), \
518 throw fc::unhandled_exception( \
519 FC_LOG_MESSAGE( LOG_LEVEL, FORMAT,__VA_ARGS__), \
520 std::current_exception() ); \
523#define FC_CAPTURE_AND_RETHROW( ... ) \
524 catch( const boost::interprocess::bad_alloc& ) {\
526 } catch( fc::exception& er ) { \
527 FC_RETHROW_EXCEPTION( er, warn, "", FC_FORMAT_ARG_PARAMS(__VA_ARGS__) ); \
528 } catch( const std::exception& e ) { \
529 fc::std_exception_wrapper sew( \
530 FC_LOG_MESSAGE( warn, "${what}: ",FC_FORMAT_ARG_PARAMS(__VA_ARGS__)("what",e.what())), \
531 std::current_exception(), \
532 BOOST_CORE_TYPEID(e).name(), \
536 throw fc::unhandled_exception( \
537 FC_LOG_MESSAGE( warn, "",FC_FORMAT_ARG_PARAMS(__VA_ARGS__)), \
538 std::current_exception() ); \
541#define FC_CHECK_DEADLINE( DEADLINE, ... ) \
542 FC_MULTILINE_MACRO_BEGIN \
543 if( DEADLINE < fc::time_point::maximum() && DEADLINE < fc::time_point::now() ) { \
544 auto log_mgs = FC_LOG_MESSAGE( error, "deadline ${d} exceeded by ${t}us ", \
545 FC_FORMAT_ARG_PARAMS(__VA_ARGS__)("d", DEADLINE)("t", fc::time_point::now() - DEADLINE) ); \
546 auto msg = log_mgs.get_limited_message(); \
547 throw fc::timeout_exception( std::move( log_mgs ), fc::timeout_exception_code, "timeout_exception", std::move( msg ) ); \
549 FC_MULTILINE_MACRO_END
void register_exception()
static exception_factory & instance()
Used to generate a useful error report when an exception is thrown.
static constexpr fc::microseconds format_time_limit
virtual std::shared_ptr< exception > dynamic_copy_exception() const
friend void to_variant(const exception &e, variant &v)
exception(int64_t code=unspecified_exception_code, const std::string &name_value="exception", const std::string &what_value="unspecified")
void append_log(log_message m)
const log_messages & get_log() const
std::string to_detail_string(log_level ll=log_level::all) const
std::unique_ptr< detail::exception_impl > my
friend void from_variant(const variant &e, exception &ll)
const char * name() const
const char * what() const noexcept override
virtual NO_RETURN void dynamic_rethrow_exception() const
std::string top_message() const
aggregates a message along with the context and associated meta-information.
wrapper for std::exception
re-thrown whenever an unhandled exception is caught.Any exceptions thrown by 3rd party libraries that...
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
#define FC_DECLARE_EXCEPTION(TYPE, CODE, WHAT)
std::shared_ptr< exception > exception_ptr
fc::exception_ptr copy_exception(T &&e)
bool enable_record_assert_trip
void record_assert_trip(const char *filename, uint32_t lineno, const char *expr)
std::vector< log_message > log_messages
constexpr microseconds milliseconds(int64_t s)
void copy(const path &from, const path &to)
@ timeout_exception_code
timeout exceptions
@ unknown_host_exception_code
@ bad_cast_exception_code
@ unhandled_exception_code
for unhandled 3rd party exceptions
@ file_not_found_exception_code
@ unspecified_exception_code
@ parse_error_exception_code
@ invalid_arg_exception_code
@ key_not_found_exception_code
@ canceled_exception_code
@ invalid_operation_exception_code
@ out_of_range_exception_code
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
std::optional< exception > oexception
#define T(meth, val, expected)
virtual NO_RETURN void rethrow(const exception &e) const =0
virtual NO_RETURN void rethrow(const exception &e) const override