2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2016-07-26 04:06:31 +02:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_EXCEPTION_H
|
|
|
|
|
2018-03-08 17:34:55 +01:00
|
|
|
/// Forward declarations for boost::system because it is not included here.
|
|
|
|
namespace boost::system
|
|
|
|
{
|
|
|
|
struct error_code;
|
|
|
|
struct system_error;
|
|
|
|
namespace errc {}
|
|
|
|
}
|
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
namespace ircd
|
|
|
|
{
|
2018-01-08 22:26:29 +01:00
|
|
|
// Root exception
|
2017-08-28 23:51:22 +02:00
|
|
|
struct exception;
|
2017-11-26 01:20:42 +01:00
|
|
|
|
|
|
|
// Prefer ircd::terminate() to std::terminate() if possible.
|
|
|
|
[[noreturn]] void terminate(const std::exception &) noexcept;
|
|
|
|
[[noreturn]] void terminate(std::exception_ptr) noexcept;
|
|
|
|
[[noreturn]] void terminate() noexcept;
|
|
|
|
|
2018-01-08 22:26:29 +01:00
|
|
|
// Terminates in debug mode; throws in release mode; always logs critical.
|
2018-01-27 06:49:41 +01:00
|
|
|
[[noreturn]] void assertion(const std::exception &) noexcept(RB_DEBUG_LEVEL);
|
|
|
|
[[noreturn]] void assertion(std::exception_ptr) noexcept(RB_DEBUG_LEVEL);
|
|
|
|
[[noreturn]] void assertion() noexcept(RB_DEBUG_LEVEL);
|
2018-01-11 06:32:32 +01:00
|
|
|
|
|
|
|
// util
|
2018-03-08 18:21:16 +01:00
|
|
|
std::error_code make_error_code(const int &code = errno);
|
|
|
|
std::error_code make_error_code(const std::error_code &);
|
|
|
|
std::error_code make_error_code(const boost::system::error_code &);
|
|
|
|
std::error_code make_error_code(const boost::system::system_error &);
|
|
|
|
std::system_error make_system_error(const int &code = errno);
|
|
|
|
std::system_error make_system_error(const boost::system::error_code &);
|
|
|
|
std::system_error make_system_error(const boost::system::system_error &);
|
|
|
|
template<class... args> std::exception_ptr make_system_eptr(args&&...);
|
|
|
|
template<class... args> [[noreturn]] void throw_system_error(args&&...);
|
|
|
|
|
|
|
|
string_view string(const mutable_buffer &, const std::error_code &);
|
|
|
|
string_view string(const mutable_buffer &, const std::system_error &);
|
2018-03-08 17:34:55 +01:00
|
|
|
string_view string(const mutable_buffer &, const boost::system::error_code &);
|
|
|
|
string_view string(const mutable_buffer &, const boost::system::system_error &);
|
2018-03-08 18:21:16 +01:00
|
|
|
std::string string(const std::error_code &);
|
|
|
|
std::string string(const std::system_error &);
|
2018-03-08 17:34:55 +01:00
|
|
|
std::string string(const boost::system::error_code &);
|
|
|
|
std::string string(const boost::system::system_error &);
|
|
|
|
|
2017-11-26 01:20:42 +01:00
|
|
|
// Can be used to clobber the std::terminate_handler
|
|
|
|
void aborting() noexcept;
|
2017-08-28 23:51:22 +02:00
|
|
|
}
|
2016-07-26 04:06:31 +02:00
|
|
|
|
2017-09-12 18:37:44 +02:00
|
|
|
/// The root exception type.
|
|
|
|
///
|
|
|
|
/// All exceptions in the project inherit from this type.
|
|
|
|
/// To catch any exception from a project developer's code:
|
|
|
|
/// catch(const ircd::exception &) {}
|
|
|
|
///
|
2018-05-06 10:00:11 +02:00
|
|
|
/// Remember: not all exceptions are from project developer's code, such as
|
|
|
|
/// std::out_of_range. In most contexts if you have to deal with this someone
|
|
|
|
/// else was lazy, which is bad. To be sure and catch any exception:
|
2017-09-12 18:37:44 +02:00
|
|
|
/// catch(const std::exception &) {}
|
|
|
|
///
|
2018-05-06 10:00:11 +02:00
|
|
|
/// Remember: not all exceptions have to inherit from std::exception. We have
|
|
|
|
/// only one example of this: ctx::terminated. To be sure nothing can possibly
|
|
|
|
/// get through, add to the bottom, with care:
|
2017-09-12 18:37:44 +02:00
|
|
|
/// catch(...) {}
|
|
|
|
///
|
|
|
|
/// Note: Prefer 'noexcept' instead of catch(...), noexcept is like an 'assert'
|
2018-05-06 10:00:11 +02:00
|
|
|
/// for exceptions, and a rogue can be found-out in testing.
|
2017-09-12 18:37:44 +02:00
|
|
|
///
|
2017-08-28 23:51:22 +02:00
|
|
|
struct ircd::exception
|
2016-07-26 04:06:31 +02:00
|
|
|
:std::exception
|
|
|
|
{
|
2016-08-15 04:03:25 +02:00
|
|
|
protected:
|
2016-10-17 21:15:26 +02:00
|
|
|
IRCD_OVERLOAD(generate_skip)
|
|
|
|
|
2016-08-15 04:03:25 +02:00
|
|
|
char buf[BUFSIZE];
|
2016-07-26 04:06:31 +02:00
|
|
|
|
2017-03-18 01:37:40 +01:00
|
|
|
ssize_t generate(const char *const &name, const char *const &fmt, const va_rtti &ap) noexcept;
|
|
|
|
ssize_t generate(const char *const &fmt, const va_rtti &ap) noexcept;
|
2016-07-26 04:06:31 +02:00
|
|
|
|
2016-08-15 04:03:25 +02:00
|
|
|
public:
|
2018-01-27 06:58:52 +01:00
|
|
|
const char *what() const noexcept final override
|
2016-08-15 04:03:25 +02:00
|
|
|
{
|
|
|
|
return buf;
|
|
|
|
}
|
2016-07-26 04:06:31 +02:00
|
|
|
|
2016-10-17 21:15:26 +02:00
|
|
|
exception(const generate_skip_t = {}) noexcept
|
2016-08-15 04:03:25 +02:00
|
|
|
{
|
|
|
|
buf[0] = '\0';
|
|
|
|
}
|
|
|
|
};
|
2016-07-26 04:06:31 +02:00
|
|
|
|
2017-09-12 18:37:44 +02:00
|
|
|
/// Exception generator convenience macro
|
|
|
|
///
|
|
|
|
/// If you want to create your own exception type, you have found the right
|
|
|
|
/// place! This macro allows creating an exception in the the hierarchy.
|
|
|
|
///
|
|
|
|
/// To create an exception, invoke this macro in your header. Examples:
|
|
|
|
///
|
|
|
|
/// IRCD_EXCEPTION(ircd::exception, my_exception)
|
|
|
|
/// IRCD_EXCEPTION(my_exception, my_specific_exception)
|
|
|
|
///
|
|
|
|
/// Then your catch sequence can look like the following:
|
|
|
|
///
|
|
|
|
/// catch(const my_specific_exception &e)
|
|
|
|
/// {
|
|
|
|
/// log("something specifically bad happened: %s", e.what());
|
|
|
|
/// }
|
|
|
|
/// catch(const my_exception &e)
|
|
|
|
/// {
|
|
|
|
/// log("something generically bad happened: %s", e.what());
|
|
|
|
/// }
|
|
|
|
/// catch(const ircd::exception &e)
|
|
|
|
/// {
|
|
|
|
/// log("unrelated bad happened: %s", e.what());
|
|
|
|
/// }
|
|
|
|
/// catch(const std::exception &e)
|
|
|
|
/// {
|
|
|
|
/// log("unhandled bad happened: %s", e.what());
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// Remember: the order of the catch blocks is important.
|
|
|
|
///
|
2016-08-15 04:03:25 +02:00
|
|
|
#define IRCD_EXCEPTION(parent, name) \
|
|
|
|
struct name \
|
|
|
|
:parent \
|
|
|
|
{ \
|
2017-03-18 01:37:40 +01:00
|
|
|
template<class... args> \
|
2017-03-31 00:46:40 +02:00
|
|
|
name(const char *const &fmt = " ", args&&... ap) noexcept \
|
2016-10-17 21:15:26 +02:00
|
|
|
:parent{generate_skip} \
|
2016-08-15 04:03:25 +02:00
|
|
|
{ \
|
2018-02-03 03:56:30 +01:00
|
|
|
generate(#name, fmt, ircd::va_rtti{std::forward<args>(ap)...}); \
|
2016-08-15 04:03:25 +02:00
|
|
|
} \
|
2016-10-17 21:15:26 +02:00
|
|
|
\
|
2017-03-31 00:46:40 +02:00
|
|
|
name(generate_skip_t) noexcept \
|
2016-10-17 21:15:26 +02:00
|
|
|
:parent{generate_skip} \
|
|
|
|
{ \
|
|
|
|
} \
|
|
|
|
}; \
|
2016-07-26 04:06:31 +02:00
|
|
|
|
2017-09-12 18:37:44 +02:00
|
|
|
/// Hides the name of the exception when generating a string
|
2016-08-15 04:03:25 +02:00
|
|
|
#define IRCD_EXCEPTION_HIDENAME(parent, name) \
|
|
|
|
struct name \
|
|
|
|
:parent \
|
|
|
|
{ \
|
2017-03-18 01:37:40 +01:00
|
|
|
template<class... args> \
|
2017-03-31 00:46:40 +02:00
|
|
|
name(const char *const &fmt = " ", args&&... ap) noexcept \
|
|
|
|
:parent{generate_skip} \
|
2016-08-15 04:03:25 +02:00
|
|
|
{ \
|
2018-02-03 03:56:30 +01:00
|
|
|
generate(fmt, ircd::va_rtti{std::forward<args>(ap)...}); \
|
2016-10-17 21:15:26 +02:00
|
|
|
} \
|
|
|
|
\
|
2017-03-31 00:46:40 +02:00
|
|
|
name(generate_skip_t = {}) noexcept \
|
2016-10-17 21:15:26 +02:00
|
|
|
:parent{generate_skip} \
|
|
|
|
{ \
|
2016-08-15 04:03:25 +02:00
|
|
|
} \
|
|
|
|
};
|
2016-07-26 04:06:31 +02:00
|
|
|
|
2018-01-08 22:26:29 +01:00
|
|
|
/// Creates an assertion-type exception.
|
|
|
|
///
|
|
|
|
/// Throwable exception which will terminate on construction in debug mode
|
|
|
|
/// but throw normally in release mode. Ideally this should never be thrown
|
|
|
|
/// in release mode because the termination in debug means a test can never
|
|
|
|
/// pass and the triggering callsite should be eliminated. Nevertheless it
|
|
|
|
/// throws normally in release mode.
|
|
|
|
#define IRCD_ASSERTION(parent, name) \
|
|
|
|
struct name \
|
|
|
|
:parent \
|
|
|
|
{ \
|
|
|
|
template<class... args> \
|
2018-01-27 06:49:41 +01:00
|
|
|
name(const char *const &fmt = " ", args&&... ap) noexcept(RB_DEBUG_LEVEL) \
|
2018-01-08 22:26:29 +01:00
|
|
|
:parent{generate_skip} \
|
|
|
|
{ \
|
2018-02-03 03:56:30 +01:00
|
|
|
generate(#name, fmt, ircd::va_rtti{std::forward<args>(ap)...}); \
|
2018-01-08 22:26:29 +01:00
|
|
|
ircd::assertion(*this); \
|
|
|
|
} \
|
|
|
|
\
|
2018-01-27 06:49:41 +01:00
|
|
|
name(generate_skip_t) noexcept(RB_DEBUG_LEVEL) \
|
2018-01-08 22:26:29 +01:00
|
|
|
:parent{generate_skip} \
|
|
|
|
{ \
|
|
|
|
} \
|
|
|
|
}; \
|
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
namespace ircd
|
|
|
|
{
|
2017-10-05 03:10:00 +02:00
|
|
|
/// Root error exception type. Inherit from this.
|
|
|
|
/// List your own exception somewhere else (unless you're overhauling libircd).
|
|
|
|
/// example, in your namespace:
|
|
|
|
///
|
|
|
|
/// IRCD_EXCEPTION(ircd::error, error)
|
|
|
|
///
|
2018-01-08 22:26:29 +01:00
|
|
|
IRCD_EXCEPTION(exception, error) // throw ircd::error("something bad")
|
2017-08-28 23:51:22 +02:00
|
|
|
IRCD_EXCEPTION(error, user_error) // throw ircd::user_error("something silly")
|
2018-01-08 22:26:29 +01:00
|
|
|
|
|
|
|
// Assertion errors; see IRCD_ASSERTION docs.
|
|
|
|
IRCD_ASSERTION(exception, assertive)
|
|
|
|
IRCD_ASSERTION(assertive, not_implemented)
|
2017-08-28 23:51:22 +02:00
|
|
|
}
|
2018-03-08 18:21:16 +01:00
|
|
|
|
|
|
|
template<class... args>
|
|
|
|
void
|
|
|
|
ircd::throw_system_error(args&&... a)
|
|
|
|
{
|
|
|
|
throw std::system_error
|
|
|
|
{
|
|
|
|
make_error_code(std::forward<args>(a)...)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class... args>
|
|
|
|
std::exception_ptr
|
|
|
|
ircd::make_system_eptr(args&&... a)
|
|
|
|
{
|
|
|
|
return std::make_exception_ptr(make_system_error(std::forward<args>(a)...));
|
|
|
|
}
|