Exceptions.h 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. This file is part of cpp-ethereum.
  3. cpp-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. cpp-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file Exceptions.h
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2014
  17. */
  18. #pragma once
  19. #include <exception>
  20. #include <string>
  21. #include <boost/exception/exception.hpp>
  22. #include <boost/exception/info.hpp>
  23. #include <boost/exception/info_tuple.hpp>
  24. #include <boost/exception/diagnostic_information.hpp>
  25. #include <boost/throw_exception.hpp>
  26. #include <boost/tuple/tuple.hpp>
  27. #include "CommonData.h"
  28. #include "FixedHash.h"
  29. namespace dev
  30. {
  31. /// Base class for all exceptions.
  32. struct Exception: virtual std::exception, virtual boost::exception
  33. {
  34. Exception(std::string _message = std::string()): m_message(std::move(_message)) {}
  35. const char* what() const noexcept override { return m_message.empty() ? std::exception::what() : m_message.c_str(); }
  36. private:
  37. std::string m_message;
  38. };
  39. #define DEV_SIMPLE_EXCEPTION(X) struct X: virtual Exception { const char* what() const noexcept override { return #X; } }
  40. /// Base class for all RLP exceptions.
  41. struct RLPException: virtual Exception { RLPException(std::string _message = std::string()): Exception(_message) {} };
  42. #define DEV_SIMPLE_EXCEPTION_RLP(X) struct X: virtual RLPException { const char* what() const noexcept override { return #X; } }
  43. DEV_SIMPLE_EXCEPTION_RLP(BadCast);
  44. DEV_SIMPLE_EXCEPTION_RLP(BadRLP);
  45. DEV_SIMPLE_EXCEPTION_RLP(OversizeRLP);
  46. DEV_SIMPLE_EXCEPTION_RLP(UndersizeRLP);
  47. DEV_SIMPLE_EXCEPTION(BadHexCharacter);
  48. DEV_SIMPLE_EXCEPTION(NoNetworking);
  49. DEV_SIMPLE_EXCEPTION(NoUPnPDevice);
  50. DEV_SIMPLE_EXCEPTION(RootNotFound);
  51. struct BadRoot: virtual Exception { public: BadRoot(h256 const& _root): Exception("BadRoot " + _root.hex()), root(_root) {} h256 root; };
  52. DEV_SIMPLE_EXCEPTION(FileError);
  53. DEV_SIMPLE_EXCEPTION(Overflow);
  54. DEV_SIMPLE_EXCEPTION(FailedInvariant);
  55. DEV_SIMPLE_EXCEPTION(ValueTooLarge);
  56. struct InterfaceNotSupported: virtual Exception { public: InterfaceNotSupported(std::string _f): Exception("Interface " + _f + " not supported.") {} };
  57. struct ExternalFunctionFailure: virtual Exception { public: ExternalFunctionFailure(std::string _f): Exception("Function " + _f + "() failed.") {} };
  58. // error information to be added to exceptions
  59. using errinfo_invalidSymbol = boost::error_info<struct tag_invalidSymbol, char>;
  60. using errinfo_wrongAddress = boost::error_info<struct tag_address, std::string>;
  61. using errinfo_comment = boost::error_info<struct tag_comment, std::string>;
  62. using errinfo_required = boost::error_info<struct tag_required, bigint>;
  63. using errinfo_got = boost::error_info<struct tag_got, bigint>;
  64. using errinfo_min = boost::error_info<struct tag_min, bigint>;
  65. using errinfo_max = boost::error_info<struct tag_max, bigint>;
  66. using RequirementError = boost::tuple<errinfo_required, errinfo_got>;
  67. using errinfo_hash256 = boost::error_info<struct tag_hash, h256>;
  68. using errinfo_required_h256 = boost::error_info<struct tag_required_h256, h256>;
  69. using errinfo_got_h256 = boost::error_info<struct tag_get_h256, h256>;
  70. using Hash256RequirementError = boost::tuple<errinfo_required_h256, errinfo_got_h256>;
  71. using errinfo_extraData = boost::error_info<struct tag_extraData, bytes>;
  72. }