]> git.tdb.fi Git - libs/core.git/commitdiff
Add new exception classes for some common errors
authorMikko Rasa <tdb@tdb.fi>
Tue, 6 Dec 2022 23:02:22 +0000 (01:02 +0200)
committerMikko Rasa <tdb@tdb.fi>
Tue, 6 Dec 2022 23:03:49 +0000 (01:03 +0200)
23 files changed:
source/core/application.cpp
source/core/except.h [new file with mode: 0644]
source/core/process.cpp
source/core/thread.cpp
source/core/unix/process.cpp
source/core/windows/process.cpp
source/fs/dir.cpp
source/fs/filemonitor.cpp
source/fs/osx/dir_location.cpp
source/fs/unix/dir_location.cpp
source/fs/windows/dir_location.cpp
source/fs/windows/filemonitor.cpp
source/fs/windows/utils.cpp
source/io/asset.cpp
source/io/buffered.cpp
source/io/file.cpp
source/io/memory.cpp
source/io/poll.cpp
source/io/slice.cpp
source/io/windows/console.cpp
source/io/zlibcompressed.cpp
source/stringcodec/iso2022jp.cpp
source/strings/regex.cpp

index 8447eed669d2b8f58c9b078b547b1543b0bab27f..1b77a7c11273daadcf5c080fa77c369fe57ed65f 100644 (file)
@@ -9,6 +9,7 @@
 #include <msp/io/print.h>
 #include <msp/strings/utils.h>
 #include "application.h"
+#include "except.h"
 #include "getopt.h"
 
 using namespace std;
@@ -24,7 +25,7 @@ void *Application::_data = nullptr;
 Application::Application(const string &n)
 {
        if(_app)
-               throw logic_error("instance already exists");
+               throw already_called("Application::Application");
 
        if(!n.empty())
                _name = n;
@@ -94,7 +95,7 @@ int Application::run(int argc, char **argv, void *data, void (*created_callback)
 void Application::set_startup_info(const char *argv0, void *data)
 {
        if(_argv0)
-               throw logic_error("startup info already set");
+               throw already_called("Application::set_startup_info");
 
        static FS::Path exe;
 
@@ -146,7 +147,7 @@ void Application::_sighandler(int s)
 Application::Starter::Starter()
 {
        if(_starter)
-               throw logic_error("Can't create more than one Starter instance");
+               throw already_called("Application::Starter::Starter");
 
        _starter = this;
 }
diff --git a/source/core/except.h b/source/core/except.h
new file mode 100644 (file)
index 0000000..65d9ef9
--- /dev/null
@@ -0,0 +1,37 @@
+#ifndef MSP_CORE_EXCEPT_H_
+#define MSP_CORE_EXCEPT_H_
+
+#include <stdexcept>
+
+namespace Msp {
+
+class invalid_state: public std::logic_error
+{
+public:
+       invalid_state(const std::string &w): logic_error(w) { }
+};
+
+
+class already_called: public invalid_state
+{
+public:
+       already_called(const std::string &w): invalid_state(w) { }
+};
+
+
+class unsupported: public std::logic_error
+{
+public:
+       unsupported(const std::string &w): logic_error(w) { }
+};
+
+
+class internal_error: public std::logic_error
+{
+public:
+       internal_error(const std::string &w): logic_error(w) { }
+};
+
+} // namespace Msp;
+
+#endif
index 43b7384a5a3d66d49fd708e45d7cd274795f04ac..4c5ed4651164615fc67b1d8a7c7523856876a233 100644 (file)
@@ -1,4 +1,5 @@
 #include <msp/io/console.h>
+#include "except.h"
 #include "process.h"
 #include "process_private.h"
 
@@ -78,7 +79,7 @@ void Process::execute(const FS::Path &command, const Arguments &args)
 unsigned Process::get_exit_code() const
 {
        if(!finished)
-               throw logic_error("not finished");
+               throw invalid_state("not finished");
        return exit_code;
 }
 
index 841cc3f48e8111a894dc9151268fce3858dcd4f0..ad78d945cbd85e995d954ba6411964ccaa702421 100644 (file)
@@ -1,4 +1,4 @@
-#include <stdexcept>
+#include "except.h"
 #include "thread.h"
 #include "thread_private.h"
 
@@ -39,7 +39,7 @@ void Thread::kill()
 void Thread::launch()
 {
        if(_state>=RUNNING)
-               throw logic_error("already launched");
+               throw already_called("Thread::launch");
 
        platform_launch();
        _state = RUNNING;
index 7370b70803f94bff6c24c57a3adb453f5ea6682a..975fa223520486fd59122ae90e17e322fd4e755a 100644 (file)
@@ -4,6 +4,7 @@
 #include <msp/core/systemerror.h>
 #include <msp/fs/dir.h>
 #include <msp/io/console.h>
+#include "except.h"
 #include "process.h"
 #include "process_private.h"
 
@@ -70,7 +71,7 @@ void Process::execute(const string &command, bool path_search, const Arguments &
 bool Process::wait(bool block)
 {
        if(!running)
-               throw logic_error("not running");
+               throw invalid_state("not running");
 
        int status;
        int pid = waitpid(priv->info.pid, &status, (block ? 0 : WNOHANG));
index 3bd03fa3816dbefc3faa71726fa1632c5537cdcd..aa0fad94e3699a6383628ef5c970124350673fdf 100644 (file)
@@ -2,6 +2,7 @@
 #include <msp/core/systemerror.h>
 #include <msp/io/handle_private.h>
 #include <msp/strings/utils.h>
+#include "except.h"
 #include "process.h"
 #include "process_private.h"
 
@@ -99,7 +100,7 @@ void Process::execute(const string &command, bool path_search, const Arguments &
 bool Process::wait(bool block)
 {
        if(!running)
-               throw logic_error("not running");
+               throw invalid_state("not running");
 
        DWORD ret = WaitForSingleObject(priv->info.hProcess, (block ? INFINITE : 0));
        if(ret==WAIT_FAILED)
index 066d88bc1fd2c461bae39b5e3700d756f2aaa5b6..0f617e58717b984db918eb51e714a8534c01a410 100644 (file)
@@ -1,5 +1,6 @@
 #include <msp/core/application.h>
 #include <msp/core/environ.h>
+#include <msp/core/except.h>
 #include <msp/strings/utils.h>
 #include "dir.h"
 #include "path.h"
@@ -109,7 +110,7 @@ Path get_sys_conf_dir()
 {
        const char *argv0 = Application::get_argv0();
        if(!argv0)
-               throw logic_error("no startup command");
+               throw invalid_state("no startup command");
 
        Path dir = get_bin_dir(argv0);
 
@@ -128,7 +129,7 @@ Path get_sys_data_dir()
 {
        const char *argv0 = Application::get_argv0();
        if(!argv0)
-               throw logic_error("no startup command");
+               throw invalid_state("no startup command");
 
        Path dir = get_bin_dir(argv0);
 
@@ -144,7 +145,7 @@ Path get_sys_lib_dir()
 {
        const char *argv0 = Application::get_argv0();
        if(!argv0)
-               throw logic_error("no startup command");
+               throw invalid_state("no startup command");
 
        Path dir = get_bin_dir(argv0);
 
index 5ab3f49197a0169c18b0df5a786be1064df8f94f..5ff6392a352fe4e06af3e97f0371d1b19d67c10b 100644 (file)
@@ -1,4 +1,5 @@
 #include <msp/core/algorithm.h>
+#include <msp/core/except.h>
 #include "filemonitor.h"
 #include "filemonitor_platform.h"
 
@@ -19,7 +20,7 @@ FileMonitor::~FileMonitor()
 void FileMonitor::use_event_dispatcher(IO::EventDispatcher &ed)
 {
        if(event_disp)
-               throw logic_error("event_disp!=nullptr");
+               throw already_called("FileMonitor::use_event_dispatcher");
 
        event_disp = &ed;
        platform_use_event_dispatcher();
index b58b971961e25cdb83db4abe9f51e31c917c0fef..13aa3d2d500fd167153f16b4811f575004e517af 100644 (file)
@@ -1,5 +1,6 @@
 #include <msp/core/application.h>
 #include <msp/core/environ.h>
+#include <msp/core/except.h>
 #include "dir.h"
 
 using namespace std;
@@ -28,7 +29,7 @@ Path get_user_data_dir()
 {
        const string &appname = Application::get_name();
        if(appname.empty())
-               throw logic_error("no application name");
+               throw invalid_state("no application name");
 
        char buf[1024];
        unsigned len = get_application_support_dir(buf, sizeof(buf));
index 73825875fd4df979ae1b7c5fff619af6982fb133..68d82a27f968c37d14c1e2c42aafe1814e679570 100644 (file)
@@ -1,5 +1,6 @@
 #include <msp/core/application.h>
 #include <msp/core/environ.h>
+#include <msp/core/except.h>
 #include "dir.h"
 
 using namespace std;
@@ -19,7 +20,7 @@ Path get_user_data_dir()
 {
        const string &appname = Application::get_name();
        if(appname.empty())
-               throw logic_error("no application name");
+               throw invalid_state("no application name");
 
        return get_home_dir()/("."+appname);
 }
index 4f9b8f643b723b9317af370a84dbc8b7e6529b8e..85c5c2fa30d1f6809c359cfb7f919605d83fedfb 100644 (file)
@@ -1,5 +1,6 @@
 #include <shlobj.h>
 #include <msp/core/application.h>
+#include <msp/core/except.h>
 #include "dir.h"
 
 using namespace std;
@@ -19,7 +20,7 @@ Path get_user_data_dir()
 {
        const string &appname = Application::get_name();
        if(appname.empty())
-               throw logic_error("no application name");
+               throw invalid_state("no application name");
 
        char datadir[MAX_PATH];
        if(SHGetFolderPath(nullptr, CSIDL_LOCAL_APPDATA, nullptr, 0, datadir)==S_OK)
index 3ed65b188aeb0d043e46d9a1a609572eaa2de055..da0bca3a65566df44e3256313f31ab9e7363ecf6 100644 (file)
@@ -21,7 +21,7 @@ void FileMonitor::tick()
 
 FileMonitor::Private::Private(FileMonitor &)
 {
-       throw logic_error("not implemented");
+       throw unsupported("FileMonitor");
 }
 
 } // namespace FS
index ca9e7aa0f11f333f6710b93f48c1f86b3e62e7f9..66699aaf0bc2229d3782116539e50213b90214d1 100644 (file)
@@ -11,7 +11,7 @@ namespace FS {
 Path readlink(const Path &link)
 {
        (void)link;
-       throw logic_error("no symbolic links on win32");
+       throw unsupported("no symbolic links on win32");
 }
 
 Path realpath(const Path &path)
index 9284f97107f9e19a2eacfb121ddcd43c0c09e3b3..d605ff49573cb2e329513ea2d722023945de302b 100644 (file)
@@ -1,3 +1,4 @@
+#include <msp/core/except.h>
 #include "asset.h"
 
 using namespace std;
@@ -7,12 +8,12 @@ namespace IO {
 
 void Asset::set_block(bool)
 {
-       throw logic_error("Asset::set_block");
+       throw unsupported("Asset::set_block");
 }
 
 void Asset::set_inherit(bool)
 {
-       throw logic_error("Asset::set_inherit");
+       throw unsupported("Asset::set_inherit");
 }
 
 size_t Asset::do_write(const char *, size_t)
@@ -22,7 +23,7 @@ size_t Asset::do_write(const char *, size_t)
 
 const Handle &Asset::get_handle(Mode)
 {
-       throw logic_error("Asset::get_handle");
+       throw unsupported("Asset::get_handle");
 }
 
 } // namespace IO
index 638e4ec3696a77a71e7f516ccdd0ccf6033abdc3..9f001fbe9ae94b944611f368416799ceab836a04 100644 (file)
@@ -1,5 +1,5 @@
 #include <cstring>
-#include <stdexcept>
+#include <msp/core/except.h>
 #include "buffered.h"
 #include "handle.h"
 
@@ -33,12 +33,12 @@ Buffered::~Buffered()
 
 void Buffered::set_block(bool)
 {
-       throw logic_error("Buffered::set_block");
+       throw unsupported("Buffered::set_block");
 }
 
 void Buffered::set_inherit(bool)
 {
-       throw logic_error("Buffered::set_block");
+       throw unsupported("Buffered::set_block");
 }
 
 void Buffered::flush()
@@ -179,7 +179,7 @@ int Buffered::get()
 
 const Handle &Buffered::get_handle(Mode)
 {
-       throw logic_error("Buffered::get_handle");
+       throw unsupported("Buffered::get_handle");
 }
 
 void Buffered::set_op(Mode op)
index 5ead278813d7bd73e8c9eae38282e8b07f1d0bd4..7d6e1f9b8df12feee60d3eae53b532db13ae55e0 100644 (file)
@@ -1,3 +1,4 @@
+#include <msp/core/except.h>
 #include "file.h"
 #include "handle_private.h"
 
@@ -104,7 +105,7 @@ void BufferedFile::set_block(bool b)
 
 void BufferedFile::set_inherit(bool)
 {
-       throw logic_error("BufferedFile::set_inherit");
+       throw unsupported("BufferedFile::set_inherit");
 }
 
 size_t BufferedFile::do_write(const char *buf, size_t size)
@@ -146,7 +147,7 @@ int BufferedFile::get()
 
 const Handle &BufferedFile::get_handle(Mode)
 {
-       throw logic_error("BufferedFile::get_handle");
+       throw unsupported("BufferedFile::get_handle");
 }
 
 SeekOffset BufferedFile::seek(SeekOffset offset, SeekType type)
index 0b2d8f4c1b7f49ec7ccb93222d2bf48915abf3fd..19629c446d6c8bfc5e25288e8580deeadbe68c5c 100644 (file)
@@ -1,5 +1,6 @@
 #include <algorithm>
 #include <cstring>
+#include <msp/core/except.h>
 #include "handle.h"
 #include "memory.h"
 
@@ -18,12 +19,12 @@ Memory::Memory(char *b, char *e, Mode m)
 
 void Memory::set_block(bool)
 {
-       throw logic_error("Memory::set_block");
+       throw unsupported("Memory::set_block");
 }
 
 void Memory::set_inherit(bool)
 {
-       throw logic_error("Memory::set_inherit");
+       throw unsupported("Memory::set_inherit");
 }
 
 size_t Memory::do_write(const char *buf, size_t size)
@@ -94,7 +95,7 @@ int Memory::get()
 
 const Handle &Memory::get_handle(Mode)
 {
-       throw logic_error("Memory::get_handle");
+       throw unsupported("Memory::get_handle");
 }
 
 SeekOffset Memory::seek(SeekOffset off, SeekType type)
index 4f9b24f8e6340b8111c6a45824b60cecf336216c..cfaa57bde68f0265a5c8d50cde1dd8c78840616e 100644 (file)
@@ -44,7 +44,7 @@ void Poller::set_object(EventObject &obj, PollEvent ev)
 
 #ifdef _WIN32
        if(objects.size()>=MAXIMUM_WAIT_OBJECTS)
-               throw logic_error("Maximum number of wait objects reached");
+               throw invalid_state("Maximum number of wait objects reached");
 #endif
 
        objects.push_back(PolledObject(&obj, ev));
index 535deae1184bfdf7900375652e4faf8bede8473d..98013b60c4e4c5c1c086e62d621c8224e2de522f 100644 (file)
@@ -1,4 +1,4 @@
-#include <stdexcept>
+#include <msp/core/except.h>
 #include "slice.h"
 
 using namespace std;
@@ -21,12 +21,12 @@ Slice::Slice(Seekable &b, SeekOffset s, SeekOffset l):
 
 void Slice::set_block(bool)
 {
-       throw logic_error("Slice::set_block");
+       throw unsupported("Slice::set_block");
 }
 
 void Slice::set_inherit(bool)
 {
-       throw logic_error("Slice::set_inherit");
+       throw unsupported("Slice::set_inherit");
 }
 
 void Slice::flush()
@@ -108,7 +108,7 @@ int Slice::get()
 
 const Handle &Slice::get_handle(Mode)
 {
-       throw logic_error("Slice::get_handle");
+       throw unsupported("Slice::get_handle");
 }
 
 SeekOffset Slice::seek(SeekOffset off, SeekType type)
index a331133c2bf8bec39762021e9da8f96889aed192..5d68c23fe4dec484964ba4a5a1beee243bb294ae 100644 (file)
@@ -13,7 +13,7 @@ DWORD stream_to_sys(Msp::IO::Console::Stream stream)
        case Msp::IO::Console::CIN: return STD_INPUT_HANDLE;
        case Msp::IO::Console::COUT: return STD_OUTPUT_HANDLE;
        case Msp::IO::Console::CERR: return STD_ERROR_HANDLE;
-       default: throw invalid_argument("stream_to_sys");
+       default: throw internal_error("stream_to_sys");
        }
 }
 
index fba71eb06d5e8b9ced1df0e2e1ce1e8ba743c889..8896f147bf6b1ecb038e7739a275a4a7c5732a96 100644 (file)
@@ -1,6 +1,7 @@
 #ifdef WITH_ZLIB
 #include <zlib.h>
 #endif
+#include <msp/core/except.h>
 #include "zlibcompressed.h"
 
 using namespace std;
@@ -73,7 +74,7 @@ ZlibCompressed::ZlibCompressed(Base &b, Mode m, unsigned level):
        (void)buffer_size;
        (void)stream_end;
        (void)level;
-       throw zlib_error("unsupported", -1);
+       throw unsupported("ZlibCompressed");
 #endif
 }
 
@@ -96,12 +97,12 @@ ZlibCompressed::~ZlibCompressed()
 
 void ZlibCompressed::set_block(bool)
 {
-       throw logic_error("ZlibCompressed::set_block");
+       throw unsupported("ZlibCompressed::set_block");
 }
 
 void ZlibCompressed::set_inherit(bool)
 {
-       throw logic_error("ZlibCompressed::set_inherit");
+       throw unsupported("ZlibCompressed::set_inherit");
 }
 
 void ZlibCompressed::flush()
@@ -261,7 +262,7 @@ size_t ZlibCompressed::do_read(char *data, size_t size)
 
 const Handle &ZlibCompressed::get_handle(Mode)
 {
-       throw logic_error("ZlibCompressed::get_handle");
+       throw unsupported("ZlibCompressed::get_handle");
 }
 
 } // namespace IO
index 3332922ccda68e81d354ca5f4e25f35bc96f492c..93c50f489fb8486182a1f2b69711efea71d5e01e 100644 (file)
@@ -1,3 +1,4 @@
+#include <msp/core/except.h>
 #include "ascii.h"
 #include "iso2022jp.h"
 #include "jisx0201.h"
@@ -122,7 +123,7 @@ unichar Iso2022Jp::Decoder::decode_char(const string &str, string::const_iterato
                else if(dec)
                        return dec->decode_char(str, i);
                else
-                       throw logic_error("no sub-decoder");
+                       throw internal_error("no sub-decoder");
 
                if(result>=0)
                        return result;
index 915d33556f17cdd8d4f3a23c48e9a3f5b8343054..5307c6990286ba373291281fb4ee230ff81d2d07 100644 (file)
@@ -2,6 +2,7 @@
 #include <list>
 #include <stack>
 #include <vector>
+#include <msp/core/except.h>
 #include "format.h"
 #include "regex.h"
 
@@ -477,7 +478,7 @@ bool Regex::run(const string &str, const string::const_iterator &begin, vector<R
                                                input_consumed = true;
                                        }
                                        else
-                                               throw logic_error("invalid instruction in regex bytecode");
+                                               throw internal_error("invalid instruction in regex bytecode");
 
                                        if(match_result==negate_match)
                                                terminate = true;