Date: Thursday, January 12, 2006 @ 19:55:10
Author: marc
Path: /cvsroot/carob/carob
Modified: include/CarobException.hpp (1.28 -> 1.29)
include/StringCodecs.hpp (1.4 -> 1.5) src/StringCodecs.cpp (1.4
-> 1.5) test/CarobProtector.hpp (1.10 -> 1.11)
Implemented a decent CodecException class and used it. CAROB-50
----------------------------+
include/CarobException.hpp | 13 ++++++++++++
include/StringCodecs.hpp | 6 -----
src/StringCodecs.cpp | 45 ++++++++++++++++++++++++-------------------
test/CarobProtector.hpp | 7 ++++++
4 files changed, 47 insertions(+), 24 deletions(-)
Index: carob/include/CarobException.hpp
diff -u carob/include/CarobException.hpp:1.28
carob/include/CarobException.hpp:1.29
--- carob/include/CarobException.hpp:1.28 Thu Jan 12 18:05:05 2006
+++ carob/include/CarobException.hpp Thu Jan 12 19:55:10 2006
@@ -226,6 +226,19 @@
};
/**
+ * Exception thrown when an encoding error occurs.
+ */
+class CodecException : public DriverException
+{
+public:
+ /**
+ * Constructs a CodecException with the given message
+ * @param s error message of the exception
+ */
+ CodecException(std::wstring s) : DriverException(s) {};
+};
+
+/**
* Exception thrown when client tries to retrieve a value that is NULL,
* typically via GetString() GetInt(), etc.
*/
Index: carob/include/StringCodecs.hpp
diff -u carob/include/StringCodecs.hpp:1.4 carob/include/StringCodecs.hpp:1.5
--- carob/include/StringCodecs.hpp:1.4 Wed Dec 21 13:05:44 2005
+++ carob/include/StringCodecs.hpp Thu Jan 12 19:55:10 2006
@@ -24,14 +24,10 @@
#include <locale>
-
+#include "CarobException.hpp"
namespace CarobNS {
-// TODO: get some features, at least a what().
-// Derive from CarobException or overkill?
-class CodecException {};
-
typedef std::codecvt<wchar_t, char, mbstate_t> mbs_codecvt;
typedef std::codecvt_byname<wchar_t, char, mbstate_t> mbs_codecvt_byname;
Index: carob/src/StringCodecs.cpp
diff -u carob/src/StringCodecs.cpp:1.4 carob/src/StringCodecs.cpp:1.5
--- carob/src/StringCodecs.cpp:1.4 Fri Dec 30 18:25:11 2005
+++ carob/src/StringCodecs.cpp Thu Jan 12 19:55:10 2006
@@ -19,15 +19,26 @@
* Contributor(s):
*/
+#include "StringCodecs.hpp"
+
#include <stdexcept>
+#include <sstream>
-#include <iostream> // TODO: remove me once we have decent exceptions
+#include "Common.hpp"
-#include "StringCodecs.hpp"
+using namespace CarobNS;
+namespace {
-using namespace CarobNS;
+std::wstring
+exception_message(const char *what, const char *locname)
+{
+ std::wstring msg(fromString(what));
+ msg += fromString(", locale name was: ") + fromString(locname);
+ return msg;
+}
+}
/**
* Catching std::runtime_error from std::codecvt_byname() segfaults
@@ -41,10 +52,8 @@
{
try {
return new facet_byname_t(name);
- } catch (std::runtime_error re) {
- std::cerr << re.what() << std::endl;
- std::cerr << "locale name was: " << name << std::endl;
- exit(912);
+ } catch (std::runtime_error& re) {
+ throw CodecException(exception_message(re.what(), name));
}
}
}
@@ -54,10 +63,8 @@
{
try {
return std::locale(name);
- } catch (std::runtime_error re) {
- std::cerr << re.what() << std::endl;
- std::cerr << "locale name was: " << name << std::endl;
- throw CodecException();
+ } catch (std::runtime_error& re) {
+ throw CodecException(exception_message(re.what(), name));
}
}
@@ -81,11 +88,11 @@
if (success != std::codecvt_base::ok) // codecvt_base::noconv is not
possible
{
- // TODO something sensible here: messages in the exception instead of
cerr
- std::cerr << "Error " << success << " in "
- << loc.name() << " encode method" << std::endl;
+ std::wostringstream woss;
+ woss << L"Error " << success << L" in "
+ << fromString(loc.name()) << L" encoding method";
delete[] res_buf;
- throw CodecException();
+ throw CodecException(woss.str());
}
std::string retStr(res_buf, res_next-res_buf);
delete[] res_buf;
@@ -110,11 +117,11 @@
if (success != std::codecvt_base::ok) // codecvt_base::noconv is not
possible
{
- // TODO something sensible here: put messages in the exception instead
of cerr
- std::cerr << "Error " << success << " in "
- << loc.name() << " decode method" << std::endl;
+ std::wostringstream woss;
+ woss << L"Error " << success << L" in "
+ << fromString(loc.name()) << L" decoding method";
delete[] wres_buf;
- throw CodecException();
+ throw CodecException(woss.str());
}
std::wstring retWStr(wres_buf, wres_next-wres_buf);
delete[] wres_buf;
Index: carob/test/CarobProtector.hpp
diff -u carob/test/CarobProtector.hpp:1.10 carob/test/CarobProtector.hpp:1.11
--- carob/test/CarobProtector.hpp:1.10 Fri Dec 2 15:53:07 2005
+++ carob/test/CarobProtector.hpp Thu Jan 12 19:55:10 2006
@@ -103,6 +103,13 @@
// CPPunit report
reportCppUnitError(context, "ControllerException:", ce);
}
+ catch (const CarobNS::CodecException& ce)
+ {
+ // log
+ cerrCarobExceptionChain(COPREFIX, ce);
+ // CPPunit report
+ reportCppUnitError(context, "CodecException:", ce);
+ }
catch (const CarobNS::UnexpectedException& ue)
{
// log
_______________________________________________
Carob-commits mailing list
[email protected]
https://forge.continuent.org/mailman/listinfo/carob-commits