Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
Exception.hpp
Go to the documentation of this file.
1// The Reactive C++ Toolbox.
2// Copyright (C) 2013-2019 Swirly Cloud Limited
3// Copyright (C) 2022 Reactive Markets Limited
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17#ifndef TOOLBOX_UTIL_EXCEPTION_HPP
18#define TOOLBOX_UTIL_EXCEPTION_HPP
19
21
22namespace toolbox {
23inline namespace util {
24
26constexpr std::size_t MaxErrSize{511};
27
29
30class TOOLBOX_API Exception : public std::runtime_error {
31 public:
32 explicit Exception(std::error_code ec = std::error_code());
33 Exception(int err, const std::error_category& ecat);
34 Exception(std::error_code ec, std::string_view what);
35 Exception(int err, const std::error_category& ecat, std::string_view what);
36 ~Exception() override;
37
38 // Copy.
39 Exception(const Exception&) = default;
40 Exception& operator=(const Exception&) = default;
41
42 // Move.
43 Exception(Exception&&) noexcept = default;
44 Exception& operator=(Exception&&) noexcept = default;
45
50 static void to_json(std::ostream& os, int code, const char* message);
51
56 static void to_json(std::ostream& os, const std::error_code& code, const char* message)
57 {
58 to_json(os, code.value(), message);
59 }
60
63 void to_json(std::ostream& os) const { to_json(os, ec_, what()); }
64
66 const std::error_code& code() const noexcept { return ec_; }
67
68 private:
69 std::error_code ec_;
70};
71
72namespace detail {
73
74template <typename ExceptionT>
75struct PutAsJson {
76 const ExceptionT* e;
77};
78
79template <typename ExceptionT>
80std::ostream& operator<<(std::ostream& os, PutAsJson<ExceptionT> val)
81{
82 val.e->to_json(os);
83 return os;
84}
85
86template <typename ExceptionT>
87struct PutWithCode {
88 const ExceptionT* e;
89};
90
91template <typename ExceptionT>
92std::ostream& operator<<(std::ostream& os, PutWithCode<ExceptionT> val)
93{
94 return os << val.e->what() << " (" << val.e->code().value() << ')';
95}
96
97} // namespace detail
98
99template <typename ExceptionT>
101{
102 return detail::PutAsJson<ExceptionT>{.e = &e};
103}
104
105template <typename ExceptionT>
107{
108 return detail::PutWithCode<ExceptionT>{.e = &e};
109}
110
115
116} // namespace util
117} // namespace toolbox
118
119#endif // TOOLBOX_UTIL_EXCEPTION_HPP
#define TOOLBOX_API
Definition Config.h:39
Exception(const Exception &)=default
const std::error_code & code() const noexcept
Returns the error code.
Definition Exception.hpp:66
Exception & operator=(const Exception &)=default
Exception(Exception &&) noexcept=default
void to_json(std::ostream &os) const
Definition Exception.hpp:63
STL namespace.
ostream & operator<<(ostream &os, PutPercentiles pp)
Definition Utility.cpp:96
ErrMsg & err_msg() noexcept
Definition Exception.cpp:57
OStaticStream< MaxErrSize > ErrMsg
Definition Exception.hpp:28
auto put_as_json(const ExceptionT &e)
constexpr std::size_t MaxErrSize
Maximum error message length.
Definition Exception.hpp:26
auto put_with_code(const ExceptionT &e)
constexpr auto bind() noexcept
Definition Slot.hpp:92