libcaf  0.15.0
Public Member Functions | Friends | Related Functions | List of all members
caf::error Class Reference

A serializable type for storing error codes with category and optional, human-readable context information. More...

#include <error.hpp>

Inherits comparable< error >.

Public Member Functions

 error (error &&)=default
 
 error (const error &)=default
 
erroroperator= (error &&)=default
 
erroroperator= (const error &)=default
 
 error (uint8_t code, atom_value category, message context=message{})
 
template<class E , class = typename std::enable_if< std::is_same< decltype(make_error(std::declval<const E&>())), error >::value >::type>
 error (E error_value)
 
uint8_t code () const
 Returns the category-specific error code, whereas 0 means "no error".
 
atom_value category () const
 Returns the category of this error.
 
messagecontext ()
 Returns optional context information to this error.
 
const messagecontext () const
 Returns optional context information to this error.
 
 operator bool () const noexcept
 Returns code() != 0.
 
bool operator! () const noexcept
 Returns code() == 0.
 
void clear ()
 Sets the error code to 0.
 
uint32_t compress_code_and_size () const
 Creates a flag storing the context size and the error code.
 
int compare (uint8_t code, atom_value category) const
 
int compare (const error &) const
 

Friends

void serialize (serializer &sink, error &x, const unsigned int)
 
void serialize (deserializer &source, error &x, const unsigned int)
 

Related Functions

(Note that these are not member functions.)

template<class E , class = typename std::enable_if< std::is_same< decltype(make_error(std::declval<const E&>())), error >::value >::type>
bool operator== (const error &x, E y)
 
template<class E , class = typename std::enable_if< std::is_same< decltype(make_error(std::declval<const E&>())), error >::value >::type>
bool operator== (E x, const error &y)
 
template<class E , class = typename std::enable_if< std::is_same< decltype(make_error(std::declval<const E&>())), error >::value >::type>
bool operator!= (const error &x, E y)
 
template<class E , class = typename std::enable_if< std::is_same< decltype(make_error(std::declval<const E&>())), error >::value >::type>
bool operator!= (E x, const error &y)
 
std::string to_string (const error &x)
 

Detailed Description

A serializable type for storing error codes with category and optional, human-readable context information.

Unlike error handling classes from the C++ standard library, this type is serializable. It consists of an 8-bit code, a 64-bit atom constant, plus optionally a ::message to store additional information.

Why not std::error_code or std::error_condition?

First, the standard does not define the values for std::errc. This means serializing error conditions (which are meant to be portable) is not safe in a distributed setting unless all machines are running the same operating system and version of the C++ standard library.

Second, the standard library primitives, unlike exceptions, do not offer an API for attaching additional context to an error. The error handling API offered by the standard is meant to wrap C system calls in a (source code) portable way. In a distributed setting, an error may not occur locally. In this case, an error code and category alone is often not satisfactory information when signalling errors back to end users. The additional context also enables composition of errors by modifying the message details as needed.

Why is there no string() member function?

The C++ standard library uses category singletons and virtual dispatching to correlate error codes to descriptive strings. However, singletons are a poor choice when it comes to serialization. CAF uses atoms for categories instead and requires users to register custom error categories to the actor system. This makes the actor system the natural instance for rendering error messages via actor_system::render(const error&).

Friends And Related Function Documentation

template<class E , class = typename std::enable_if< std::is_same< decltype(make_error(std::declval<const E&>())), error >::value >::type>
bool operator!= ( const error x,
y 
)
related
template<class E , class = typename std::enable_if< std::is_same< decltype(make_error(std::declval<const E&>())), error >::value >::type>
bool operator!= ( x,
const error y 
)
related
template<class E , class = typename std::enable_if< std::is_same< decltype(make_error(std::declval<const E&>())), error >::value >::type>
bool operator== ( const error x,
y 
)
related
template<class E , class = typename std::enable_if< std::is_same< decltype(make_error(std::declval<const E&>())), error >::value >::type>
bool operator== ( x,
const error y 
)
related
std::string to_string ( const error x)
related

The documentation for this class was generated from the following file: