TurtleBrains  0.3.5
High quality, portable, C++ framework for rapid 2D game development.
TurtleBrains::Core::Error Namespace Reference

Contains defines and functionality for protecting the framework and your project from disaster. More...

Classes

class  ErrorHandlerInterface
 Customize what happens when an error is fired. More...
 

Functions

void TriggerError (const char *const message,...)
 
void TriggerError (const char *const fromFile, int onLineNumber, const char *const message,...)
 
void TriggerErrorIf (bool testResult, const char *const message,...)
 
void TriggerErrorIf (bool testResult, const char *const fromFile, int onLineNumber, const char *const message,...)
 
void AddErrorHandler (ErrorHandlerInterface *errorHandler)
 
void RemoveErrorHandler (ErrorHandlerInterface *errorHandler)
 
bool IsThrowingOnError (void)
 
void EnableThrowOnError (void)
 
void DisableThrowOnError (void)
 
std::string GetErrorString (int errorNumber)
 

Detailed Description

Disasters will happen, they are natural after all. Mistakes will happen and to minimize the mistakes made from misusing a function or object can be protected by defensive programming. As much as possible, the functions and objects in TurtleBrains will trigger an error when misused. Several options exist for how a triggered error can be handled. Exceptions can be thrown, assertions fired, or a custom handler can be created using the ErrorHandlerInterface.

Function Documentation

◆ AddErrorHandler()

void TurtleBrains::Core::Error::AddErrorHandler ( ErrorHandlerInterface errorHandler)

Each ErrorHandlerInterface will have a chance to handle a fired error, in the order they were added unless the path of the code is modified by termination, exceptions or other means within a handler.

Parameters
errorHandlerThe errorHandler to be added, must be non-null.
Note
This may become an implementation detail that is not available to the programmer. TODO: TIM: Investigate TODO: TIM: Planning: Check if the ctor should register and unregister themselves in dtor.
#include "turtle_brains/core/tb_error.h"
#include <iostream>
class MyErrorHandler : public tbCore::Error::ErrorHandlerInterface
{
virtual void OnErrorFired(const std::string& errorMessage)
{
std::cout << "MyErrorHandler: Error in: " << GetFileName() << " on line: " << GetLineNumber() << std::endl;
std::cout << "\tWith message: " << assertMessage << std::endl;
}
};
static MyErrorHandler gsErrorHandler;
void main(int argumentCount, char* argumentValues[])
{
tbCore::Error::AddErrorHandler(&gsErrorHandler);
//The error will be triggered if the test is true. tb_error_if(test == true, "Information about error.");
tb_error_if(1 == 0, "This really should not trigger an error.");
tb_error_if(1 == 1, "This really should trigger an error.");
return 0;
}
#define tb_error_if(errorTest, message,...)
Definition: tb_error.hpp:42

◆ DisableThrowOnError()

void TurtleBrains::Core::Error::DisableThrowOnError ( void  )

Disables the exception throwing upon a triggered error condition, which can be enabled or disabled at runtime by calling this or EnableThrowOnError(). When disabled, the triggered error condition will be handled through the stack of errorHandlers added via AddErrorHandler().

See also
EnableThrowOnError, ErrorHandlerInterface, AddErrorHandler

◆ EnableThrowOnError()

void TurtleBrains::Core::Error::EnableThrowOnError ( void  )

Instead of firing each ErrorHandlerInterface when an error is fired, this will throw an exception which can be caught and handled by the application developer. By default, exception throwing is disabled.

See also
EnableThrowOnError, ErrorHandlerInterface, AddErrorHandler
#include "turtle_brains/core/tb_error.h"
#include <iostream>
void main(int argumentCount, char* argumentValues[])
{
tbCore::Error::EnableThrowOnError();
try
{
//The error will be triggered if the test is true. tb_error_if(test == true, "Information about error.");
tb_error_if(1 == 0, "This really should not throw an exception.");
tb_error_if(1 == 1, "This really should throw an exception.");
}
catch (const std::exception& except)
{
std::cout << "Caught exception: " << except.what() << std::endl;
}
return 0;
}

◆ IsThrowingOnError()

bool TurtleBrains::Core::Error::IsThrowingOnError ( void  )

Check to see if the error handler is to throw an exception instead of firing each ErrorHandler when an error condition is triggered.

Note
this can be enabled/disabled at runtime by calling EnabledThrowOnError() or DisableThrowOnError().

◆ RemoveErrorHandler()

void TurtleBrains::Core::Error::RemoveErrorHandler ( ErrorHandlerInterface errorHandler)

Removes a handler from the list of handlers.

Parameters
errorHandlerThe errorHandler to be added, must be non-null.
Note
This may become an implementation detail that is not available to the programmer. TODO: TIM: Investigate TODO: TIM: Planning: Check if the ctor should register and unregister themselves in dtor.

◆ TriggerError() [1/2]

void TurtleBrains::Core::Error::TriggerError ( const char *const  fromFile,
int  onLineNumber,
const char *const  message,
  ... 
)

Triggers an error condition so OnErrorFired() will be called and all error handlers will be notified.

◆ TriggerError() [2/2]

void TurtleBrains::Core::Error::TriggerError ( const char *const  message,
  ... 
)

Triggers an error condition so OnErrorFired() will be called and all error handlers will be notified.

◆ TriggerErrorIf() [1/2]

void TurtleBrains::Core::Error::TriggerErrorIf ( bool  testResult,
const char *const  fromFile,
int  onLineNumber,
const char *const  message,
  ... 
)

If testResult is true, OnErrorFired() will be called and all error handlers will be notified.

Note
Prefer use of the macro tb_error_if instead of directly calling the function so the file and line number information is correct.

◆ TriggerErrorIf() [2/2]

void TurtleBrains::Core::Error::TriggerErrorIf ( bool  testResult,
const char *const  message,
  ... 
)

If testResult is true, OnErrorFired() will be called and all error handlers will be notified.

Note
Prefer use of the macro tb_error_if instead of directly calling the function so the file and line number information is known.