From 4394594518a7046d03d14c14fdcf40850ebd9275 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Sat, 28 Sep 2013 18:15:00 -0700 Subject: [PATCH] Tidy up some use of Error for throw --- .../beast_core/diagnostic/ProtectedCall.cpp | 26 +--------------- .../memory/FifoFreeStoreWithTLS.cpp | 2 +- .../memory/FifoFreeStoreWithoutTLS.cpp | 4 +-- modules/beast_core/memory/PagedFreeStore.cpp | 6 ++-- modules/beast_core/memory/PagedFreeStore.h | 2 +- modules/beast_core/misc/Result.cpp | 2 +- modules/beast_core/thread/Listeners.cpp | 2 +- .../beast_sqdb/api/type_conversion_traits.h | 4 +-- modules/beast_sqdb/source/into_type.cpp | 15 +++++----- modules/beast_sqdb/source/session.cpp | 30 +++++++------------ modules/beast_sqdb/source/statement_imp.cpp | 5 ++-- modules/beast_sqdb/source/transaction.cpp | 6 ++-- modules/beast_sqdb/source/use_type.cpp | 5 ++-- 13 files changed, 34 insertions(+), 75 deletions(-) diff --git a/modules/beast_core/diagnostic/ProtectedCall.cpp b/modules/beast_core/diagnostic/ProtectedCall.cpp index b1aab675e..99de06789 100644 --- a/modules/beast_core/diagnostic/ProtectedCall.cpp +++ b/modules/beast_core/diagnostic/ProtectedCall.cpp @@ -34,10 +34,6 @@ namespace vf namespace { -// -// While this object is in scope, any Windows SEH -// exceptions will be caught and re-thrown as an Error object. -// class ScopedPlatformExceptionCatcher : public Uncopyable { public: @@ -155,7 +151,7 @@ public: break; } - Throw (Error ().fail (__FILE__, __LINE__, s, Error::platform)); + beast_reportFatalError (s, __FILE__, __LINE__); } return s_sehPrev (ei); @@ -218,26 +214,6 @@ bool CatchAny (Function f, bool returnFromException) caughtException = false; } - catch (Error& e) - { - if (!returnFromException) - { - JUCEApplication* app = JUCEApplication::getInstance (); - - if (app) - { - app->unhandledException ( - &e, - e.getSourceFilename (), - e.getLineNumber ()); - } - else - { - std::cout << e.what (); - std::unexpected (); - } - } - } catch (std::exception& e) { if (!returnFromException) diff --git a/modules/beast_core/memory/FifoFreeStoreWithTLS.cpp b/modules/beast_core/memory/FifoFreeStoreWithTLS.cpp index f01ffab69..d277ba02b 100644 --- a/modules/beast_core/memory/FifoFreeStoreWithTLS.cpp +++ b/modules/beast_core/memory/FifoFreeStoreWithTLS.cpp @@ -115,7 +115,7 @@ public: const size_t bytesNeeded = headerBytes + bytes; if (bytesNeeded > m_allocator.m_pages->getPageBytes ()) - Throw (Error ().fail (__FILE__, __LINE__, TRANS ("the memory request was too large"))); + fatal_error ("the memory request was too large"); Header* header; diff --git a/modules/beast_core/memory/FifoFreeStoreWithoutTLS.cpp b/modules/beast_core/memory/FifoFreeStoreWithoutTLS.cpp index 07502eefd..da07d46ff 100644 --- a/modules/beast_core/memory/FifoFreeStoreWithoutTLS.cpp +++ b/modules/beast_core/memory/FifoFreeStoreWithoutTLS.cpp @@ -144,7 +144,7 @@ FifoFreeStoreWithoutTLS::FifoFreeStoreWithoutTLS () : m_pages (GlobalPagedFreeStore::getInstance ()) { if (m_pages->getPageBytes () < sizeof (Block) + 256) - Throw (Error ().fail (__FILE__, __LINE__, TRANS ("the block size is too small"))); + fatal_error ("the block size is too small"); m_active = newBlock (); } @@ -161,7 +161,7 @@ void* FifoFreeStoreWithoutTLS::allocate (const size_t bytes) const size_t actual = sizeof (Header) + bytes; if (actual > m_pages->getPageBytes ()) - Throw (Error ().fail (__FILE__, __LINE__, TRANS ("the memory request was too large"))); + fatal_error ("the memory request was too large"); Header* h; diff --git a/modules/beast_core/memory/PagedFreeStore.cpp b/modules/beast_core/memory/PagedFreeStore.cpp index 8081ae4bf..05b92ba34 100644 --- a/modules/beast_core/memory/PagedFreeStore.cpp +++ b/modules/beast_core/memory/PagedFreeStore.cpp @@ -123,16 +123,14 @@ void* PagedFreeStore::allocate () const bool exhausted = m_newPagesLeft.release (); if (exhausted) - Throw (Error ().fail (__FILE__, __LINE__, - TRANS ("the limit of memory allocations was reached"))); + fatal_error ("the limit of memory allocations was reached"); #endif void* storage = ::malloc (m_pageBytes); if (!storage) - Throw (Error ().fail (__FILE__, __LINE__, - TRANS ("a memory allocation failed"))); + fatal_error ("a memory allocation failed"); page = new (storage) Page (this); diff --git a/modules/beast_core/memory/PagedFreeStore.h b/modules/beast_core/memory/PagedFreeStore.h index d39eeee32..bcad772c0 100644 --- a/modules/beast_core/memory/PagedFreeStore.h +++ b/modules/beast_core/memory/PagedFreeStore.h @@ -46,7 +46,7 @@ public: inline void* allocate (const size_t bytes) { if (bytes > m_pageBytes) - Throw (Error ().fail (__FILE__, __LINE__, "the size is too large")); + fatal_error ("the size is too large"); return allocate (); } diff --git a/modules/beast_core/misc/Result.cpp b/modules/beast_core/misc/Result.cpp index a3ad74447..cf04bbc07 100644 --- a/modules/beast_core/misc/Result.cpp +++ b/modules/beast_core/misc/Result.cpp @@ -64,7 +64,7 @@ bool Result::operator!= (const Result& other) const noexcept Result Result::fail (const String& errorMessage) noexcept { - return Result (errorMessage.isEmpty() ? "Unknown Error" : errorMessage); + return Result (errorMessage.isEmpty() ? "Unknown error" : errorMessage); } const String& Result::getErrorMessage() const noexcept diff --git a/modules/beast_core/thread/Listeners.cpp b/modules/beast_core/thread/Listeners.cpp index 72924a8f8..be45f02c0 100644 --- a/modules/beast_core/thread/Listeners.cpp +++ b/modules/beast_core/thread/Listeners.cpp @@ -380,7 +380,7 @@ ListenersBase::Proxy::Proxy (void const* const member, const size_t bytes) : m_bytes (bytes) { if (bytes > maxMemberBytes) - Throw (Error ().fail (__FILE__, __LINE__, "the Proxy member is too large")); + fatal_error ("the Proxy member is too large"); memcpy (m_member, member, bytes); } diff --git a/modules/beast_sqdb/api/type_conversion_traits.h b/modules/beast_sqdb/api/type_conversion_traits.h index 7742051d4..c7b478641 100644 --- a/modules/beast_sqdb/api/type_conversion_traits.h +++ b/modules/beast_sqdb/api/type_conversion_traits.h @@ -72,9 +72,7 @@ struct type_conversion static void from_base(base_type const& in, indicator ind, T& out) { // null not allowed - if (ind == i_null) - Throw(Error().fail(__FILE__, __LINE__)); - + check_precondition (ind != i_null); out = in; } diff --git a/modules/beast_sqdb/source/into_type.cpp b/modules/beast_sqdb/source/into_type.cpp index 3eb7c4c2f..d3bae1258 100644 --- a/modules/beast_sqdb/source/into_type.cpp +++ b/modules/beast_sqdb/source/into_type.cpp @@ -97,8 +97,7 @@ void standard_into_type::do_into() if (colType == SQLITE_NULL) { // null encountered with no indicator - if (!m_ind) - Throw(Error().fail(__FILE__, __LINE__)); + check_precondition (m_ind != nullptr); *m_ind = i_null; } @@ -164,7 +163,7 @@ void standard_into_type::do_into() break; default: - Throw(Error().fail(__FILE__, __LINE__)); + fatal_error ("unknown case"); } } break; @@ -188,7 +187,7 @@ void standard_into_type::do_into() break; default: - Throw(Error().fail(__FILE__, __LINE__)); + fatal_error ("unknown case"); }; } break; @@ -208,7 +207,7 @@ void standard_into_type::do_into() break; case x_stdwstring: - Throw(Error().fail(__FILE__, __LINE__)); + fatal_error ("invalid case"); break; case x_beastString: @@ -276,7 +275,7 @@ void standard_into_type::do_into() break; default: - Throw(Error().fail(__FILE__, __LINE__)); + fatal_error ("unknown case"); } } @@ -286,10 +285,10 @@ void standard_into_type::do_into() break; case SQLITE_BLOB: - Throw(Error().fail(__FILE__, __LINE__)); + fatal_error ("invalid case"); default: - Throw(Error().fail(__FILE__, __LINE__)); + fatal_error ("unknown case"); }; } diff --git a/modules/beast_sqdb/source/session.cpp b/modules/beast_sqdb/source/session.cpp index 1b6706afe..6e18c66eb 100644 --- a/modules/beast_sqdb/source/session.cpp +++ b/modules/beast_sqdb/source/session.cpp @@ -67,14 +67,12 @@ public: { int threadSafe = sqlite3_threadsafe(); - if (threadSafe == 0) - Throw(Error().fail(__FILE__, __LINE__, Error::assertFailed)); + check_precondition (threadSafe != 0); #if 0 int result = sqlite3_config(SQLITE_CONFIG_MULTITHREAD); - if (result != SQLITE_OK) - Throw(Error().fail(__FILE__, __LINE__, Error::assertFailed)); + check_postcondition (result == SQLITE_OK); #endif sqlite3_initialize(); @@ -115,8 +113,7 @@ session::~session() Error session::clone() { - if (m_connection) - Throw(Error().fail(__FILE__, __LINE__)); // already open + check_precondition (! m_connection); return open(m_fileName, m_connectString); } @@ -134,8 +131,7 @@ Error session::open(String fileName, std::string options) Error err; // can't open twice - if (m_connection) - Throw(err.fail(__FILE__, __LINE__, Error::fileInUse)); + check_precondition (! m_connection); int mode = 0; int flags = 0; @@ -183,13 +179,12 @@ Error session::open(String fileName, std::string options) } else { - Throw(err.fail(__FILE__, __LINE__, Error::badParameter)); + fatal_error ("bad parameter"); } } else { - // duplicate - Throw(err.fail(__FILE__, __LINE__, Error::badParameter)); + fatal_error ("duplicate parameter"); } } @@ -210,13 +205,12 @@ Error session::open(String fileName, std::string options) } else { - Throw(err.fail(__FILE__, __LINE__, Error::badParameter)); + fatal_error ("bad parameter"); } } else { - // duplicate - Throw(err.fail(__FILE__, __LINE__, Error::badParameter)); + fatal_error ("duplicate parameter"); } } @@ -235,19 +229,17 @@ Error session::open(String fileName, std::string options) } else { - Throw(err.fail(__FILE__, __LINE__, Error::badParameter)); + fatal_error ("bad parameter"); } } else { - // duplicate - Throw(err.fail(__FILE__, __LINE__, Error::badParameter)); + fatal_error ("duplicate parameter"); } } else { - // unknown option - Throw(err.fail(__FILE__, __LINE__, Error::badParameter)); + fatal_error ("unknown parameter"); } } diff --git a/modules/beast_sqdb/source/statement_imp.cpp b/modules/beast_sqdb/source/statement_imp.cpp index 8c2933d5f..501678def 100644 --- a/modules/beast_sqdb/source/statement_imp.cpp +++ b/modules/beast_sqdb/source/statement_imp.cpp @@ -180,8 +180,7 @@ Error statement_imp::execute() { Error error; - if (!m_stmt) - Throw(Error().fail(__FILE__, __LINE__, Error::badParameter)); + check_precondition (m_stmt != nullptr); // ??? m_bGotData = false; @@ -253,7 +252,7 @@ bool statement_imp::fetch(Error& error) else { // should never get SQLITE_OK here - Throw(Error().fail(__FILE__, __LINE__, Error::assertFailed)); + fatal_error ("invalid result"); } return m_bGotData; diff --git a/modules/beast_sqdb/source/transaction.cpp b/modules/beast_sqdb/source/transaction.cpp index 6d767e1f7..2999be25f 100644 --- a/modules/beast_sqdb/source/transaction.cpp +++ b/modules/beast_sqdb/source/transaction.cpp @@ -77,8 +77,7 @@ Error transaction::commit() Error error; // already handled - if (m_bHandled) - Throw(Error().fail(__FILE__, __LINE__)); + check_precondition (!m_bHandled); // if commit() throws, m_bHandled will remain false // and the destructor will attempt a rollback. @@ -93,8 +92,7 @@ Error transaction::commit() void transaction::rollback() { // already handled - if (m_bHandled) - Throw(Error().fail(__FILE__, __LINE__)); + check_precondition (!m_bHandled); m_session.rollback(); m_bHandled = true; diff --git a/modules/beast_sqdb/source/use_type.cpp b/modules/beast_sqdb/source/use_type.cpp index 3c8b05227..70db82548 100644 --- a/modules/beast_sqdb/source/use_type.cpp +++ b/modules/beast_sqdb/source/use_type.cpp @@ -87,8 +87,7 @@ inline T const& as(void const* v) { T const& val = *static_cast (v); - if (val > T(std::numeric_limits::max())) - Throw(Error().fail(__FILE__, __LINE__)); + check_precondition (val <= T(std::numeric_limits::max())); return val; } @@ -203,7 +202,7 @@ void standard_use_type::do_use() case x_stdtm: case x_blob: default: - Throw(Error().fail(__FILE__, __LINE__, Error::badParameter)); + fatal_error ("bad parameter"); } if (result != SQLITE_OK)