2016-07-26 04:06:31 +02:00
|
|
|
/*
|
|
|
|
* charybdis: 21st Century IRCd
|
|
|
|
* exception.h: Exception root
|
|
|
|
*
|
|
|
|
* Copyright (C) 2016 Charybdis Development Team
|
|
|
|
* Copyright (C) 2016 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.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
|
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_EXCEPTION_H
|
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
namespace ircd
|
|
|
|
{
|
|
|
|
struct exception;
|
|
|
|
}
|
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 &) {}
|
|
|
|
///
|
|
|
|
/// 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:
|
|
|
|
/// catch(const std::exception &) {}
|
|
|
|
///
|
|
|
|
/// Remember: not all exceptions have to inherit from std::exception, but
|
|
|
|
/// those are rogue exceptions. We do not allow this. To be sure nothing
|
|
|
|
/// can possibly get through, add to the bottom:
|
|
|
|
/// catch(...) {}
|
|
|
|
///
|
|
|
|
/// Note: Prefer 'noexcept' instead of catch(...), noexcept is like an 'assert'
|
|
|
|
/// for exceptions, and the rogue can be found-out in testing.
|
|
|
|
///
|
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:
|
|
|
|
const char *what() const noexcept override
|
|
|
|
{
|
|
|
|
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
|
|
|
{ \
|
2017-03-18 01:37:40 +01:00
|
|
|
generate(#name, fmt, 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
|
|
|
{ \
|
2017-03-18 01:37:40 +01:00
|
|
|
generate(fmt, 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
|
|
|
|
2017-09-12 18:37:44 +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)
|
|
|
|
///
|
2017-08-28 23:51:22 +02:00
|
|
|
namespace ircd
|
|
|
|
{
|
|
|
|
IRCD_EXCEPTION(exception, error) // throw ircd::error("something bad")
|
|
|
|
IRCD_EXCEPTION(error, user_error) // throw ircd::user_error("something silly")
|
|
|
|
}
|