Browse Source

Exemplar alteration to show effect of using boost::exception

cl-refactor
Christoph Jentzsch 10 years ago
parent
commit
7b17adba08
  1. 8
      alethzero/MainWin.cpp
  2. 17
      libdevcore/Exceptions.h
  3. 8
      libdevcore/RLP.cpp
  4. 13
      libethcore/BlockInfo.cpp

8
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));

17
libdevcore/Exceptions.h

@ -22,6 +22,8 @@
#pragma once
#include <exception>
#include <boost/exception/all.hpp>
#include <boost/throw_exception.hpp>
#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 {};

8
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];
}

13
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<h256>();
}
catch (BException const& e)
{
// define error information to be added to the exception
typedef boost::error_info<struct field_info,int> InvalidBlockHeaderFormat_field;
typedef boost::error_info<struct header_field_data,string> 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());

Loading…
Cancel
Save