Class last_api_error

Synopsis

#include "breeze/diagnostics/last_api_error.hpp"

class last_api_error
    :   public std::runtime_error

Description

The last error encountered by the calling thread in an OS API call.

It corresponds to GetLastError() under Windows and errno under Unix. This class can be used in two ways:

  • As an exception type; the idea is that after an unsuccessful API call you write: throw last_api_error( "<APIName>() failed" ) ; and you're done with error handling.
  • As a convenience facility to get both the error code and the error message for the last error.
Warning
This class is experimental.
Warning
Only throw an exception of type last_api_error if the return value of the last function you called in the current thread indicates that GetLastError() (if under Windows) or errno (if under Unix) is meaningful.

Inheritance

Ancestors: std::runtime_error

Methods

last_api_error
codeReturns: The code of the last error
to_stringReturns: A string representation of the object.

Source

Lines 54-90 in breeze/diagnostics/last_api_error.hpp.

class last_api_error
    :   public std::runtime_error
{
public:
    explicit            last_api_error( char const * ) ;

    //!     \return
    //!         The code of the last error. A <code>long long</code>,
    //!         which is enough to accomodate both \c errno and the
    //!         result of \c GetLastError() under Windows.
    // -----------------------------------------------------------------------
    long long           code() const noexcept ;

    //!     \return
    //!         A string representation of the object.
    //!
    //!     This function gives more information than \c what(), because
    //!     the resulting string will include the code of the error (\c
    //!     code()), but, differently from \c what(), may throw.
    // -----------------------------------------------------------------------
    std::string         to_string() const ;

private:
    typedef std::runtime_error
                        base_type ;

    friend std::ostream &
                        operator <<( std::ostream &, last_api_error const & ) ;

    long long           m_code ;

    //      See the comments in the implementation files for the purpose
    //      of this private constructor.
    // -----------------------------------------------------------------------
                        last_api_error( char const * p, long long error_code ) ;

} ;