The complete source code of IdealIRC
http://www.idealirc.org/
129 lines
4.7 KiB
129 lines
4.7 KiB
/*
|
|
* IdealIRC Core - Internet Relay Chat API
|
|
* Copyright (C) 2021 Tom-Andre Barstad.
|
|
* This software is licensed under the Software Attribution License.
|
|
* See LICENSE for more information.
|
|
*/
|
|
|
|
#include "IRCError.h"
|
|
#include <iostream>
|
|
#include <asio/error.hpp>
|
|
|
|
int lastAsioErrorCode{ 0 };
|
|
std::string lastAsioErrorMessage;
|
|
|
|
std::string IRCErrorToString(IRCError e)
|
|
{
|
|
switch (e) {
|
|
case IRCError::NoError:
|
|
return "No error";
|
|
case IRCError::NotConnected:
|
|
return "Not connected";
|
|
case IRCError::AlreadyConnected:
|
|
return "Already connected";
|
|
case IRCError::CannotResolveAddress:
|
|
return "Cannot resolve address";
|
|
case IRCError::BrokenPipe:
|
|
return "Broken pipe";
|
|
case IRCError::ConnectionAborted:
|
|
return "Connection aborted";
|
|
case IRCError::ConnectionRefused:
|
|
return "Connection refused";
|
|
case IRCError::ConnectionReset:
|
|
return "Connection reset";
|
|
case IRCError::HostUnreachable:
|
|
return "Host unreachable";
|
|
case IRCError::NetworkDown:
|
|
return "Network down";
|
|
case IRCError::NetworkReset:
|
|
return "Network reset";
|
|
case IRCError::NetworkUnreachable:
|
|
return "Network unreachable";
|
|
case IRCError::NoDescriptors:
|
|
return "No file descriptors left";
|
|
case IRCError::TimedOut:
|
|
return "Timed out";
|
|
case IRCError::EndOfFile:
|
|
return "EOF";
|
|
case IRCError::SSL_SelfSigned:
|
|
return "SSL: Self-signed certificate";
|
|
case IRCError::SSL_CN_Mismatch:
|
|
return "SSL: (CN) Hostname mismatch";
|
|
case IRCError::SSL_CN_Missing:
|
|
return "SSL: (CN) Hostname missing";
|
|
case IRCError::SSL_CN_WildcardIllegal:
|
|
return "SSL: (CN) Illegal wildcard usage";
|
|
case IRCError::SSL_NotYetValid:
|
|
return "SSL: Certificate not yet valid";
|
|
case IRCError::SSL_Expired:
|
|
return "SSL: Certificate expired";
|
|
case IRCError::CannotChangeWhenConnected:
|
|
return "Cannot change this setting when connected";
|
|
case IRCError::HostNotSet:
|
|
return "Hostname is not set";
|
|
case IRCError::PortNotSet:
|
|
return "Port number is not set";
|
|
case IRCError::IdentNotSet:
|
|
return "Ident/username is not set";
|
|
case IRCError::NicknameNotSet:
|
|
return "Nickname is not set";
|
|
case IRCError::RealnameNotSet:
|
|
return "Real name is not set";
|
|
case IRCError::DCC_NotATarget:
|
|
return "DCC: We are not a DCC target";
|
|
case IRCError::DCC_TimedOut:
|
|
return "DCC: Request timed out";
|
|
case IRCError::DCC_InitiateError:
|
|
return "DCC: Unable to initiate";
|
|
case IRCError::UnhandledException:
|
|
return "Unhandled exception";
|
|
}
|
|
|
|
/*
|
|
* Compiler complains even if all cases are covered...
|
|
* I don't wanna use a 'default' so that my IDE and tools can pick up on missing enumerations.
|
|
* A return here would suffice.
|
|
*/
|
|
return fmt::format("Uncaught error code {}", static_cast<int>(e));
|
|
}
|
|
|
|
IRCError SystemErrorToIRCError(const std::system_error& e)
|
|
{
|
|
switch (e.code().value()) {
|
|
case 0:
|
|
return IRCError::NoError;
|
|
case asio::error::host_not_found:
|
|
return IRCError::CannotResolveAddress;
|
|
case asio::error::broken_pipe:
|
|
return IRCError::BrokenPipe;
|
|
case asio::error::connection_aborted:
|
|
return IRCError::ConnectionAborted;
|
|
case asio::error::connection_refused:
|
|
return IRCError::ConnectionRefused;
|
|
case asio::error::connection_reset:
|
|
return IRCError::ConnectionReset;
|
|
case asio::error::host_unreachable:
|
|
return IRCError::HostUnreachable;
|
|
case asio::error::network_down:
|
|
return IRCError::NetworkDown;
|
|
case asio::error::network_reset:
|
|
return IRCError::NetworkReset;
|
|
case asio::error::network_unreachable:
|
|
return IRCError::NetworkUnreachable;
|
|
case asio::error::no_descriptors:
|
|
return IRCError::NoDescriptors;
|
|
case asio::error::timed_out:
|
|
return IRCError::TimedOut;
|
|
case asio::error::eof:
|
|
return IRCError::EndOfFile;
|
|
|
|
/*
|
|
* A 'default' here is needed since asio::error has a few errors I don't care about, or is able to care for...
|
|
*/
|
|
default:
|
|
lastAsioErrorCode = e.code().value();
|
|
lastAsioErrorMessage = e.code().message();
|
|
std::cerr << fmt::format("Unhandled exception {}: {}", lastAsioErrorCode, lastAsioErrorMessage) << std::endl;
|
|
return IRCError::UnhandledException;
|
|
}
|
|
}
|
|
|