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 { return os_.str(); }
34};
35
36template <typename ValueT>
38{
39 err.os_ << std::forward<ValueT>(val);
40 return err;
41}
42
43class TOOLBOX_API Exception : public std::runtime_error {
44 public:
45 explicit Exception(std::error_code ec = std::error_code());
46 Exception(int err, const std::error_category& ecat);
47 Exception(std::error_code ec, std::string_view what);
48 Exception(int err, const std::error_category& ecat, std::string_view what);
49 ~Exception() override;
50
51 // Copy.
52 Exception(const Exception&) = default;
53 Exception& operator=(const Exception&) = default;
54
55 // Move.
56 Exception(Exception&&) noexcept = default;
57 Exception& operator=(Exception&&) noexcept = default;
58
63 static void to_json(std::ostream& os, int code, const char* message);
64
69 static void to_json(std::ostream& os, const std::error_code& code, const char* message)
70 {
71 to_json(os, code.value(), message);
72 }
73
76 void to_json(std::ostream& os) const { to_json(os, ec_, what()); }
77
79 const std::error_code& code() const noexcept { return ec_; }
80
81 private:
82 std::error_code ec_;
83};
84
85namespace detail {
86
87template <typename ExceptionT>
88struct PutAsJson {
89 const ExceptionT* e;
90};
91
92template <typename ExceptionT, typename StreamT>
93 requires Streamable<StreamT>
95{
96 val.e->to_json(os);
97 return os;
98}
99
100template <typename ExceptionT>
101struct PutWithCode {
102 const ExceptionT* e;
103};
104
105template <typename ExceptionT, typename StreamT>
106 requires Streamable<StreamT>
108{
109 os << val.e->what() << " (" << val.e->code().value() << ')';
110 return os;
111}
112
113} // namespace detail
114
115template <typename ExceptionT>
117{
118 return detail::PutAsJson<ExceptionT>{.e = &e};
119}
120
121template <typename ExceptionT>
123{
124 return detail::PutWithCode<ExceptionT>{.e = &e};
125}
126
131
132} // namespace util
133} // namespace toolbox
134
135#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:79
Exception & operator=(const Exception &)=default
Exception(Exception &&) noexcept=default
void to_json(std::ostream &os) const
Definition Exception.hpp:76
std::string_view str() const noexcept
Definition Stream.hpp:161
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:97
OStaticStream< MaxErrSize > os_
Definition Exception.hpp:32