/lw/error/Error.h
C Header | 185 lines | 120 code | 41 blank | 24 comment | 1 complexity | 655980ef590f85669bbd73b83ed2e6bf MD5 | raw file
- ///
- /// @file lw/error/Error.h
- ///
- /// A basic error class which all other errors in the LW Common library build upon.
- ///
- /// @author Oz <oz@lifewanted.com>
- ///
- #pragma once
- #include <exception>
- #include <sstream>
- #include <string>
- #include <type_traits>
- #include "lw/defines.h"
- #include "lw/error/errorCodes.h"
- namespace lw {
- namespace error {
- #define LW_ERROR( ErrorClass, message ) \
- static_assert( \
- std::is_base_of< lw::error::Error, ErrorClass >::value, \
- #ErrorClass " is not an LW Error." \
- ); \
- throw ErrorClass( message, __PRETTY_FUNCTION__, __FILE__, __LINE__ )
- class Error : public virtual std::exception {
- public:
- typedef std::exception_ptr Pointer;
- private:
- s64 m_errorCode;
- std::string m_errorName;
- std::string m_message;
- std::string m_function;
- std::string m_file;
- u16 m_line;
- public:
- Error( void );
- Error(
- const std::string& message,
- const std::string& function,
- const std::string& file,
- const u16 line
- );
- Error(
- const s64 errorCode,
- const std::string& errorName,
- const std::string& message,
- const std::string& function,
- const std::string& file,
- const u16 line
- );
- const char* what( void ) const LW_NOEXCEPT LW_OVERRIDE;
- virtual const std::string whatStr( void ) const LW_NOEXCEPT;
- const s64& getErrorCode( void ) const LW_NOEXCEPT;
- const std::string& getErrorName( void ) const LW_NOEXCEPT;
- const std::string& getMessage( void ) const LW_NOEXCEPT;
- const std::string& getFunction( void ) const LW_NOEXCEPT;
- const std::string& getFile( void ) const LW_NOEXCEPT;
- std::string getFileName( void ) const LW_NOEXCEPT;
- const u16& getLineNumber( void ) const LW_NOEXCEPT;
- operator bool( void ) const LW_NOEXCEPT;
- bool operator!( void ) const LW_NOEXCEPT;
- }; // end class Error
- // -------------------------------------------------------------------------- //
- inline Error::Error( void ): Error( lw::error::Code::None, "", "", "", "", 0 ){
- // This space intentionally left blank.
- }
- // -------------------------------------------------------------------------- //
- inline Error::Error(
- const std::string& message,
- const std::string& function,
- const std::string& file,
- const u16 line
- ):
- Error( lw::error::Code::Generic, "Generic", message, function, file, line )
- {
- // This space intentionally left blank.
- }
- // -------------------------------------------------------------------------- //
- inline Error::Error(
- const s64 errorCode,
- const std::string& errorName,
- const std::string& message,
- const std::string& function,
- const std::string& file,
- const u16 line
- ):
- m_errorCode( errorCode ),
- m_errorName( errorName ),
- m_message( message ),
- m_function( function ),
- m_file( file ),
- m_line( line )
- {
- // This space intentionally left blank.
- }
- // -------------------------------------------------------------------------- //
- inline const char* Error::what( void ) const LW_NOEXCEPT {
- return whatStr().c_str();
- }
- // -------------------------------------------------------------------------- //
- inline const std::string Error::whatStr( void ) const LW_NOEXCEPT {
- std::stringstream stream;
- stream
- << getErrorName() << " Error (" << getErrorCode() << ") [" << getFileName() << ":"
- << getLineNumber() << "]: " << getMessage();
- return stream.str();
- }
- // -------------------------------------------------------------------------- //
- inline const s64& Error::getErrorCode( void ) const LW_NOEXCEPT {
- return m_errorCode;
- }
- // -------------------------------------------------------------------------- //
- inline const std::string& Error::getErrorName( void ) const LW_NOEXCEPT {
- return m_errorName;
- }
- // -------------------------------------------------------------------------- //
- inline const std::string& Error::getMessage( void ) const LW_NOEXCEPT {
- return m_message;
- }
- // -------------------------------------------------------------------------- //
- inline const std::string& Error::getFunction( void ) const LW_NOEXCEPT {
- return m_function;
- }
- // -------------------------------------------------------------------------- //
- inline const std::string& Error::getFile( void ) const LW_NOEXCEPT {
- return m_file;
- }
- // -------------------------------------------------------------------------- //
- inline std::string Error::getFileName( void ) const LW_NOEXCEPT {
- const std::size_t slashPos = m_file.rfind( '/' );
- return slashPos == std::string::npos ? m_file : m_file.substr( slashPos + 1 );
- }
- // -------------------------------------------------------------------------- //
- inline const u16& Error::getLineNumber( void ) const LW_NOEXCEPT {
- return m_line;
- }
- // -------------------------------------------------------------------------- //
- inline Error::operator bool( void ) const LW_NOEXCEPT {
- return (bool)m_errorCode;
- }
- // -------------------------------------------------------------------------- //
- inline bool Error::operator!( void ) const LW_NOEXCEPT {
- return !(bool)m_errorCode;
- }
- } // end namespace error
- } // end namespace lw