diff options
author | Jay Berkenbilt <ejb@ql.org> | 2018-08-14 01:48:02 +0200 |
---|---|---|
committer | Jay Berkenbilt <ejb@ql.org> | 2018-08-14 02:01:51 +0200 |
commit | b4bdc42b4fd627529e1c4a4636d1631254a2f26e (patch) | |
tree | 4a3526842ce36c08fef94ec7f60da585a505d6ce /libqpdf | |
parent | 164cbdde46d7ec6924782a60f346a6a465a79a26 (diff) | |
download | qpdf-b4bdc42b4fd627529e1c4a4636d1631254a2f26e.tar.zst |
New exception class QPDFSystemError (fixes #221)
Diffstat (limited to 'libqpdf')
-rw-r--r-- | libqpdf/QPDFSystemError.cc | 51 | ||||
-rw-r--r-- | libqpdf/QUtil.cc | 18 | ||||
-rw-r--r-- | libqpdf/build.mk | 1 |
3 files changed, 54 insertions, 16 deletions
diff --git a/libqpdf/QPDFSystemError.cc b/libqpdf/QPDFSystemError.cc new file mode 100644 index 00000000..094341cd --- /dev/null +++ b/libqpdf/QPDFSystemError.cc @@ -0,0 +1,51 @@ +#include <qpdf/QPDFSystemError.hh> +#include <qpdf/QUtil.hh> +#include <string.h> + +QPDFSystemError::QPDFSystemError(std::string const& description, + int system_errno) : + std::runtime_error(createWhat(description, system_errno)), + description(description), + system_errno(system_errno) +{ +} + +QPDFSystemError::~QPDFSystemError() throw () +{ +} + +std::string +QPDFSystemError::createWhat(std::string const& description, + int system_errno) +{ + std::string message; +#ifdef _MSC_VER + // "94" is mentioned in the MSVC docs, but it's still safe if the + // message is longer. strerror_s is a templated function that + // knows the size of buf and truncates. + char buf[94]; + if (strerror_s(buf, errno) != 0) + { + message = description + ": failed with an unknown error"; + } + else + { + message = description + ": " + buf; + } +#else + message = description + ": " + strerror(errno); +#endif + return message; +} + +std::string const& +QPDFSystemError::getDescription() const +{ + return this->description; +} + +int +QPDFSystemError::getErrno() const +{ + return this->system_errno; +} diff --git a/libqpdf/QUtil.cc b/libqpdf/QUtil.cc index e2bc0bac..56bac386 100644 --- a/libqpdf/QUtil.cc +++ b/libqpdf/QUtil.cc @@ -7,6 +7,7 @@ # include <qpdf/InsecureRandomDataProvider.hh> #endif #include <qpdf/SecureRandomDataProvider.hh> +#include <qpdf/QPDFSystemError.hh> #include <cmath> #include <iomanip> @@ -132,22 +133,7 @@ QUtil::unsigned_char_pointer(char const* str) void QUtil::throw_system_error(std::string const& description) { -#ifdef _MSC_VER - // "94" is mentioned in the MSVC docs, but it's still safe if the - // message is longer. strerror_s is a templated function that - // knows the size of buf and truncates. - char buf[94]; - if (strerror_s(buf, errno) != 0) - { - throw std::runtime_error(description + ": failed with an unknown error"); - } - else - { - throw std::runtime_error(description + ": " + buf); - } -#else - throw std::runtime_error(description + ": " + strerror(errno)); -#endif + throw QPDFSystemError(description, errno); } int diff --git a/libqpdf/build.mk b/libqpdf/build.mk index 528456f8..61ea4b2d 100644 --- a/libqpdf/build.mk +++ b/libqpdf/build.mk @@ -45,6 +45,7 @@ SRCS_libqpdf = \ libqpdf/QPDFObjectHandle.cc \ libqpdf/QPDFPageDocumentHelper.cc \ libqpdf/QPDFPageObjectHelper.cc \ + libqpdf/QPDFSystemError.cc \ libqpdf/QPDFTokenizer.cc \ libqpdf/QPDFWriter.cc \ libqpdf/QPDFXRefEntry.cc \ |