2017-12-18 15:46:52 +11:00
|
|
|
/*
|
2018-08-04 15:14:06 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2017-12-18 15:46:52 +11:00
|
|
|
*
|
2018-07-05 13:44:03 +10:00
|
|
|
* Copyright 2010-2018
|
2017-12-18 15:46:52 +11:00
|
|
|
* Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "except.hpp"
|
|
|
|
|
2018-08-13 14:51:33 +10:00
|
|
|
#include "../platform.hpp"
|
2019-05-17 12:26:08 +10:00
|
|
|
#include "../debug/assert.hpp"
|
2017-12-18 15:46:52 +11:00
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
using cruft::posix::error;
|
2017-12-18 15:46:52 +11:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// Construct an exception object from a given error value.
|
|
|
|
///
|
|
|
|
/// The error value MUST be an error at construction time.
|
|
|
|
error::error (int _code):
|
|
|
|
m_code (_code)
|
|
|
|
{
|
|
|
|
CHECK_NEQ (_code, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///----------------------------------------------------------------------------
|
|
|
|
/// Construct an exception object from the current value of errno.
|
|
|
|
///
|
|
|
|
/// errno MUST signal an error at construction time.
|
|
|
|
error::error ():
|
|
|
|
error (last_code ())
|
|
|
|
{
|
|
|
|
CHECK_NEQ (m_code, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
int
|
|
|
|
error::last_code (void)
|
|
|
|
{
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
int
|
|
|
|
error::code (void) const
|
|
|
|
{
|
|
|
|
return m_code;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// Throw an exception object if errno currently signals an error.
|
|
|
|
void
|
|
|
|
error::try_code (void)
|
|
|
|
{
|
|
|
|
try_code (last_code ());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///----------------------------------------------------------------------------
|
|
|
|
/// Throw an exception object if 'code' represents an error.
|
|
|
|
void
|
|
|
|
error::try_code (int code)
|
|
|
|
{
|
|
|
|
if (__builtin_expect (code != 0, false))
|
|
|
|
throw error (code);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///----------------------------------------------------------------------------
|
|
|
|
void
|
|
|
|
error::throw_code (void)
|
|
|
|
{
|
|
|
|
throw_code (last_code ());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///----------------------------------------------------------------------------
|
|
|
|
void
|
|
|
|
error::throw_code (int code)
|
|
|
|
{
|
|
|
|
CHECK_NEQ (code, 0);
|
|
|
|
throw error (code);
|
|
|
|
}
|
2017-12-22 18:34:00 +11:00
|
|
|
|
|
|
|
|
2018-07-05 13:44:03 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
const char*
|
|
|
|
error::what (void) const noexcept
|
|
|
|
{
|
|
|
|
return strerror (m_code);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-12-22 18:34:00 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-08-05 14:42:02 +10:00
|
|
|
using cruft::posix::eai;
|
2017-12-22 18:34:00 +11:00
|
|
|
|
2018-08-13 14:51:33 +10:00
|
|
|
#if !defined(PLATFORM_WIN32)
|
2017-12-22 18:34:00 +11:00
|
|
|
#include <netdb.h>
|
2018-08-13 14:51:33 +10:00
|
|
|
#else
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#endif
|
2017-12-22 18:34:00 +11:00
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
eai::eai (int code):
|
|
|
|
runtime_error (gai_strerror (code)),
|
|
|
|
m_code (code)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
int
|
|
|
|
eai::code (void) const
|
|
|
|
{
|
|
|
|
return m_code;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void
|
|
|
|
eai::try_code (int code)
|
|
|
|
{
|
|
|
|
if (code)
|
|
|
|
throw_code (code);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void
|
|
|
|
eai::throw_code (int code)
|
|
|
|
{
|
|
|
|
throw eai { code };
|
|
|
|
}
|