summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2016-04-12 22:18:36 +0200
committerMax Kellermann <max@duempel.org>2016-04-13 10:04:19 +0200
commit01b68db30e6b4e9e036e0ae1adcdc2a35d102e43 (patch)
treed906c65e052c78396ee54c6e8b43954e0ac6de83 /src/lib
parent33fdaa5b6d66a9a296e8ba170098b10f24be523b (diff)
lib/icu/Converter: Create() throws exception on error
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/icu/Converter.cxx25
-rw-r--r--src/lib/icu/Converter.hxx7
2 files changed, 15 insertions, 17 deletions
diff --git a/src/lib/icu/Converter.cxx b/src/lib/icu/Converter.cxx
index 819a1c9e3..cb818d375 100644
--- a/src/lib/icu/Converter.cxx
+++ b/src/lib/icu/Converter.cxx
@@ -19,12 +19,13 @@
#include "config.h"
#include "Converter.hxx"
-#include "Error.hxx"
-#include "util/Error.hxx"
#include "util/Macros.hxx"
#include "util/AllocatedString.hxx"
#include "util/AllocatedArray.hxx"
#include "util/ConstBuffer.hxx"
+#include "util/FormatString.hxx"
+
+#include <stdexcept>
#include <string.h>
@@ -32,8 +33,7 @@
#include "Util.hxx"
#include <unicode/ucnv.h>
#elif defined(HAVE_ICONV)
-#include "util/Domain.hxx"
-static constexpr Domain iconv_domain("iconv");
+#include "system/Error.hxx"
#endif
#ifdef HAVE_ICU
@@ -48,30 +48,27 @@ IcuConverter::~IcuConverter()
#ifdef HAVE_ICU_CONVERTER
IcuConverter *
-IcuConverter::Create(const char *charset, Error &error)
+IcuConverter::Create(const char *charset)
{
#ifdef HAVE_ICU
UErrorCode code = U_ZERO_ERROR;
UConverter *converter = ucnv_open(charset, &code);
- if (converter == nullptr) {
- error.Format(icu_domain, int(code),
- "Failed to initialize charset '%s': %s",
- charset, u_errorName(code));
- return nullptr;
- }
+ if (converter == nullptr)
+ throw std::runtime_error(FormatString("Failed to initialize charset '%s': %s",
+ charset, u_errorName(code)).c_str());
return new IcuConverter(converter);
#elif defined(HAVE_ICONV)
iconv_t to = iconv_open("utf-8", charset);
iconv_t from = iconv_open(charset, "utf-8");
if (to == (iconv_t)-1 || from == (iconv_t)-1) {
- error.FormatErrno("Failed to initialize charset '%s'",
- charset);
+ int e = errno;
if (to != (iconv_t)-1)
iconv_close(to);
if (from != (iconv_t)-1)
iconv_close(from);
- return nullptr;
+ throw FormatErrno(e, "Failed to initialize charset '%s'",
+ charset);
}
return new IcuConverter(to, from);
diff --git a/src/lib/icu/Converter.hxx b/src/lib/icu/Converter.hxx
index bbe25ead6..59b1e5b5a 100644
--- a/src/lib/icu/Converter.hxx
+++ b/src/lib/icu/Converter.hxx
@@ -33,8 +33,6 @@
#ifdef HAVE_ICU_CONVERTER
-class Error;
-
#ifdef HAVE_ICU
struct UConverter;
#endif
@@ -73,7 +71,10 @@ public:
}
#endif
- static IcuConverter *Create(const char *charset, Error &error);
+ /**
+ * Throws std::runtime_error on error.
+ */
+ static IcuConverter *Create(const char *charset);
/**
* Convert the string to UTF-8.