std::error_code and std::expected implementation
This commit is contained in:
parent
243d67c93e
commit
537aa6bc36
4 changed files with 2756 additions and 0 deletions
|
@ -290,6 +290,9 @@ add_library (secure
|
|||
|
||||
SET (RAI_LIB_SOURCES
|
||||
${PLATFORM_LIB_SOURCE}
|
||||
rai/lib/errors.hpp
|
||||
rai/lib/errors.cpp
|
||||
rai/lib/expected.hpp
|
||||
rai/lib/blocks.cpp
|
||||
rai/lib/blocks.hpp
|
||||
rai/lib/interface.cpp
|
||||
|
|
53
rai/lib/errors.cpp
Normal file
53
rai/lib/errors.cpp
Normal file
|
@ -0,0 +1,53 @@
|
|||
#include "rai/lib/errors.hpp"
|
||||
|
||||
std::string nano::error_common_messages::message (int ev) const
|
||||
{
|
||||
switch (static_cast<nano::error_common> (ev))
|
||||
{
|
||||
case nano::error_common::generic:
|
||||
return "Unknown error";
|
||||
case nano::error_common::account_exists:
|
||||
return "Account already exists";
|
||||
case nano::error_common::account_not_found:
|
||||
return "Account not found";
|
||||
case nano::error_common::bad_account_number:
|
||||
return "Bad account number";
|
||||
case nano::error_common::bad_public_key:
|
||||
return "Bad public key";
|
||||
case nano::error_common::bad_seed:
|
||||
return "Bad seed";
|
||||
case nano::error_common::bad_wallet_number:
|
||||
return "Bad wallet number";
|
||||
case nano::error_common::bad_work_format:
|
||||
return "Bad work";
|
||||
case nano::error_common::invalid_work:
|
||||
return "Invalid work";
|
||||
case nano::error_common::invalid_index:
|
||||
return "Invalid index";
|
||||
case nano::error_common::numeric_conversion:
|
||||
return "Numeric conversion error";
|
||||
case nano::error_common::wallet_locked:
|
||||
return "Wallet is locked";
|
||||
case nano::error_common::wallet_not_found:
|
||||
return "Wallet not found";
|
||||
}
|
||||
|
||||
return "Invalid error code";
|
||||
}
|
||||
|
||||
std::string nano::error_blocks_messages::message (int ev) const
|
||||
{
|
||||
switch (static_cast<nano::error_blocks> (ev))
|
||||
{
|
||||
case nano::error_blocks::generic:
|
||||
return "Unknown error";
|
||||
case nano::error_blocks::bad_hash_number:
|
||||
return "Bad hash number";
|
||||
case nano::error_blocks::invalid_block_hash:
|
||||
return "Invalid block hash";
|
||||
case nano::error_blocks::not_found:
|
||||
return "Block not found";
|
||||
}
|
||||
|
||||
return "Invalid error code";
|
||||
}
|
96
rai/lib/errors.hpp
Normal file
96
rai/lib/errors.hpp
Normal file
|
@ -0,0 +1,96 @@
|
|||
#pragma once
|
||||
|
||||
#include <rai/lib/expected.hpp>
|
||||
#include <string>
|
||||
#include <system_error>
|
||||
|
||||
using tl::expected;
|
||||
using tl::make_unexpected;
|
||||
|
||||
namespace nano
|
||||
{
|
||||
/** Returns the error code if non-zero, otherwise the value */
|
||||
template <class T>
|
||||
auto either (T && value, std::error_code ec) -> expected<std::remove_reference_t<T>, std::error_code>
|
||||
{
|
||||
if (ec)
|
||||
{
|
||||
return make_unexpected (ec);
|
||||
}
|
||||
else
|
||||
{
|
||||
return std::move (value);
|
||||
}
|
||||
}
|
||||
|
||||
/** Common error codes */
|
||||
enum class error_common
|
||||
{
|
||||
generic = 1,
|
||||
account_not_found,
|
||||
account_exists,
|
||||
bad_account_number,
|
||||
bad_public_key,
|
||||
bad_seed,
|
||||
bad_wallet_number,
|
||||
bad_work_format,
|
||||
invalid_work,
|
||||
invalid_index,
|
||||
numeric_conversion,
|
||||
wallet_locked,
|
||||
wallet_not_found
|
||||
};
|
||||
|
||||
/** Block related errors */
|
||||
enum class error_blocks
|
||||
{
|
||||
generic = 1,
|
||||
bad_hash_number,
|
||||
invalid_block_hash,
|
||||
not_found
|
||||
};
|
||||
}
|
||||
|
||||
// Convenience macro to implement the standard boilerplate for using std::error_code with enums
|
||||
// Use this at the end of any header defining one or more error code enums.
|
||||
#define REGISTER_ERROR_CODES(namespace_name, enum_type) \
|
||||
namespace namespace_name \
|
||||
{ \
|
||||
static_assert (static_cast<int> (enum_type::generic) > 0, "The first error enum must be generic = 1"); \
|
||||
class enum_type##_messages : public std::error_category \
|
||||
{ \
|
||||
public: \
|
||||
const char * name () const noexcept override \
|
||||
{ \
|
||||
return #enum_type; \
|
||||
} \
|
||||
\
|
||||
std::string message (int ev) const override; \
|
||||
}; \
|
||||
\
|
||||
inline const std::error_category & enum_type##_category () \
|
||||
{ \
|
||||
static enum_type##_messages instance; \
|
||||
return instance; \
|
||||
} \
|
||||
\
|
||||
inline std::error_code make_error_code (::namespace_name::enum_type err) \
|
||||
{ \
|
||||
return { static_cast<int> (err), enum_type##_category () }; \
|
||||
} \
|
||||
\
|
||||
inline auto unexpected_error (::namespace_name::enum_type err) -> decltype (make_unexpected (make_error_code (err))) \
|
||||
{ \
|
||||
return make_unexpected (make_error_code (err)); \
|
||||
} \
|
||||
} \
|
||||
namespace std \
|
||||
{ \
|
||||
template <> \
|
||||
struct is_error_code_enum<::namespace_name::enum_type> : std::true_type \
|
||||
{ \
|
||||
}; \
|
||||
}
|
||||
|
||||
REGISTER_ERROR_CODES (nano, error_common);
|
||||
REGISTER_ERROR_CODES (nano, error_blocks);
|
2604
rai/lib/expected.hpp
Normal file
2604
rai/lib/expected.hpp
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue