LCOV - code coverage report
Current view: top level - wirestead/diagnostics - exceptions.hpp (source / functions) Coverage Total Hit
Test: Wirestead Coverage Report Lines: 100.0 % 6 6
Test Date: 2026-08-30 10:35:09 Functions: 100.0 % 3 3
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /*
       2                 :             :  * Copyright 2025 Jinwoo Sung
       3                 :             :  *
       4                 :             :  * Licensed under the Apache License, Version 2.0 (the "License");
       5                 :             :  * you may not use this file except in compliance with the License.
       6                 :             :  * You may obtain a copy of the License at
       7                 :             :  *
       8                 :             :  *     http://www.apache.org/licenses/LICENSE-2.0
       9                 :             :  *
      10                 :             :  * Unless required by applicable law or agreed to in writing, software
      11                 :             :  * distributed under the License is distributed on an "AS IS" BASIS,
      12                 :             :  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      13                 :             :  * See the License for the specific language governing permissions and
      14                 :             :  * limitations under the License.
      15                 :             :  */
      16                 :             : 
      17                 :             : #pragma once
      18                 :             : 
      19                 :             : #include <stdexcept>
      20                 :             : #include <string>
      21                 :             : 
      22                 :             : #include "wirestead/base/visibility.hpp"
      23                 :             : 
      24                 :             : namespace wirestead {
      25                 :             : namespace diagnostics {
      26                 :             : 
      27                 :             : /**
      28                 :             :  * @brief Base exception class for all wirestead exceptions
      29                 :             :  *
      30                 :             :  * Provides a common base for all exceptions thrown by the wirestead library.
      31                 :             :  * Includes additional context information for better error reporting.
      32                 :             :  */
      33                 :             : class WIRESTEAD_API WiresteadException : public std::runtime_error {
      34                 :             :  public:
      35                 :         112 :   explicit WiresteadException(const std::string& message, const std::string& component = "",
      36                 :             :                               const std::string& operation = "")
      37                 :         112 :       : std::runtime_error(message), component_(component), operation_(operation) {}
      38                 :             : 
      39                 :             :   virtual ~WiresteadException() noexcept;
      40                 :             : 
      41                 :             :   const std::string& component() const noexcept { return component_; }
      42                 :             :   const std::string& operation() const noexcept { return operation_; }
      43                 :             : 
      44                 :             :   std::string full_message() const {
      45                 :             :     if (component_.empty() && operation_.empty()) {
      46                 :             :       return what();
      47                 :             :     }
      48                 :             :     if (operation_.empty()) {
      49                 :             :       return "[" + component_ + "] " + what();
      50                 :             :     }
      51                 :             :     if (component_.empty()) {
      52                 :             :       return std::string(what()) + " (operation: " + operation_ + ")";
      53                 :             :     }
      54                 :             :     return "[" + component_ + "] " + what() + " (operation: " + operation_ + ")";
      55                 :             :   }
      56                 :             : 
      57                 :             :  private:
      58                 :             :   std::string component_;
      59                 :             :   std::string operation_;
      60                 :             : };
      61                 :             : 
      62                 :             : /**
      63                 :             :  * @brief Exception thrown during builder operations
      64                 :             :  *
      65                 :             :  * Indicates errors that occur during the construction or configuration
      66                 :             :  * of communication channels using the Builder pattern.
      67                 :             :  */
      68                 :             : class WIRESTEAD_API BuilderException : public WiresteadException {
      69                 :             :  public:
      70                 :          13 :   explicit BuilderException(const std::string& message, const std::string& builder_type = "",
      71                 :             :                             const std::string& operation = "")
      72                 :          39 :       : WiresteadException(message, "builder", operation), builder_type_(builder_type) {}
      73                 :             : 
      74                 :             :   ~BuilderException() noexcept override;
      75                 :             : 
      76                 :             :   const std::string& builder_type() const noexcept { return builder_type_; }
      77                 :             : 
      78                 :             :   std::string full_message() const {
      79                 :             :     if (builder_type_.empty()) {
      80                 :             :       return WiresteadException::full_message();
      81                 :             :     }
      82                 :             :     return "[" + builder_type_ + "] " + WiresteadException::full_message();
      83                 :             :   }
      84                 :             : 
      85                 :             :  private:
      86                 :             :   std::string builder_type_;
      87                 :             : };
      88                 :             : 
      89                 :             : /**
      90                 :             :  * @brief Exception thrown during input validation
      91                 :             :  *
      92                 :             :  * Indicates that input parameters failed validation checks.
      93                 :             :  * Provides detailed information about what validation failed.
      94                 :             :  */
      95                 :             : class WIRESTEAD_API ValidationException : public WiresteadException {
      96                 :             :  public:
      97                 :          97 :   explicit ValidationException(const std::string& message, const std::string& parameter = "",
      98                 :             :                                const std::string& expected = "")
      99                 :         485 :       : WiresteadException(message, "validation", "validate"), parameter_(parameter), expected_(expected) {}
     100                 :             : 
     101                 :             :   ~ValidationException() noexcept override;
     102                 :             : 
     103                 :             :   const std::string& parameter() const noexcept { return parameter_; }
     104                 :             :   const std::string& expected() const noexcept { return expected_; }
     105                 :             : 
     106                 :             :   std::string full_message() const {
     107                 :             :     if (parameter_.empty() && expected_.empty()) {
     108                 :             :       return WiresteadException::full_message();
     109                 :             :     }
     110                 :             :     if (expected_.empty()) {
     111                 :             :       return WiresteadException::full_message() + " (parameter: " + parameter_ + ")";
     112                 :             :     }
     113                 :             :     if (parameter_.empty()) {
     114                 :             :       return WiresteadException::full_message() + " (expected: " + expected_ + ")";
     115                 :             :     }
     116                 :             :     return WiresteadException::full_message() + " (parameter: " + parameter_ + ") (expected: " + expected_ + ")";
     117                 :             :   }
     118                 :             : 
     119                 :             :  private:
     120                 :             :   std::string parameter_;
     121                 :             :   std::string expected_;
     122                 :             : };
     123                 :             : 
     124                 :             : /**
     125                 :             :  * @brief Exception thrown during memory operations
     126                 :             :  *
     127                 :             :  * Indicates errors related to memory allocation, deallocation,
     128                 :             :  * or memory safety violations.
     129                 :             :  */
     130                 :             : class WIRESTEAD_API MemoryException : public WiresteadException {
     131                 :             :  public:
     132                 :             :   explicit MemoryException(const std::string& message, size_t size = 0, const std::string& operation = "")
     133                 :             :       : WiresteadException(message, "memory", operation), size_(size) {}
     134                 :             : 
     135                 :             :   ~MemoryException() noexcept override;
     136                 :             : 
     137                 :             :   size_t size() const noexcept { return size_; }
     138                 :             : 
     139                 :             :   std::string full_message() const {
     140                 :             :     if (size_ == 0) {
     141                 :             :       return WiresteadException::full_message();
     142                 :             :     }
     143                 :             :     return WiresteadException::full_message() + " (size: " + std::to_string(size_) + " bytes)";
     144                 :             :   }
     145                 :             : 
     146                 :             :  private:
     147                 :             :   size_t size_;
     148                 :             : };
     149                 :             : 
     150                 :             : /**
     151                 :             :  * @brief Exception thrown during connection operations
     152                 :             :  *
     153                 :             :  * Indicates errors that occur during network or serial connection
     154                 :             :  * establishment, maintenance, or teardown.
     155                 :             :  */
     156                 :             : class WIRESTEAD_API ConnectionException : public WiresteadException {
     157                 :             :  public:
     158                 :             :   explicit ConnectionException(const std::string& message, const std::string& connection_type = "",
     159                 :             :                                const std::string& operation = "")
     160                 :             :       : WiresteadException(message, "connection", operation), connection_type_(connection_type) {}
     161                 :             : 
     162                 :             :   ~ConnectionException() noexcept override;
     163                 :             : 
     164                 :             :   const std::string& connection_type() const noexcept { return connection_type_; }
     165                 :             : 
     166                 :             :   std::string full_message() const {
     167                 :             :     if (connection_type_.empty()) {
     168                 :             :       return WiresteadException::full_message();
     169                 :             :     }
     170                 :             :     return "[" + connection_type_ + "] " + WiresteadException::full_message();
     171                 :             :   }
     172                 :             : 
     173                 :             :  private:
     174                 :             :   std::string connection_type_;
     175                 :             : };
     176                 :             : 
     177                 :             : /**
     178                 :             :  * @brief Exception thrown during configuration operations
     179                 :             :  *
     180                 :             :  * Indicates errors that occur during configuration loading,
     181                 :             :  * validation, or application.
     182                 :             :  */
     183                 :             : class WIRESTEAD_API ConfigurationException : public WiresteadException {
     184                 :             :  public:
     185                 :             :   explicit ConfigurationException(const std::string& message, const std::string& config_section = "",
     186                 :             :                                   const std::string& operation = "")
     187                 :             :       : WiresteadException(message, "configuration", operation), config_section_(config_section) {}
     188                 :             : 
     189                 :             :   ~ConfigurationException() noexcept override;
     190                 :             : 
     191                 :             :   const std::string& config_section() const noexcept { return config_section_; }
     192                 :             : 
     193                 :             :   std::string full_message() const {
     194                 :             :     if (config_section_.empty()) {
     195                 :             :       return WiresteadException::full_message();
     196                 :             :     }
     197                 :             :     return WiresteadException::full_message() + " (section: " + config_section_ + ")";
     198                 :             :   }
     199                 :             : 
     200                 :             :  private:
     201                 :             :   std::string config_section_;
     202                 :             : };
     203                 :             : 
     204                 :             : using UnilinkException = WiresteadException;
     205                 :             : 
     206                 :             : }  // namespace diagnostics
     207                 :             : 
     208                 :             : }  // namespace wirestead
        

Generated by: LCOV version 2.0-1