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 "XBase.h"
#include <cerrno> #include <cerrno>
#include <cstdarg>
// win32 wants a const char* argument to std::exception c'tor // win32 wants a const char* argument to std::exception c'tor
#if WINDOWS_LIKE #if WINDOWS_LIKE
@ -46,9 +47,22 @@ XBase::what() const
CString CString
XBase::format(const char* /*id*/, const char* fmt, ...) const throw() XBase::format(const char* /*id*/, const char* fmt, ...) const throw()
{ {
// FIXME -- use id to lookup formating string // FIXME -- lookup message string using id as an index. set
// FIXME -- format string with arguments // fmt to that string if it exists.
return fmt;
// 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 CString
XHTTP::getWhat() const throw() XHTTP::getWhat() const throw()
{ {
try { const char* reason;
std::ostringstream s; if (m_reason.empty()) {
s << m_status << " "; reason = getReason(m_status);
if (!m_reason.empty()) {
s << m_reason.c_str();
}
else {
s << getReason(m_status);
}
return s.str();
} }
catch (...) { else {
return CString(); reason = m_reason.c_str();
} }
return format("XHTTP", "%{1} %{2}",
CStringUtil::print("%d", m_status).c_str(),
reason);
} }
const char* const char*

View File

@ -24,7 +24,7 @@ XIOErrno::XIOErrno(int err) :
CString CString
XIOClose::getWhat() const throw() 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() XNetworkVersion::getWhat() const throw()
{ {
return format("XNetworkVersion", return format("XNetworkVersion",
"unsupported network version %d.%d", "unsupported network version %{1}.%{2}",
m_major, m_minor); CStringUtil::print("%d", m_major).c_str(),
CStringUtil::print("%d", m_minor).c_str());
} }
@ -73,6 +74,6 @@ CString
XNetworkFunctionUnavailable::getWhat() const throw() XNetworkFunctionUnavailable::getWhat() const throw()
{ {
return format("XNetworkFunctionUnavailable", return format("XNetworkFunctionUnavailable",
"missing network function %s", "missing network function %{1}",
m_name.c_str()); m_name.c_str());
} }

View File

@ -34,12 +34,21 @@ XSocketAddress::getPort() const throw()
CString CString
XSocketAddress::getWhat() const throw() XSocketAddress::getWhat() const throw()
{ {
return "no address"; static const char* s_errorID[] = {
/* FIXME "XSocketAddressUnknown",
return format("XSocketAddress", "no address: %1:%2", "XSocketAddressNotFound",
m_hostname.t_str(), "XSocketAddressNoAddress",
CString::sprintf("%d", m_port).t_str()); "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 CString
XConfigRead::getWhat() const throw() 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 { try {
s_synergyAddress = CNetworkAddress(argv[i + 1], kDefaultPort); s_synergyAddress = CNetworkAddress(argv[i + 1], kDefaultPort);
} }
catch (XSocketAddress&) { catch (XSocketAddress& e) {
log((CLOG_PRINT "%s: invalid address for `%s'" BYE, log((CLOG_PRINT "%s: invalid address for `%s'. %s." BYE,
pname, argv[i], pname)); pname, argv[i], e.what(), pname));
bye(kExitArgs); bye(kExitArgs);
} }
++i; ++i;
@ -349,9 +349,9 @@ parse(int argc, const char** argv)
try { try {
s_httpAddress = CNetworkAddress(argv[i + 1], kDefaultPort + 1); s_httpAddress = CNetworkAddress(argv[i + 1], kDefaultPort + 1);
} }
catch (XSocketAddress&) { catch (XSocketAddress& e) {
log((CLOG_PRINT "%s: invalid address for `%s'" BYE, log((CLOG_PRINT "%s: invalid address for `%s'. %s." BYE,
pname, argv[i], pname)); pname, argv[i], e.what(), pname));
bye(kExitArgs); bye(kExitArgs);
} }
++i; ++i;

View File

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

View File

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

View File

@ -37,7 +37,9 @@ XIncompatibleClient::getMinor() const throw()
CString CString
XIncompatibleClient::getWhat() const throw() 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 CString
XDuplicateClient::getWhat() const throw() 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 CString
XUnknownClient::getWhat() const throw() XUnknownClient::getWhat() const throw()
{ {
return "XUnknownClient"; return format("XUnknownClient", "unknown client %{1}", m_name.c_str());
} }