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
22
23#include <string_view>
24
25namespace toolbox {
26inline namespace util {
27
29constexpr std::size_t MaxErrSize{511};
30
31struct ErrMsg {
33 operator std::string_view() const noexcept {
34 return os_.str();
35 }
36};
37
38template <typename ValueT>
40{
41 err.os_ << std::forward<ValueT>(val);
42 return err;
43}
44
45class TOOLBOX_API Exception : public std::runtime_error {
46 public:
47 explicit Exception(std::error_code ec = std::error_code());
48 Exception(int err, const std::error_category& ecat);
49 Exception(std::error_code ec, std::string_view what);
50 Exception(int err, const std::error_category& ecat, std::string_view what);
51 ~Exception() override;
52
53 // Copy.
54 Exception(const Exception&) = default;
55 Exception& operator=(const Exception&) = default;
56
57 // Move.
58 Exception(Exception&&) noexcept = default;
59 Exception& operator=(Exception&&) noexcept = default;
60
65 static void to_json(std::ostream& os, int code, const char* message);
66
71 static void to_json(std::ostream& os, const std::error_code& code, const char* message)
72 {
73 to_json(os, code.value(), message);
74 }
75
78 void to_json(std::ostream& os) const { to_json(os, ec_, what()); }
79
81 const std::error_code& code() const noexcept { return ec_; }
82
83 private:
84 std::error_code ec_;
85};
86
87namespace detail {
88
89template <typename ExceptionT>
90struct PutAsJson {
91 const ExceptionT* e;
92};
93
94
95template <typename ExceptionT, typename StreamT>
96 requires Streamable<StreamT>
98{
99 val.e->to_json(os);
100 return os;
101}
102
103template <typename ExceptionT>
104struct PutWithCode {
105 const ExceptionT* e;
106};
107
108template <typename ExceptionT, typename StreamT>
109 requires Streamable<StreamT>
111{
112 os << val.e->what() << " (" << val.e->code().value() << ')';
113 return os;
114}
115
116} // namespace detail
117
118template <typename ExceptionT>
120{
121 return detail::PutAsJson<ExceptionT>{.e = &e};
122}
123
124template <typename ExceptionT>
126{
127 return detail::PutWithCode<ExceptionT>{.e = &e};
128}
129
134
135} // namespace util
136} // namespace toolbox
137
138#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:81
Exception & operator=(const Exception &)=default
Exception(Exception &&) noexcept=default
void to_json(std::ostream &os) const
Definition Exception.hpp:78
std::string_view str() const noexcept
Definition Stream.hpp:197
STL namespace.
ostream & operator<<(ostream &os, const pair< T, U > &p)
Definition Parser.ut.cpp:29
StreamT & operator<<(StreamT &os, PutPercentiles pp)
Definition Utility.hpp:68
ErrMsg & err_msg() noexcept
Definition Exception.cpp:57
auto put_as_json(const ExceptionT &e)
constexpr std::size_t MaxErrSize
Maximum error message length.
Definition Exception.hpp:29
auto put_with_code(const ExceptionT &e)
constexpr auto bind() noexcept
Definition Slot.hpp:92
OStaticStream< MaxErrSize > os_
Definition Exception.hpp:32