ifw-daq  3.0.0-pre2
IFW Data Acquisition modules
error.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @ingroup daq_common_libdaq
4  * @copyright 2022 ESO - European Southern Observatory
5  *
6  * @brief Contains error related definitions for DAQ
7  */
8 #include <daq/error.hpp>
9 
10 #include <ostream>
11 
12 #include <fmt/format.h>
13 
14 #include <daq/error/report.hpp>
15 
16 namespace daq {
17 
18 std::ostream& operator<<(std::ostream& os, ErrorPolicy policy) {
19  switch (policy) {
21  os << "Strict";
22  break;
24  os << "Tolerant";
25  break;
26  default:
27  os << "n/a";
28  }
29  return os;
30 }
31 
32 DaqSourceError::DaqSourceError(std::string request, std::string source, std::string message)
33  : m_request(std::move(request)), m_source(std::move(source)), m_message(std::move(message)) {
34  m_formatted = fmt::format(
35  "Data source error: source={}, request={}, message={}", m_source, m_request, m_message);
36 }
37 
38 char const* DaqSourceError::what() const noexcept {
39  return m_formatted.c_str();
40 }
41 
42 DaqSourceErrors::DaqSourceErrors(std::vector<std::exception_ptr> errors) {
43  std::copy(errors.begin(), errors.end(), std::back_inserter(m_errors));
44  Update();
45 }
46 
48  std::vector<std::variant<DaqSourceError, std::exception_ptr>> errors)
49  : m_errors(std::move(errors)) {
50  Update();
51 }
52 
53 void DaqSourceErrors::Update() {
54  for (auto const& e : m_errors) {
55  if (auto const* ptr = std::get_if<DaqSourceError>(&e); ptr) {
56  m_what += ptr->what();
57  m_what += '\n';
58  } else if (auto const* ptr = std::get_if<std::exception_ptr>(&e); ptr) {
59  m_what += error::FormatException(*ptr);
60  }
61  }
62 }
63 
64 char const* DaqSourceErrors::what() const noexcept {
65  return m_what.c_str();
66 }
67 
68 } // namespace daq
std::string m_source
Definition: error.hpp:74
std::string m_message
Definition: error.hpp:75
char const * what() const noexcept override
Definition: error.cpp:38
DaqSourceError(std::string request, std::string source, std::string message)
Definition: error.cpp:32
std::string m_request
Definition: error.hpp:73
std::string m_formatted
Definition: error.hpp:76
char const * what() const noexcept override
Definition: error.cpp:64
DaqSourceErrors(std::vector< std::exception_ptr > errors)
Definition: error.cpp:42
std::vector< std::variant< DaqSourceError, std::exception_ptr > > m_errors
Definition: error.hpp:91
Contains error related declarations for DAQ.
void FormatException(std::ostream &os, std::exception_ptr ptr)
Report without nesting.
Definition: report.cpp:79
daqif::DaqStatus & operator<<(daqif::DaqStatus &status, daq::Status const &rhs)
Convert daq::Status -> daqif::DaqStatus by populating from rhs.
Definition: conversion.cpp:18
ErrorPolicy
Error policy supported by certain operations.
Definition: error.hpp:25
@ Strict
Any error is considered fatal and may lead to the operation being aborted.
@ Tolerant
Errors that can be ignored with partial completion of a command will be tolerated and is reported as ...