async_mqtt 5.0.0
Loading...
Searching...
No Matches
exception.hpp
1// Copyright Takatoshi Kondo 2022
2//
3// Distributed under the Boost Software License, Version 1.0.
4// (See accompanying file LICENSE_1_0.txt or copy at
5// http://www.boost.org/LICENSE_1_0.txt)
6
7#if !defined(ASYNC_MQTT_EXCEPTION_HPP)
8#define ASYNC_MQTT_EXCEPTION_HPP
9
10#include <exception>
11#include <sstream>
12
13#include <boost/system/error_code.hpp>
14#include <boost/system/system_error.hpp>
15#include <boost/assert.hpp>
16#include <boost/operators.hpp>
17
18#include <async_mqtt/util/string_view.hpp>
19
20namespace async_mqtt {
21
22namespace sys = boost::system;
23using error_code = sys::error_code;
24namespace errc = sys::errc;
25
29struct system_error : sys::system_error, private boost::totally_ordered<system_error> {
30 using base_type = sys::system_error;
31 using base_type::base_type;
32
37 system_error(error_code const& ec = error_code())
38 : base_type{ec}
39 {}
40
41 // std::string what() const noexcept; return more detailed message
42 // It is defined in base_type
43
50 std::string message() const {
51 return code().message();
52 }
53
58 operator bool() const {
59 return code() != errc::success;
60 }
61};
62
63
72template <typename WhatArg>
73inline system_error make_error(errc::errc_t ec, WhatArg&& wa) {
74 return system_error(make_error_code(ec), std::forward<WhatArg>(wa));
75}
76
77inline bool operator==(system_error const& lhs, system_error const& rhs) {
78 return
79 std::tuple<error_code, string_view>(lhs.code(), lhs.what()) ==
80 std::tuple<error_code, string_view>(rhs.code(), rhs.what());
81}
82
83inline bool operator<(system_error const& lhs, system_error const& rhs) {
84 return
85 std::tuple<error_code, string_view>(lhs.code(), lhs.what()) <
86 std::tuple<error_code, string_view>(rhs.code(), rhs.what());
87}
88
89inline std::ostream& operator<<(std::ostream& o, system_error const& v) {
90 o << v.what();
91 return o;
92}
93
94} // namespace async_mqtt
95
96#endif // ASYNC_MQTT_EXCEPTION_HPP
Definition packet_variant.hpp:49
async_mqtt error class. It is used as CompletionToken parameter and exception.
Definition exception.hpp:29
system_error(error_code const &ec=error_code())
constructor
Definition exception.hpp:37
std::string message() const
get error message If you want to more detaied error message, call what() instead.
Definition exception.hpp:50
system_error make_error(errc::errc_t ec, WhatArg &&wa)
system_error factory function
Definition exception.hpp:73