LCOV - code coverage report
Current view: top level - libMagAOX/common - exceptions.hpp (source / functions) Coverage Total Hit
Test: MagAOX Lines: 77.8 % 9 7
Test Date: 2026-01-03 21:03:39 Functions: 66.7 % 3 2

            Line data    Source code
       1              : /** \file exceptions.hpp
       2              :  * \brief Exception helpers
       3              :  */
       4              : 
       5              : #ifndef common_exceptions_hpp
       6              : #define common_exceptions_hpp
       7              : 
       8              : #include <exception>
       9              : #include <source_location>
      10              : #include <format>
      11              : 
      12              : namespace MagAOX
      13              : {
      14              : 
      15              : /** \defgroup exceptions
      16              :  * \ingroup common
      17              :  *
      18              :  * @{
      19              :  */
      20              : 
      21              : /// Augments an exception with the source file and line
      22              : /**
      23              :  * \tparam baseexcept is the base class exception which takes a string as constructor argument
      24              :  */
      25              : class xwcException : public std::exception
      26              : {
      27              :   protected:
      28              :     std::string m_what; ///< The full what message (message + file information).
      29              : 
      30              :     std::string m_message; ///< The explanatory message
      31              : 
      32              :     std::source_location m_location;
      33              : 
      34              :     int m_code{ 0 }; ///< An error code (optional)
      35              : 
      36              :   public:
      37              :     xwcException() = delete;
      38              : 
      39              :     /// Constructor
      40              :     /**
      41              :      * The what() message becomes "msg (file line)".
      42              :      */
      43              :     explicit
      44            7 :     xwcException( const std::string         &msg, /**<[in] the error descriptionat message) */
      45              :                   const std::source_location loc = std::source_location::current() )
      46            7 :         : m_what{ std::format( "{} ({} {})", msg, loc.file_name(), loc.line() ) }, m_message{ msg }, m_location{ loc }
      47              :     {
      48            7 :     }
      49              : 
      50              :     /// Constructor with code
      51              :     /**
      52              :      * The what() message becomes "msg (file line)".
      53              :      */
      54           19 :     xwcException( const std::string         &msg,  /**<[in] the error description (what message) */
      55              :                   int                        code, /**<[in] a descriptive error code */
      56              :                   const std::source_location loc =
      57              :                       std::source_location::current() /**< [in] [optional] the location of this call */ )
      58           19 :         : m_what{ std::format( "{} code: {} ({} {})", msg, code, loc.file_name(), loc.line() ) }, m_message{ msg },
      59           19 :           m_location{ loc }, m_code{ code }
      60              :     {
      61           19 :     }
      62              : 
      63              :     /// Get the what string
      64              :     /** \returns the value of m_what.c_str()
      65              :      *
      66              :      */
      67            0 :     virtual const char *what() const noexcept
      68              :     {
      69            0 :         return m_what.c_str();
      70              :     }
      71              : 
      72              :     /// Get the message
      73              :     /** \returns the value of m_message
      74              :      *
      75              :      */
      76              :     const std::string &message() const
      77              :     {
      78              :         return m_message;
      79              :     }
      80              : 
      81              :     /// Get the source file
      82              :     /** \returns the value of m_location.file_name()
      83              :      *
      84              :      */
      85              :     const std::string file_name() const
      86              :     {
      87              :         return m_location.file_name();
      88              :     }
      89              : 
      90              :     /// Get the source line
      91              :     /** \returns the value of m_location.line()
      92              :      *
      93              :      */
      94              :     int line() const
      95              :     {
      96              :         return m_location.line();
      97              :     }
      98              : 
      99              :     /// Get the error code
     100              :     /** \returns the value of m_code
     101              :      *
     102              :      */
     103              :     int code() const
     104              :     {
     105              :         return m_code;
     106              :     }
     107              : };
     108              : 
     109              : } // namespace MagAOX
     110              : 
     111              : ///@}
     112              : 
     113              : #endif // common_exceptions_hpp
        

Generated by: LCOV version 2.0-1