From: Alex Rousskov Date: Tue, 30 Sep 2008 16:21:43 +0000 (-0600) Subject: Made TextException a child of std::exception so that it is easier to catch X-Git-Tag: SQUID_3_1_0_1~45^2~16 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=0a8bbeebedd0ac6b6813890fab16d02bfb88ea4f;p=thirdparty%2Fsquid.git Made TextException a child of std::exception so that it is easier to catch more exceptions (standard and custom) with one catch(). The catching code usually does not care what the exception is anyway. TextException needs more work to report more information in what() method. Catch std::exception to catch more printable exceptions. TextException is an std::exception [child]. These changes were inspired by and required for eCAP. --- diff --git a/src/ICAP/AsyncJob.cc b/src/ICAP/AsyncJob.cc index 3f7b7439b7..f97454b98f 100644 --- a/src/ICAP/AsyncJob.cc +++ b/src/ICAP/AsyncJob.cc @@ -115,7 +115,7 @@ void AsyncJob::callStart(AsyncCall &call) typeName << " status in:" << status()); } -void AsyncJob::callException(const TextException &e) +void AsyncJob::callException(const std::exception &e) { // we must be called asynchronously and hence, the caller must lock us Must(cbdataReferenceValid(toCbdata())); @@ -212,9 +212,9 @@ JobDialer::dial(AsyncCall &call) try { doDial(); } - catch (const TextException &e) { + catch (const std::exception &e) { debugs(call.debugSection, 3, - HERE << call.name << " threw exception: " << e.message); + HERE << call.name << " threw exception: " << e.what()); job->callException(e); } diff --git a/src/ICAP/AsyncJob.h b/src/ICAP/AsyncJob.h index 6f3f635204..96a68450c9 100644 --- a/src/ICAP/AsyncJob.h +++ b/src/ICAP/AsyncJob.h @@ -30,7 +30,7 @@ * asynchronous calls. */ -class TextException; +class std::exception; /// \ingroup AsyncJobAPI class AsyncJob @@ -63,7 +63,7 @@ public: // asynchronous call maintenance bool canBeCalled(AsyncCall &call) const; void callStart(AsyncCall &call); - virtual void callException(const TextException &e); + virtual void callException(const std::exception &e); virtual void callEnd(); protected: diff --git a/src/ICAP/ICAPModXact.cc b/src/ICAP/ICAPModXact.cc index f6c39d0744..ff0412efdc 100644 --- a/src/ICAP/ICAPModXact.cc +++ b/src/ICAP/ICAPModXact.cc @@ -566,7 +566,7 @@ void ICAPModXact::parseMore() parseBody(); } -void ICAPModXact::callException(const TextException &e) +void ICAPModXact::callException(const std::exception &e) { if (!canStartBypass || isRetriable) { ICAPXaction::callException(e); @@ -575,10 +575,10 @@ void ICAPModXact::callException(const TextException &e) try { debugs(93, 3, "bypassing ICAPModXact::" << inCall << " exception: " << - e.message << ' ' << status()); + e.what() << ' ' << status()); bypassFailure(); } - catch (const TextException &bypassE) { + catch (const std::exception &bypassE) { ICAPXaction::callException(bypassE); } } diff --git a/src/ICAP/ICAPModXact.h b/src/ICAP/ICAPModXact.h index 9b60baa866..1c07fec3be 100644 --- a/src/ICAP/ICAPModXact.h +++ b/src/ICAP/ICAPModXact.h @@ -158,7 +158,7 @@ public: protected: // bypasses exceptions if needed and possible - virtual void callException(const TextException &e); + virtual void callException(const std::exception &e); private: virtual void start(); diff --git a/src/TextException.cc b/src/TextException.cc index 3300e20d3a..ada4f5c6ba 100644 --- a/src/TextException.cc +++ b/src/TextException.cc @@ -5,11 +5,17 @@ TextException::TextException(const char *aMsg, const char *aFileName, int aLineN message(xstrdup(aMsg)), theFileName(aFileName), theLineNo(aLineNo) {} -TextException::~TextException() +TextException::~TextException() throw() { xfree(message); } +const char *TextException::what() const throw() +{ + /// \todo add file:lineno + return message ? message : "TextException without a message"; +} + void Throw(const char *message, const char *fileName, int lineNo) { diff --git a/src/TextException.h b/src/TextException.h index 3a78dadf2d..6e4ae9abb7 100644 --- a/src/TextException.h +++ b/src/TextException.h @@ -7,14 +7,14 @@ // simple exception to report custom errors // we may want to change the interface to be able to report system errors -class TextException +class TextException: public std::exception { public: TextException(const char *aMessage, const char *aFileName = 0, int aLineNo = -1); - ~TextException(); + virtual ~TextException() throw(); - // ostream &print(ostream &os) const; + virtual const char *what() const throw(); public: char *message; // read-only diff --git a/src/http.cc b/src/http.cc index 0db0fd268d..c940a4927d 100644 --- a/src/http.cc +++ b/src/http.cc @@ -62,8 +62,8 @@ #define SQUID_EXIT_THROWING_CODE(status) \ status = true; \ } \ - catch (const TextException &e) { \ - debugs (11, 1, "Exception error:" << e.message); \ + catch (const std::exception &e) { \ + debugs (11, 1, "Exception error:" << e.what()); \ status = false; \ }