made all getWhat() methods on exceptions consistent. they now

all use format() the same way.  also changed format() to actually
do formatting.  however, it doesn't try looking up formatting
strings by id, it just uses the fallback format string.
This commit is contained in:
crs 2002-07-25 17:52:40 +00:00
parent 1fd8e25f7d
commit f129841b38
10 changed files with 59 additions and 37 deletions

View File

@ -1,5 +1,6 @@
#include "XBase.h"
#include <cerrno>
#include <cstdarg>
// win32 wants a const char* argument to std::exception c'tor
#if WINDOWS_LIKE
@ -46,9 +47,22 @@ XBase::what() const
CString
XBase::format(const char* /*id*/, const char* fmt, ...) const throw()
{
// FIXME -- use id to lookup formating string
// FIXME -- format string with arguments
return fmt;
// FIXME -- lookup message string using id as an index. set
// fmt to that string if it exists.
// format
CString result;
va_list args;
va_start(args, fmt);
try {
result = CStringUtil::vformat(fmt, args);
}
catch (...) {
// ignore
}
va_end(args);
return result;
}

View File

@ -48,20 +48,16 @@ XHTTP::addHeaders(CHTTPReply&) const
CString
XHTTP::getWhat() const throw()
{
try {
std::ostringstream s;
s << m_status << " ";
if (!m_reason.empty()) {
s << m_reason.c_str();
}
else {
s << getReason(m_status);
}
return s.str();
const char* reason;
if (m_reason.empty()) {
reason = getReason(m_status);
}
catch (...) {
return CString();
else {
reason = m_reason.c_str();
}
return format("XHTTP", "%{1} %{2}",
CStringUtil::print("%d", m_status).c_str(),
reason);
}
const char*

View File

@ -24,7 +24,7 @@ XIOErrno::XIOErrno(int err) :
CString
XIOClose::getWhat() const throw()
{
return format("XIOClose", "close: %1", XIOErrno::getErrstr());
return format("XIOClose", "close: %{1}", XIOErrno::getErrstr());
}

View File

@ -49,8 +49,9 @@ CString
XNetworkVersion::getWhat() const throw()
{
return format("XNetworkVersion",
"unsupported network version %d.%d",
m_major, m_minor);
"unsupported network version %{1}.%{2}",
CStringUtil::print("%d", m_major).c_str(),
CStringUtil::print("%d", m_minor).c_str());
}
@ -73,6 +74,6 @@ CString
XNetworkFunctionUnavailable::getWhat() const throw()
{
return format("XNetworkFunctionUnavailable",
"missing network function %s",
"missing network function %{1}",
m_name.c_str());
}

View File

@ -34,12 +34,21 @@ XSocketAddress::getPort() const throw()
CString
XSocketAddress::getWhat() const throw()
{
return "no address";
/* FIXME
return format("XSocketAddress", "no address: %1:%2",
m_hostname.t_str(),
CString::sprintf("%d", m_port).t_str());
*/
static const char* s_errorID[] = {
"XSocketAddressUnknown",
"XSocketAddressNotFound",
"XSocketAddressNoAddress",
"XSocketAddressBadPort"
};
static const char* s_errorMsg[] = {
"unknown error for: %{1}:%{2}",
"address not found for: %{1}",
"no address for: %{1}",
"invalid port: %{2}"
};
return format(s_errorID[m_error], s_errorMsg[m_error],
m_hostname.c_str(),
CStringUtil::print("%d", m_port).c_str());
}

View File

@ -695,5 +695,5 @@ XConfigRead::~XConfigRead()
CString
XConfigRead::getWhat() const throw()
{
return m_error;
return format("XConfigRead", "read error: %s", m_error.c_str());
}

View File

@ -336,9 +336,9 @@ parse(int argc, const char** argv)
try {
s_synergyAddress = CNetworkAddress(argv[i + 1], kDefaultPort);
}
catch (XSocketAddress&) {
log((CLOG_PRINT "%s: invalid address for `%s'" BYE,
pname, argv[i], pname));
catch (XSocketAddress& e) {
log((CLOG_PRINT "%s: invalid address for `%s'. %s." BYE,
pname, argv[i], e.what(), pname));
bye(kExitArgs);
}
++i;
@ -349,9 +349,9 @@ parse(int argc, const char** argv)
try {
s_httpAddress = CNetworkAddress(argv[i + 1], kDefaultPort + 1);
}
catch (XSocketAddress&) {
log((CLOG_PRINT "%s: invalid address for `%s'" BYE,
pname, argv[i], pname));
catch (XSocketAddress& e) {
log((CLOG_PRINT "%s: invalid address for `%s'. %s." BYE,
pname, argv[i], e.what(), pname));
bye(kExitArgs);
}
++i;

View File

@ -370,5 +370,5 @@ CProtocolUtil::read(IInputStream* stream, void* vbuffer, UInt32 count)
CString
XIOReadMismatch::getWhat() const throw()
{
return "CProtocolUtil::readf() mismatch";
return format("XIOReadMismatch", "CProtocolUtil::readf() mismatch");
}

View File

@ -7,5 +7,5 @@
CString
XScreenOpenFailure::getWhat() const throw()
{
return "XScreenOpenFailure";
return format("XScreenOpenFailure", "unable to open screen");
}

View File

@ -37,7 +37,9 @@ XIncompatibleClient::getMinor() const throw()
CString
XIncompatibleClient::getWhat() const throw()
{
return "XIncompatibleClient";
return format("XIncompatibleClient", "incompatible client %{1}.%{2}",
CStringUtil::print("%d", m_major).c_str(),
CStringUtil::print("%d", m_minor).c_str());
}
@ -60,7 +62,7 @@ XDuplicateClient::getName() const throw()
CString
XDuplicateClient::getWhat() const throw()
{
return "XDuplicateClient";
return format("XDuplicateClient", "duplicate client %{1}", m_name.c_str());
}
@ -83,5 +85,5 @@ XUnknownClient::getName() const throw()
CString
XUnknownClient::getWhat() const throw()
{
return "XUnknownClient";
return format("XUnknownClient", "unknown client %{1}", m_name.c_str());
}