From 7b17adba08b53d6c82f2defd15261faa663c0d9b Mon Sep 17 00:00:00 2001 From: Christoph Jentzsch Date: Tue, 30 Sep 2014 06:51:20 +0200 Subject: [PATCH] Exemplar alteration to show effect of using boost::exception --- alethzero/MainWin.cpp | 8 +++++++- libdevcore/Exceptions.h | 17 ++++++++++++++--- libdevcore/RLP.cpp | 8 ++++---- libethcore/BlockInfo.cpp | 13 +++++++++++++ 4 files changed, 38 insertions(+), 8 deletions(-) diff --git a/alethzero/MainWin.cpp b/alethzero/MainWin.cpp index b4bbb8ca7..76a2f9077 100644 --- a/alethzero/MainWin.cpp +++ b/alethzero/MainWin.cpp @@ -776,7 +776,13 @@ static bool blockMatch(string const& _f, dev::eth::BlockDetails const& _b, h256 if (_f.size() > 1 && _f.size() < 10 && _f[0] == '#' && stoul(_f.substr(1)) == _b.number) return true; } - catch (...) {} + catch (...) + { + std::cerr << "Unhandled exception!" << std::endl << + boost::current_exception_diagnostic_information(); + // possible output would include: function name, __FILE__, __LINE__ (at throw) and all added information, + // such has the block header info added in BlockInfo.cpp line 101-102. + } if (toHex(_h.ref()).find(_f) != string::npos) return true; BlockInfo bi(_bc.block(_h)); diff --git a/libdevcore/Exceptions.h b/libdevcore/Exceptions.h index 15ef2019f..c1d65badc 100644 --- a/libdevcore/Exceptions.h +++ b/libdevcore/Exceptions.h @@ -22,6 +22,8 @@ #pragma once #include +#include +#include #include "CommonIO.h" #include "CommonData.h" #include "FixedHash.h" @@ -36,11 +38,20 @@ public: virtual char const* what() const noexcept { return typeid(*this).name(); } }; -class BadHexCharacter: public Exception {}; +// As an exemplar case I only restructure BadRLP, if I would restrucutre everything the above Exception class +// can be replaced completely. + +struct BException: virtual boost::exception, virtual std::exception {}; + +// there is no need to derive from any other class then BException just to add more information. +// This can be done dynamically during runtime. -class RLPException: public Exception {}; +struct BadRLP: virtual BException {}; + + +class BadHexCharacter: public Exception {}; +class RLPException: public BException {}; class BadCast: public RLPException {}; -class BadRLP: public RLPException {}; class NoNetworking: public Exception {}; class NoUPnPDevice: public Exception {}; class RootNotFound: public Exception {}; diff --git a/libdevcore/RLP.cpp b/libdevcore/RLP.cpp index 68ae02065..79f586fa1 100644 --- a/libdevcore/RLP.cpp +++ b/libdevcore/RLP.cpp @@ -104,13 +104,13 @@ bool RLP::isInt() const else if (n <= c_rlpDataIndLenZero) { if (m_data.size() <= 1) - throw BadRLP(); + BOOST_THROW_EXCEPTION(BadRLP()); return m_data[1] != 0; } else if (n < c_rlpListStart) { if ((int)m_data.size() <= 1 + n - c_rlpDataIndLenZero) - throw BadRLP(); + BOOST_THROW_EXCEPTION(BadRLP()); return m_data[1 + n - c_rlpDataIndLenZero] != 0; } else @@ -131,7 +131,7 @@ unsigned RLP::length() const else if (n < c_rlpListStart) { if ((int)m_data.size() <= n - c_rlpDataIndLenZero) - throw BadRLP(); + BOOST_THROW_EXCEPTION(BadRLP()); for (int i = 0; i < n - c_rlpDataIndLenZero; ++i) ret = (ret << 8) | m_data[i + 1]; } @@ -140,7 +140,7 @@ unsigned RLP::length() const else { if ((int)m_data.size() <= n - c_rlpListIndLenZero) - throw BadRLP(); + BOOST_THROW_EXCEPTION(BadRLP()); for (int i = 0; i < n - c_rlpListIndLenZero; ++i) ret = (ret << 8) | m_data[i + 1]; } diff --git a/libethcore/BlockInfo.cpp b/libethcore/BlockInfo.cpp index 9ceb4c708..0ea2ff5f7 100644 --- a/libethcore/BlockInfo.cpp +++ b/libethcore/BlockInfo.cpp @@ -91,6 +91,19 @@ void BlockInfo::populateFromHeader(RLP const& _header, bool _checkNonce) extraData = _header[field = 11].toBytes(); nonce = _header[field = 12].toHash(); } + catch (BException const& e) + { + // define error information to be added to the exception + typedef boost::error_info InvalidBlockHeaderFormat_field; + typedef boost::error_info InvalidBlockHeaderFormat_header_field_data; + + // instead of using a new exception type, we add information to the existing exception + e << InvalidBlockHeaderFormat_field(field); + e << InvalidBlockHeaderFormat_header_field_data(toHex(_header[field].data().toBytes())); + + throw; + } + // this block would be replaced catch (RLPException const&) { throw InvalidBlockHeaderFormat(field, _header[field].data());