diff options
| author | 2022-01-07 17:12:42 +0000 | |
|---|---|---|
| committer | 2022-01-07 17:16:50 +0000 | |
| commit | 52cc8a418307ae7a551d23e6bd2d367b544e7bf9 (patch) | |
| tree | 9fff020964190f33174e78ff6201381be577d0b3 /include | |
| parent | Merge branch 'insp3' into master. (diff) | |
Refactor CoreException and ModuleException.
Diffstat (limited to 'include')
| -rw-r--r-- | include/base.h | 54 | ||||
| -rw-r--r-- | include/configreader.h | 2 | ||||
| -rw-r--r-- | include/dynref.h | 2 | ||||
| -rw-r--r-- | include/exception.h | 69 | ||||
| -rw-r--r-- | include/inspircd.h | 1 | ||||
| -rw-r--r-- | include/mode.h | 2 | ||||
| -rw-r--r-- | include/modules/dns.h | 8 | ||||
| -rw-r--r-- | include/modules/ldap.h | 10 | ||||
| -rw-r--r-- | include/modules/regex.h | 14 |
9 files changed, 93 insertions, 69 deletions
diff --git a/include/base.h b/include/base.h index 57084695b..a7d4e6c27 100644 --- a/include/base.h +++ b/include/base.h @@ -130,60 +130,6 @@ class reference final #endif }; -/** This class can be used on its own to represent an exception, or derived to represent a module-specific exception. - * When a module whishes to abort, e.g. within a constructor, it should throw an exception using ModuleException or - * a class derived from ModuleException. If a module throws an exception during its constructor, the module will not - * be loaded. If this happens, the error message returned by ModuleException::GetReason will be displayed to the user - * attempting to load the module, or dumped to the console if the ircd is currently loading for the first time. - */ -class CoreExport CoreException - : public std::exception -{ - protected: - /** Holds the error message to be displayed - */ - const std::string err; - /** Source of the exception - */ - const std::string source; - - public: - /** This constructor can be used to specify an error message before throwing. - * @param message Human readable error message - */ - CoreException(const std::string &message) : err(message), source("The core") {} - /** This constructor can be used to specify an error message before throwing, - * and to specify the source of the exception. - * @param message Human readable error message - * @param src Source of the exception - */ - CoreException(const std::string &message, const std::string &src) : err(message), source(src) {} - /** This destructor solves world hunger, cancels the world debt, and causes the world to end. - * Actually no, it does nothing. Never mind. - * @throws Nothing! - */ - virtual ~CoreException() noexcept = default; - /** Returns the reason for the exception. - * @return Human readable description of the error - */ - const std::string& GetReason() const { return err; } - - /** Returns the source of the exception - * @return Source of the exception - */ - const std::string& GetSource() const { return source; } -}; - -class Module; -class CoreExport ModuleException - : public CoreException -{ - public: - /** This constructor can be used to specify an error message before throwing. - */ - ModuleException(const std::string &message, Module* me = NULL); -}; - typedef const reference<Module> ModuleRef; enum ServiceType { diff --git a/include/configreader.h b/include/configreader.h index 6c4ce0403..9adcb1cfc 100644 --- a/include/configreader.h +++ b/include/configreader.h @@ -92,7 +92,7 @@ public: std::vector<const char*> enumkeys; std::transform(enumvals.begin(), enumvals.end(), std::back_inserter(enumkeys), [](const std::pair<const char*, TReturn>& ev) { return ev.first; }); - throw ModuleException(val + " is an invalid value for <" + name + ":" + key + ">; acceptable values are " + + throw CoreException(val + " is an invalid value for <" + name + ":" + key + ">; acceptable values are " + stdalgo::string::join(enumkeys, ' ') + ", at " + source.str()); } diff --git a/include/dynref.h b/include/dynref.h index 778653b9b..6c5d13bd3 100644 --- a/include/dynref.h +++ b/include/dynref.h @@ -62,7 +62,7 @@ class CoreExport dynamic_reference_base inline void dynamic_reference_base::check() { if (!value) - throw ModuleException("Dynamic reference to '" + name + "' failed to resolve. Are you missing a module?"); + throw ModuleException(creator, "Dynamic reference to '" + name + "' failed to resolve. Are you missing a module?"); } template<typename T> diff --git a/include/exception.h b/include/exception.h new file mode 100644 index 000000000..76e9732b5 --- /dev/null +++ b/include/exception.h @@ -0,0 +1,69 @@ +/* + * InspIRCd -- Internet Relay Chat Daemon + * + * Copyright (C) 2022 Sadie Powell <sadie@witchery.services> + * + * This file is part of InspIRCd. InspIRCd is free software: you can + * redistribute it and/or modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation, version 2. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + + +#pragma once + +/** The base class for InspIRCd exception types. */ +class CoreException + : public std::exception +{ + private: + /** The reason this exception was thrown. */ + const std::string reason; + + public: + /** Creates a new instance of the CoreException class with the specified reason. + * @param message A message that contains the reason this exception was thrown. + */ + CoreException(const std::string& message) + : reason(message) + { + } + + /** Retrieves a character array that contains the reason this exception was thrown. */ + const char* what() const noexcept override { return reason.c_str(); } + + /** Retrieves a string that contains the reason this exception was thrown. */ + const std::string& GetReason() const noexcept { return reason; } +}; + +class Module; + +/** An generic exception which was thrown by a module. */ +class CoreExport ModuleException + : public CoreException +{ + private: + /* The module which threw this exception. */ + const Module* module; + + public: + /** Creates a new instance of the ModuleException class with the specified module instance and reason. + * @param mod The module which threw this exception. + * @param message A message that contains the reason this exception was thrown. + */ + ModuleException(const Module* mod, const std::string& message) + : CoreException(message) + , module(mod) + { + } + + /** Retrieves the module which threw this exception. */ + const Module* GetModule() const noexcept { return module; } +}; diff --git a/include/inspircd.h b/include/inspircd.h index c4e4d2bdb..189692240 100644 --- a/include/inspircd.h +++ b/include/inspircd.h @@ -69,6 +69,7 @@ #include "typedefs.h" #include "convto.h" #include "stdalgo.h" +#include "exception.h" CoreExport extern InspIRCd* ServerInstance; diff --git a/include/mode.h b/include/mode.h index 85a987cb4..5b8ff4609 100644 --- a/include/mode.h +++ b/include/mode.h @@ -629,7 +629,7 @@ class CoreExport ModeParser final * @param mt The type of the mode to allocate the id for * @return The id */ - ModeHandler::Id AllocateModeId(ModeType mt); + ModeHandler::Id AllocateModeId(ModeHandler* mh); public: typedef std::vector<ListModeBase*> ListModeList; diff --git a/include/modules/dns.h b/include/modules/dns.h index 3769a4e32..4473d2be7 100644 --- a/include/modules/dns.h +++ b/include/modules/dns.h @@ -79,10 +79,14 @@ namespace DNS const int PORT = 53; - class Exception : public ModuleException + class Exception final + : public ModuleException { public: - Exception(const std::string& message) : ModuleException(message) { } + Exception(const Module* mod, const std::string& message) + : ModuleException(mod, message) + { + } }; struct Question diff --git a/include/modules/ldap.h b/include/modules/ldap.h index 760cc4ec2..5d11d89fb 100644 --- a/include/modules/ldap.h +++ b/include/modules/ldap.h @@ -20,13 +20,15 @@ typedef int LDAPQuery; +// XXX: This should be using ModuleException. class LDAPException final - : public ModuleException + : public CoreException { public: - LDAPException(const std::string& reason) : ModuleException(reason) { } - - virtual ~LDAPException() noexcept = default; + LDAPException(const std::string& msg) + : CoreException(msg) + { + } }; struct LDAPModification final diff --git a/include/modules/regex.h b/include/modules/regex.h index 72e62d807..89010a629 100644 --- a/include/modules/regex.h +++ b/include/modules/regex.h @@ -93,7 +93,7 @@ class Regex::SimpleEngine final /** @copydoc Regex::Engine::Create */ PatternPtr Create(const std::string& pattern, uint8_t options) const override { - return std::make_shared<PatternClass>(pattern, options); + return std::make_shared<PatternClass>(creator, pattern, options); } }; @@ -126,21 +126,23 @@ class Regex::Exception final { public: /** Initializes a new instance of the Regex::Exception class. + * @param mod The module which caused this exception to be thrown. * @param regex A regular expression which failed to compile. * @param error The error which occurred whilst compiling the regular expression. */ - Exception(const std::string& regex, const std::string& error) - : ModuleException("Error in regex '" + regex + "': " + error) + Exception(const Module* mod, const std::string& regex, const std::string& error) + : ModuleException(mod, "Error in regex '" + regex + "': " + error) { } /** Initializes a new instance of the Regex::Exception class. + * @param mod The module which caused this exception to be thrown. * @param regex A regular expression which failed to compile. * @param error The error which occurred whilst compiling the regular expression. * @param offset The offset at which the errror occurred. */ - Exception(const std::string& regex, const std::string& error, size_t offset) - : ModuleException("Error in regex '" + regex + "' at offset " + ConvToStr(offset) + ": " + error) + Exception(const Module* mod, const std::string& regex, const std::string& error, size_t offset) + : ModuleException(mod, "Error in regex '" + regex + "' at offset " + ConvToStr(offset) + ": " + error) { } }; @@ -190,7 +192,7 @@ inline Regex::PatternPtr Regex::Engine::CreateHuman(const std::string& pattern) size_t end = pattern.find_last_not_of("Ii"); if (!end || end == std::string::npos || pattern[end] != '/') - throw Exception(pattern, "Regex patterns must be terminated with a '/'!"); + throw Exception(creator, pattern, "Regex patterns must be terminated with a '/'!"); uint8_t options = Regex::OPT_NONE; for (const auto& flag : insp::iterator_range(pattern.begin() + end + 1, pattern.end())) |
