]> git.tdb.fi Git - libs/core.git/commitdiff
New system_error exception class
authorMikko Rasa <tdb@tdb.fi>
Sat, 28 May 2011 10:12:34 +0000 (13:12 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 28 May 2011 10:13:55 +0000 (13:13 +0300)
source/core/systemerror.cpp [new file with mode: 0644]
source/core/systemerror.h [new file with mode: 0644]

diff --git a/source/core/systemerror.cpp b/source/core/systemerror.cpp
new file mode 100644 (file)
index 0000000..6d1d876
--- /dev/null
@@ -0,0 +1,38 @@
+#ifdef WIN32
+#include <windows.h>
+#include <msp/strings/lexicalcast.h>
+#else
+#include <cerrno>
+#include <cstring>
+#endif
+#include "systemerror.h"
+
+using namespace std;
+
+namespace Msp {
+
+system_error::system_error(const string &w, int c):
+       runtime_error(w+": "+get_message(c)),
+       code_(c)
+{ }
+
+string system_error::get_message(int c)
+{
+#ifdef WIN32
+       if(c==-1)
+               c = GetLastError();
+
+       char msg[1024];
+       if(FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, 0, c, 0, msg, sizeof(msg), 0))
+               return msg;
+       else
+               return lexical_cast(c, Fmt().hex());
+#else
+       if(c==-1)
+               c = errno;
+
+       return strerror(c);
+#endif
+}
+
+} // namespace Msp
diff --git a/source/core/systemerror.h b/source/core/systemerror.h
new file mode 100644 (file)
index 0000000..4b7493d
--- /dev/null
@@ -0,0 +1,26 @@
+#ifndef MSP_CORE_SYSTEMERROR_H_
+#define MSP_CORE_SYSTEMERROR_H_
+
+#include <stdexcept>
+#include <string>
+
+namespace Msp {
+
+class system_error: public std::runtime_error
+{
+private:
+       int code_;
+
+public:
+       system_error(const std::string &, int = -1);
+       virtual ~system_error() throw() { }
+
+       int code() const throw() { return code_; }
+
+private:
+       static std::string get_message(int);
+};
+
+} // namespace Msp
+
+#endif