Skip to content

Commit

Permalink
Updated exception handling. Introduced StackWalker to extract stack t…
Browse files Browse the repository at this point in the history
…races rather than having catch spread throughout the code.
  • Loading branch information
martinknafve committed Sep 24, 2014
1 parent 2f62d3a commit 8c3d8a3
Show file tree
Hide file tree
Showing 39 changed files with 4,476 additions and 2,057 deletions.
2,667 changes: 1,883 additions & 784 deletions hmailserver/installation/License.rtf

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "stdafx.h"
#include "ErrorManager.h"


#include <oledb.h>


Expand Down Expand Up @@ -113,7 +114,7 @@ namespace HM
ErrorManager::ReportError(eSeverity iSeverity, int iErrorID, const String &sSource, const String &sDescription, const std::exception &error)
{
String formatted_message;
formatted_message.Format(_T("%s, Error code: %d, Message: %s"), sDescription, error.what());
formatted_message.Format(_T("%s, Error code: %d, Message: %s"), sDescription, String(error.what()));

ReportError(iSeverity, iErrorID, sSource, formatted_message);
}
Expand Down
9 changes: 4 additions & 5 deletions hmailserver/source/Server/Common/Application/Errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,13 @@
5323, TCPConnection::_StartAsyncConnect, An unknown error occurred while starting asynchronous connect.
5324, TCPConnection::AsyncConnectCompleted, An unknown error occurred while handling asynchronous connect.
5325, TCPConnection::_StartAsyncConnect, An unknown error occurred while starting connection.
5326, TCPConnection::_StartAsyncConnect, An unknown error occurred while handling handshake.
5327, TCPConnection::EnqueueWrite, An unknown error occurred while posting write buffer.
5328, TCPConnection::EnqueueWrite, An unknown error occurred while posting write buffer.
5329, TCPConnection::Write, An unknown error occurred while writing buffer.
5330, TCPConnection::Shutdown, An unknown error occurred while shutting down socket.
5331, TCPConnection::EnqueueRead, An unknown error occurred while posting read buffer.
5332, TCPConnection::EnqueueRead, An unknown error occurred while starting async reading
5333, TCPConnection::UpdateLogoutTimer, An unknown error occurred while updating logout timer.
5334, TCPConnection::CancelLogoutTimer, An unknown error occurred while canceling logout timer.
5335, TCPConnection::EnqueueDisconnect, An unknown error occurred while posting disconnect.
5336, TCPConnection::Disconnect, An unknown error occurred while disconnecting.
5337, TCPConnection::OnTimeout, An unknown error occurred while disconnecting.
5338, TCPConnection::EnqueueShutdown, An unknown error occurred while posting shutdown.
5339, TCPConnection::AsyncWriteCompleted, An unknown error occurred while handling buffer write.
Expand Down Expand Up @@ -112,4 +108,7 @@
5513, BIO_read returned an unexpected number of characters. Expected: {0}, Returned: {1}
5514, Unable to find appropriate MySQL character set.
5515, Failed to enable lingering for session {0}
5516, Attempted DNS lookup for empty host name.
5516, Attempted DNS lookup for empty host name.
5518, An error occured when accepting a connection
5519, An error has been detected and an error stack will be generated and saved in {0}.
5520, Failed to open socket on IP address {0}.
94 changes: 94 additions & 0 deletions hmailserver/source/Server/Common/Application/ExceptionHandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright (c) 2010 Martin Knafve / hMailServer.com.
// http://www.hmailserver.com

#include "StdAfx.h"

#include "ExceptionHandler.h"
#include "../Util/StackLogger.h"


#ifdef _DEBUG
#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
#define new DEBUG_NEW
#endif

namespace HM
{
ExceptionHandler::ExceptionHandler(void)
{

}

LONG WINAPI ExceptionFilterWithLogging(EXCEPTION_POINTERS* pExp, DWORD dwExpCode)
{
StackLogger::Log(dwExpCode, pExp->ContextRecord);

return EXCEPTION_EXECUTE_HANDLER;
}

bool
ExceptionHandler::Run(const String &descriptive_name, boost::function<void()>& func)
{
__try
{
RunWithStandardExceptions(descriptive_name, func);
return true;
}
__except (ExceptionFilterWithLogging(GetExceptionInformation(), GetExceptionCode()))
{
// this has been logged in the exception filter.
return false;
}
}

void
ExceptionHandler::RunWithStandardExceptions(const String &descriptive_name, boost::function<void()>& func)
{
try
{
func();
}
catch (thread_interrupted const&)
{
// shutting down
}
catch (boost::system::system_error error)
{
ErrorManager::Instance()->ReportError(ErrorManager::High, 4208, "ExceptionHandler::Run", GetExceptionText(descriptive_name), error);

throw;
}
catch (std::exception const& error)
{
String sErrorMessage =
Formatter::Format("An error occured while executing '{0}'", descriptive_name);

ErrorManager::Instance()->ReportError(ErrorManager::High, 4208, "ExceptionHandler::Run", GetExceptionText(descriptive_name), error);

throw;
}
catch (...)
{
String sErrorMessage =
Formatter::Format("An error occured while executing '{0}'", descriptive_name);

ErrorManager::Instance()->ReportError(ErrorManager::High, 4208, "ExceptionHandler::Run", GetExceptionText(descriptive_name));

throw;
}
}



String
ExceptionHandler::GetExceptionText(const String &descriptive_name)
{
String sErrorMessage =
Formatter::Format("An error occured while executing '{0}'", descriptive_name);

return sErrorMessage;

}


}
22 changes: 22 additions & 0 deletions hmailserver/source/Server/Common/Application/ExceptionHandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) 2010 Martin Knafve / hMailServer.com.
// http://www.hmailserver.com

#pragma once

namespace HM
{
class ExceptionHandler
{
public:
ExceptionHandler();

static bool Run(const String &descriptive_name, boost::function<void()>& functionToRun);

private:

static void RunWithStandardExceptions(const String &descriptive_name, boost::function<void()>& functionToRun);

static String GetExceptionText(const String &descriptive_name);

};
}
35 changes: 17 additions & 18 deletions hmailserver/source/Server/Common/Application/MessageIndexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "StdAfx.h"

#include "MessageIndexer.h"

#include "../Application/ExceptionHandler.h"
#include "../BO/Message.h"
#include "../BO/MessageMetaData.h"
#include "../MIME/MIME.h"
Expand Down Expand Up @@ -52,30 +54,27 @@ namespace HM
void
MessageIndexer::WorkerFunc()
{
LOG_DEBUG("Indexing messages...");
boost::function<void()> func = boost::bind( &MessageIndexer::WorkerFuncInternal, this );
if (ExceptionHandler::Run("MessageIndexer", func))
return;
}

try
{
PersistentMessageMetaData persistentMetaData;
persistentMetaData.DeleteOrphanedItems();
void
MessageIndexer::WorkerFuncInternal()
{
LOG_DEBUG("Indexing messages...");

while (true)
{
IndexMessages_();
PersistentMessageMetaData persistentMetaData;
persistentMetaData.DeleteOrphanedItems();

index_now_.WaitFor(chrono::minutes(1));
}
}
catch (thread_interrupted const&)
while (true)
{
LOG_DEBUG("Indexing stopped.");
IndexMessages_();

// shutting down.
}
catch (...)
{
ErrorManager::Instance()->ReportError(ErrorManager::Medium, 5158, "MessageIndexer::DoWork", "An error occured while indexing messages. The indexing was aborted.");
index_now_.WaitFor(chrono::minutes(1));
}


}

void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace HM


void WorkerFunc();
void WorkerFuncInternal();

void IndexMessages_();

Expand Down
2 changes: 1 addition & 1 deletion hmailserver/source/Server/Common/Application/Scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ namespace HM
if (dtRunTime <= dtNow)
{
// Yup, we should run this task now.
pTask->DoWork();
pTask->Run();

// Update run time.
pTask->SetNextRunTime();
Expand Down
Loading

0 comments on commit 8c3d8a3

Please sign in to comment.