Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7675373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:56:50+00:00 2026-05-31T16:56:50+00:00

I’m using std::error_code and have a bunch of error’s defined (using enum class) and

  • 0

I’m using std::error_code and have a bunch of error’s defined (using enum class) and registered.

I have a very generic error now called my_error::validate, but want to provide more specific versions in my library. Generally people will want to use:

if (ec == bc::error::validate)
    // ...

However sometimes they may wish to see the specific error associated with that std::error_code or print the error message.

// ec.message() says "check_block() failed to do XYZ"
assert(ec == bc::error::check_block);

I want to be able to enable something like:

if (ec == bc::error::validate)
{
    if (ec == bc::error::check_block)
        // bc::error::check_block is a more specific case of bc::error::validate
}

It seems I can somehow use categories or conditions? How can I do that without needing to define a whole bunch of new error enums? It’s for a library so it would be a pain for the user of that library to have to use bc::generic_error::validate and bc::error::check_block.

Code is below:

#include <system_error>

namespace bc {

enum class error
{
    // storage errors
    missing_object = 1,
    object_already_exists,
    unspent_output,
    // transaction_pool errors
    bad_transaction,
    // network errors
    resolve_failed,
    network_unreachable,
    address_in_use,
    listen_failed,
    accept_failed,
    bad_stream,
    channel_stopped,
    channel_timeout,
    // validate
    validate_failed,
    check_block,
    accept_block,
    connect_block
};

class error_category_impl
  : public std::error_category
{
public:
    virtual const char* name() const;
    virtual std::string message(int ev) const;
    virtual std::error_condition default_error_condition(int ev) const;
};

const std::error_category& error_category();

std::error_code make_error_code(error e);
std::error_condition make_error_condition(error e);

} // bc

namespace std
{
    template <>
    struct is_error_code_enum<libbitcoin::error>
      : public true_type {};
}

And the TU source file:

#include <bc/error.hpp>

namespace bc {

const char* error_category_impl::name() const
{
    return "bitcoin";
}

std::string error_category_impl::message(int ev) const
{
    error ec = static_cast<error>(ev);
    switch (ec)
    {
        case error::missing_object:
            return "Object does not exist";
        case error::object_already_exists:
            return "Matching previous object found";
        case error::unspent_output:
            return "Unspent output";
        case error::bad_transaction:
            return "Transaction failed to validate";
        case error::resolve_failed:
            return "Resolving hostname failed";
        case error::network_unreachable:
            return "Unable to reach remote network";
        case error::address_in_use:
            return "Address already in use";
        case error::listen_failed:
            return "Listen incoming connections failed";
        case error::accept_failed:
            return "Accept connection failed";
        case error::bad_stream:
            return "Bad stream";
        case error::channel_stopped:
            return "Channel stopped";
        case error::channel_timeout:
            return "Channel timed out";
        default:
            return "Unknown error";
    }
}

std::error_condition
    error_category_impl::default_error_condition(int ev) const
{
    error ec = static_cast<error>(ev);
    switch (ec)
    {
        case error::check_block:
        case error::accept_block:
        case error::connect_block:
            //return error::validate_failed;
            return std::errc::permission_denied;
        default:
            return std::error_condition(ev, *this);
    }
}

const std::error_category& error_category()
{
    static error_category_impl instance;
    return instance;
}

std::error_code make_error_code(error e)
{
    return std::error_code(static_cast<int>(e), error_category());
}

std::error_condition make_error_condition(error e)
{
    return std::error_condition(static_cast<int>(e), error_category());
}

} // bc
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T16:56:51+00:00Added an answer on May 31, 2026 at 4:56 pm

    OK I got help from the boost::asio and std::error_code creator and master himself: Chris Kohlhoff.

    When using ADL, a good rule of thumb is that it should not need any qualifier (error::error_code_t in my case), and I was in the wrong scope.

    #include <iostream>
    #include <system_error>
    
    namespace libbitcoin {
    
    namespace error
    {
        // Specific errors
        enum error_code_t
        {
            // storage errors
            missing_object = 1,
            object_already_exists,
            unspent_output,
            // transaction_pool errors
            bad_transaction,
            // network errors
            resolve_failed,
            network_unreachable,
            address_in_use,
            listen_failed,
            accept_failed,
            bad_stream,
            channel_stopped,
            channel_timeout,
            // validate
            check_block,
            accept_block,
            connect_block
        };
    
        // error_condition
        enum error_condition_t
        {
            // validate
            validate_failed = 1
        };
    
        std::error_code make_error_code(error_code_t e);
        std::error_condition make_error_condition(error_condition_t e);
    }
    
    class error_category_impl
      : public std::error_category
    {
    public:
        virtual const char* name() const;
        virtual std::string message(int ev) const;
        virtual std::error_condition default_error_condition(int ev) const;
    };
    
    const std::error_category& error_category();
    
    } // libbitcoin
    
    namespace std
    {
        template <>
        struct is_error_code_enum<libbitcoin::error::error_code_t>
          : public true_type {};
    
        template <>
        struct is_error_condition_enum<libbitcoin::error::error_condition_t>
          : public true_type {};
    }
    
    // -------------------------------------------------------------------
    
    namespace libbitcoin {
    
    namespace error {
    std::error_code make_error_code(error_code_t e)
    {
        return std::error_code(static_cast<int>(e), error_category());
    }
    
    std::error_condition make_error_condition(error_condition_t e)
    {
        return std::error_condition(static_cast<int>(e), error_category());
    }
    }
    
    const char* error_category_impl::name() const
    {
        return "bitcoin";
    }
    
    std::string error_category_impl::message(int ev) const
    {
        //error ec = static_cast<error>(ev);
        switch (ev)
        {
            case error::missing_object:
                return "Object does not exist";
            case error::object_already_exists:
                return "Matching previous object found";
            case error::unspent_output:
                return "Unspent output";
            case error::bad_transaction:
                return "Transaction failed to validate";
            case error::resolve_failed:
                return "Resolving hostname failed";
            case error::network_unreachable:
                return "Unable to reach remote network";
            case error::address_in_use:
                return "Address already in use";
            case error::listen_failed:
                return "Listen incoming connections failed";
            case error::accept_failed:
                return "Accept connection failed";
            case error::bad_stream:
                return "Bad stream";
            case error::channel_stopped:
                return "Channel stopped";
            case error::channel_timeout:
                return "Channel timed out";
            case error::check_block:
                return "Checkblk";
            default:
                return "Unknown error";
        }
    }
    
    std::error_condition
        error_category_impl::default_error_condition(int ev) const
    {
        //error ec = static_cast<error>(ev);
        switch (ev)
        {
            case error::check_block:
            case error::accept_block:
            case error::connect_block:
                return std::error_condition(error::validate_failed, *this);
            default:
                return std::error_condition(ev, *this);
        }
    }
    
    const std::error_category& error_category()
    {
        static error_category_impl instance;
        return instance;
    }
    
    } // libbitcoin
    
    using namespace libbitcoin;
    
    #include <assert.h>
    
    int main()
    {
        std::error_code ec = error::check_block;
        assert(ec == error::validate_failed);
        assert(ec == error::check_block);
        std::cout << ec.message() << std::endl;
        //ec = error::missing_object;
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have thousands of HTML files to process using Groovy/Java and I need to
I have a bunch of posts stored in text files formatted in yaml/textile (from
I am trying to loop through a bunch of documents I have to put
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.