LCOV - code coverage report
Current view: top level - src - Exception.cpp (source / functions) Hit Total Coverage
Test: lcov.info Lines: 24 26 92.3 %
Date: 2025-04-27 01:14:20 Functions: 7 7 100.0 %
Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            : ** Copyright (C) 2020 CERN
       3                 :            : **
       4                 :            : ** This software is provided 'as-is', without any express or implied
       5                 :            : ** warranty.  In no event will the authors be held liable for any damages
       6                 :            : ** arising from the use of this software.
       7                 :            : **
       8                 :            : ** Permission is granted to anyone to use this software for any purpose,
       9                 :            : ** including commercial applications, and to alter it and redistribute it
      10                 :            : ** freely, subject to the following restrictions:
      11                 :            : **
      12                 :            : ** 1. The origin of this software must not be misrepresented; you must not
      13                 :            : **    claim that you wrote the original software. If you use this software
      14                 :            : **    in a product, an acknowledgment in the product documentation would be
      15                 :            : **    appreciated but is not required.
      16                 :            : ** 2. Altered source versions must be plainly marked as such, and must not be
      17                 :            : **    misrepresented as being the original software.
      18                 :            : ** 3. This notice may not be removed or altered from any source distribution.
      19                 :            : **
      20                 :            : ** Created on: 2021-10-18T18:44:27+02:00
      21                 :            : **     Author: Matteo Ferrari <matteof> <matteo.ferrari.1@cern.ch>
      22                 :            : **
      23                 :            : */
      24                 :            : 
      25                 :            : #include "Exception.hpp"
      26                 :            : 
      27                 :            : #include <cerrno>
      28                 :            : #include <cstring>
      29                 :            : #include <sstream>
      30                 :            : #include <vector>
      31                 :            : 
      32                 :            : namespace ccut {
      33                 :            : 
      34                 :            : const std::string Exception::Category = "CCUT";
      35                 :            : 
      36                 :          4 : Exception::Exception(std::error_code ev) : m_errorCode{ev} {}
      37                 :            : 
      38                 :         81 : Exception::Exception(std::error_code ev, const std::string &what) :
      39                 :         81 :     m_errorCode{ev},
      40                 :         81 :     m_what(what)
      41                 :         81 : {}
      42                 :            : 
      43                 :          7 : const char *Exception::what() const noexcept
      44                 :            : {
      45                 :          7 :     if (m_what.empty())
      46                 :          3 :         m_what = m_errorCode.message();
      47                 :          7 :     return m_what.c_str();
      48                 :            : }
      49                 :            : 
      50                 :            : struct ErrorCategory : std::error_category
      51                 :            : {
      52                 :            :     const char *name() const noexcept override;
      53                 :            :     std::string message(int ev) const override;
      54                 :            : };
      55                 :            : 
      56                 :          2 : const char *ErrorCategory::name() const noexcept
      57                 :            : {
      58                 :          2 :     return Exception::Category.c_str();
      59                 :            : }
      60                 :            : 
      61                 :          3 : std::string ErrorCategory::message(int ev) const
      62                 :            : {
      63                 :          3 :     if (ev == 0)
      64                 :          0 :         return "Ok";
      65                 :          3 :     else if (ev < 1000)
      66                 :          2 :         return ::strerror(ev);
      67                 :            : 
      68                 :          1 :     switch (static_cast<ErrorCode>(ev))
      69                 :            :     {
      70                 :          1 :     case ErrorCode::InternalError: return "Internal error";
      71                 :          0 :     default: return "Unknown error";
      72                 :            :     }
      73                 :            : }
      74                 :            : 
      75                 :            : const ErrorCategory errorCategory{};
      76                 :            : 
      77                 :        119 : std::error_code make_error_code(ErrorCode e)
      78                 :            : {
      79                 :        119 :     return {static_cast<int>(e), errorCategory};
      80                 :            : }
      81                 :            : 
      82                 :          9 : Exception make_errno_exception()
      83                 :            : {
      84                 :          9 :     std::vector<char> buf(256);
      85                 :            : #if (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE
      86                 :            :     ::strerror_r(errno, buf.data(), 256);
      87                 :            :     const char *error = buf.data();
      88                 :            : #else
      89                 :          9 :     const char *error = ::strerror_r(errno, buf.data(), 256);
      90                 :            : #endif
      91                 :            : 
      92                 :         18 :     return Exception(static_cast<ErrorCode>(errno), error);
      93                 :          9 : }
      94                 :            : 
      95                 :            : } // namespace ccut

Generated by: LCOV version 1.14