CppWAMP
C++11 client library for the WAMP protocol
Error Handling

This page describes how error handling is done via ErrorOr.

ErrorOr and Exceptions

Whenever an asynchronous wamp::Session operation fails, it will emit a wamp::ErrorOr that contains a std::error_code instead of an actual result.

The wamp::ErrorOr::value method would normally be used to retrieve the underlying result of the asynchronous operation. If the ErrorOr instead contains an error, ErrorOr::value will throw a wamp::error::Failure exception. Since Failure derives from std::system_error, it contains a std::error_code that represents the cause of the error.

Example using error codes:

using namespace wamp;
auto tcp = TcpConnector::create<Json>(ioctx, {"localhost", 8001});
boost::asio::spawn(ioctx, [&](boost::asio::yield_context yield)
{
auto session = Session::create(ioctx, tcp);
auto index = session->connect(yield);
if (!index)
{
std::cerr << "Connect failed: " << index.error() << "\n";
return;
}
auto info = session->join(Realm("somerealm"), yield);
if (!info)
{
std::cerr << "Join failed: " << info.error() << "\n";
return;
}
// etc.
});
ioctx.run();

Example using exceptions:

using namespace wamp;
auto tcp = TcpConnector::create<Json>(ioctx, {"localhost", 8001});
boost::asio::spawn(ioctx, [&](boost::asio::yield_context yield)
{
auto session = Session::create(ioctx, tcp);
try
{
session->connect(yield).value(); // Note the .value()
session->join(Realm("somerealm"), yield).value(); // Note the .value()
// etc.
}
catch (const error::Failure& e)
{
// Print a message describing the error
std::cerr << e.what() << "\n";
// Obtain the std::error_code associated with the exception
auto ec = e.code();
std::cerr << "The realm doesn't exist\n";
else
std::cerr << "Unexpected error: " << ec << "\n";
}
});
ioctx.run();

Error Codes

An error code can either belong to std::generic_category, or to one of the error categories defined by the library in <cppwamp/error.hpp>:

Error category Values Used for reporting
wamp::SessionCategory wamp::SessionErrc WAMP session errors
wamp::ProtocolCategory wamp::ProtocolErrc invalid WAMP messages
wamp::TransportCategory wamp::TransportErrc general transport layer errors
wamp::RawsockCategory wamp::RawsockErrc raw socket transport errors
std::generic_category std::errc OS-level socket errors

Failed Preconditions

Note that Session will always throw wamp::error::Logic exceptions whenever preconditions are not met. Preconditions for API functions are listed in this reference documentation.


Next: Remote Procedure Calls

wamp::Session::create
static Ptr create(AnyIoExecutor exec, const Connector::Ptr &connector)
Creates a new Session instance.
Definition: session.ipp:22
wamp::Realm
Realm URI and other options contained within WAMP HELLO messages.
Definition: peerdata.hpp:59
wamp::error::Failure
General purpose runtime exception that wraps a std::error_code.
Definition: error.hpp:52
wamp
Definition: anyhandler.hpp:36
wamp::SessionErrc::noSuchRealm
@ noSuchRealm
Attempt to join non-existent realm.
wamp::AsioContext
boost::asio::io_context AsioContext
Queues and runs I/O completion handlers.
Definition: asiodefs.hpp:34