$darkmode
error.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 Robert Bosch GmbH
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  * SPDX-License-Identifier: Apache-2.0
17  */
24 #pragma once
25 #ifndef CLOE_CORE_ERROR_HPP_
26 #define CLOE_CORE_ERROR_HPP_
27 
28 #include <stdexcept> // for exception
29 #include <string> // for string
30 
31 #include <cloe/core/logger.hpp> // for fmt::format
32 
33 namespace cloe {
34 
35 class Error : public std::exception {
36  public:
37  explicit Error(const std::string& what) : err_(what) {}
38  explicit Error(const char* what) : err_(what) {}
39 
40  template <typename... Args>
41  explicit Error(const char* format, const Args&... args) : err_(fmt::format(format, args...)) {}
42 
43  virtual ~Error() noexcept = default;
44 
45  const char* what() const noexcept override { return err_.what(); }
46 
47  bool has_explanation() const { return !explanation_.empty(); }
48 
49  void set_explanation(const std::string& explanation);
50 
51  template <typename... Args>
52  void set_explanation(const char* format, const Args&... args) {
53  set_explanation(fmt::format(format, args...));
54  }
55 
56  const std::string& explanation() const { return explanation_; }
57 
58  Error explanation(const std::string& explanation) && {
59  set_explanation(explanation);
60  return std::move(*this);
61  }
62 
63  template <typename... Args>
64  Error explanation(const char* format, const Args&... args) && {
65  set_explanation(fmt::format(format, args...));
66  return std::move(*this);
67  }
68 
69  private:
70  std::runtime_error err_;
71  std::string explanation_;
72 };
73 
80 class ConcludedError : public std::exception {
81  public:
82  explicit ConcludedError(std::exception& e) : cause_(e) {}
83  virtual ~ConcludedError() noexcept = default;
84 
85  std::exception& cause() const noexcept { return cause_; }
86  const char* what() const noexcept override { return cause_.what(); }
87 
88  private:
89  std::exception& cause_;
90 };
91 
92 } // namespace cloe
93 
94 #endif // CLOE_CORE_ERROR_HPP_
Definition: error.hpp:80
Definition: error.hpp:35
Definition: coordinator.hpp:36