<commit_before>// Copyright (c) 2009 - Decho Corp.

#include "mordor/common/pch.h"

#include "exception.h"

static void throwSocketException(error_t lastError, const char *function)
{
    switch (lastError) {
        case WSA(ECONNABORTED):
            throw ConnectionAbortedException(function);
        case WSA(ECONNRESET):
            throw ConnectionResetException(function);
        case WSA(ECONNREFUSED):
            throw ConnectionRefusedException(function);
        case WSA(EHOSTDOWN):
            throw HostDownException(function);
        case WSA(EHOSTUNREACH):
            throw HostUnreachableException(function);
        case WSA(ENETDOWN):
            throw NetworkDownException(function);
        case WSA(ENETRESET):
            throw NetworkResetException(function);
        case WSA(ENETUNREACH):
            throw NetworkUnreachableException(function);
        case WSA(ETIMEDOUT):
            throw TimedOutException(function);
        case EAI_AGAIN:
            throw TemporaryNameServerFailureException(function);
        case EAI_FAIL:
            throw PermanentNameServerFailureException(function);
        case NATIVE_ERROR(WSANO_DATA, EAI_NODATA):
            throw NoNameServerDataException(function);
        case EAI_NONAME:
            throw HostNotFoundException(function);
        default:
            break;
    }
}

#ifdef WINDOWS
#include <windows.h>

Win32Error::Win32Error(unsigned int lastError, const char *function)
: std::runtime_error(constructMessage(lastError)),
  m_lastError(lastError),
  m_function(function)
{}

std::string
Win32Error::constructMessage(unsigned int lastError)
{
    std::string result;
    char *desc;
    DWORD numChars = FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER |
        FORMAT_MESSAGE_FROM_SYSTEM |
        FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,
        lastError, 0,
        (char*)&desc, 0, NULL);
    if (numChars > 0) {
        result = desc;
        LocalFree((HANDLE)desc);
    }
    return result;
}

error_t lastError()
{
    return GetLastError();
}

void throwExceptionFromLastError(unsigned int lastError, const char *function)
{
    switch (lastError) {
        case ERROR_INVALID_HANDLE:
        case WSAENOTSOCK:
            throw BadHandleException(function);
        case ERROR_FILE_NOT_FOUND:
            throw FileNotFoundException(function);
        case ERROR_OPERATION_ABORTED:
            throw OperationAbortedException(function);
        case WSAESHUTDOWN:
            throw BrokenPipeException(function);
        default:
            throwSocketException(lastError, function);
            throw Win32Error(lastError, function);
    }
}
#else
#include <errno.h>
#include <string.h>

ErrnoError::ErrnoError(int error, const char *function)
: std::runtime_error(constructMessage(error)),
  m_error(error),
  m_function(function)
{}

std::string
ErrnoError::constructMessage(int error)
{
    const char *message = strerror(error);
    if (message)
        return message;
    return "";<commit_msg>Get the proper string for name lookup errors on POSIX systems.<commit_after>// Copyright (c) 2009 - Decho Corp.

#include "mordor/common/pch.h"

#include "exception.h"

static void throwSocketException(error_t lastError, const char *function)
{
    switch (lastError) {
        case WSA(ECONNABORTED):
            throw ConnectionAbortedException(function);
        case WSA(ECONNRESET):
            throw ConnectionResetException(function);
        case WSA(ECONNREFUSED):
            throw ConnectionRefusedException(function);
        case WSA(EHOSTDOWN):
            throw HostDownException(function);
        case WSA(EHOSTUNREACH):
            throw HostUnreachableException(function);
        case WSA(ENETDOWN):
            throw NetworkDownException(function);
        case WSA(ENETRESET):
            throw NetworkResetException(function);
        case WSA(ENETUNREACH):
            throw NetworkUnreachableException(function);
        case WSA(ETIMEDOUT):
            throw TimedOutException(function);
        case EAI_AGAIN:
            throw TemporaryNameServerFailureException(function);
        case EAI_FAIL:
            throw PermanentNameServerFailureException(function);
        case NATIVE_ERROR(WSANO_DATA, EAI_NODATA):
            throw NoNameServerDataException(function);
        case EAI_NONAME:
            throw HostNotFoundException(function);
        default:
            break;
    }
}

#ifdef WINDOWS
#include <windows.h>

Win32Error::Win32Error(unsigned int lastError, const char *function)
: std::runtime_error(constructMessage(lastError)),
  m_lastError(lastError),
  m_function(function)
{}

std::string
Win32Error::constructMessage(unsigned int lastError)
{
    std::string result;
    char *desc;
    DWORD numChars = FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER |
        FORMAT_MESSAGE_FROM_SYSTEM |
        FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,
        lastError, 0,
        (char*)&desc, 0, NULL);
    if (numChars > 0) {
        result = desc;
        LocalFree((HANDLE)desc);
    }
    return result;
}

error_t lastError()
{
    return GetLastError();
}

void throwExceptionFromLastError(unsigned int lastError, const char *function)
{
    switch (lastError) {
        case ERROR_INVALID_HANDLE:
        case WSAENOTSOCK:
            throw BadHandleException(function);
        case ERROR_FILE_NOT_FOUND:
            throw FileNotFoundException(function);
        case ERROR_OPERATION_ABORTED:
            throw OperationAbortedException(function);
        case WSAESHUTDOWN:
            throw BrokenPipeException(function);
        default:
            throwSocketException(lastError, function);
            throw Win32Error(lastError, function);
    }
}
#else
#include <errno.h>
#include <string.h>

ErrnoError::ErrnoError(int error, const char *function)
: std::runtime_error(constructMessage(error)),
  m_error(error),
  m_function(function)
{}

std::string
ErrnoError::constructMessage(int error)
{
    const char *message;
    switch (error) {
        case EAI_ADDRFAMILY:
        case EAI_AGAIN:
        case EAI_BADFLAGS:
        case EAI_FAIL:
        case EAI_FAMILY:
        case EAI_MEMORY:
        case EAI_NODATA:
        case EAI_NONAME:
        case EAI_SERVICE:
        case EAI_SOCKTYPE:
        case EAI_SYSTEM:
            message = gai_strerror(error);
            break;
        default:
            message = strerror(error);
            break;
    }
    if (message)
        return message;
    return "";
{'fraction_non_alphanumeric': 0.08456334563345634, 'fraction_numerical': 0.004612546125461255, 'mean_word_length': 2.788584740827024, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1178733', 'n_tokens_mistral': 1900, 'n_tokens_neox': 1850, 'n_words': 394}