]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | /* | |
4 | * Ceph - scalable distributed file system | |
5 | * | |
6 | * Copyright (C) 2017 Red Hat, Inc. <contact@redhat.com> | |
7 | * | |
8 | * Author: Adam C. Emerson <aemerson@redhat.com> | |
9 | * | |
10 | * This is free software; you can redistribute it and/or modify it | |
11 | * under the terms of the GNU Lesser General Public License version | |
12 | * 2.1, as published by the Free Software Foundation. See file | |
13 | * COPYING. | |
14 | */ | |
15 | ||
16 | #ifndef COMMON_CEPH_ERROR_CODE | |
17 | #define COMMON_CEPH_ERROR_CODE | |
18 | ||
19 | #include <netdb.h> | |
20 | ||
21 | #include <boost/system/error_code.hpp> | |
22 | #include <boost/asio.hpp> | |
23 | ||
24 | #pragma GCC diagnostic push | |
25 | #pragma GCC diagnostic ignored "-Wnon-virtual-dtor" | |
26 | #pragma clang diagnostic push | |
27 | #pragma clang diagnostic ignored "-Wnon-virtual-dtor" | |
28 | ||
29 | namespace ceph { | |
30 | ||
31 | // This is for error categories we define, so we can specify the | |
32 | // equivalent integral value at the point of definition. | |
33 | class converting_category : public boost::system::error_category { | |
34 | public: | |
35 | virtual int from_code(int code) const noexcept = 0; | |
36 | }; | |
37 | ||
38 | const boost::system::error_category& ceph_category() noexcept; | |
39 | ||
40 | enum class errc { | |
41 | not_in_map = 1, // The requested item was not found in the map | |
42 | does_not_exist, // Item does not exist | |
43 | failure, // An internal fault or inconsistency | |
44 | exists, // Already exists | |
45 | limit_exceeded, // Attempting to use too much of something | |
46 | auth, // May not be an auth failure. It could be that the | |
47 | // preconditions to attempt auth failed. | |
48 | conflict, // Conflict or precondition failure | |
49 | }; | |
50 | } | |
51 | ||
52 | namespace boost::system { | |
53 | template<> | |
54 | struct is_error_condition_enum<::ceph::errc> { | |
55 | static const bool value = true; | |
56 | }; | |
57 | template<> | |
58 | struct is_error_code_enum<::ceph::errc> { | |
59 | static const bool value = false; | |
60 | }; | |
61 | } | |
62 | ||
63 | namespace ceph { | |
64 | // explicit conversion: | |
65 | inline boost::system::error_code make_error_code(errc e) noexcept { | |
66 | return { static_cast<int>(e), ceph_category() }; | |
67 | } | |
68 | ||
69 | // implicit conversion: | |
70 | inline boost::system::error_condition make_error_condition(errc e) noexcept { | |
71 | return { static_cast<int>(e), ceph_category() }; | |
72 | } | |
73 | ||
74 | [[nodiscard]] boost::system::error_code to_error_code(int ret) noexcept; | |
75 | [[nodiscard]] int from_error_code(boost::system::error_code e) noexcept; | |
76 | } | |
77 | #pragma GCC diagnostic pop | |
78 | #pragma clang diagnostic pop | |
79 | ||
80 | // Moved here from buffer.h so librados doesn't gain a dependency on | |
81 | // Boost.System | |
82 | ||
83 | namespace ceph::buffer { | |
84 | inline namespace v15_2_0 { | |
85 | const boost::system::error_category& buffer_category() noexcept; | |
86 | enum class errc { bad_alloc = 1, | |
87 | end_of_buffer, | |
88 | malformed_input }; | |
89 | } | |
90 | } | |
91 | ||
92 | namespace boost::system { | |
93 | template<> | |
94 | struct is_error_code_enum<::ceph::buffer::errc> { | |
95 | static const bool value = true; | |
96 | }; | |
97 | ||
98 | template<> | |
99 | struct is_error_condition_enum<::ceph::buffer::errc> { | |
100 | static const bool value = false; | |
101 | }; | |
102 | } | |
103 | ||
104 | namespace ceph::buffer { | |
105 | inline namespace v15_2_0 { | |
106 | ||
107 | // implicit conversion: | |
108 | inline boost::system::error_code make_error_code(errc e) noexcept { | |
109 | return { static_cast<int>(e), buffer_category() }; | |
110 | } | |
111 | ||
112 | // explicit conversion: | |
113 | inline boost::system::error_condition | |
114 | make_error_condition(errc e) noexcept { | |
115 | return { static_cast<int>(e), buffer_category() }; | |
116 | } | |
117 | ||
118 | struct error : boost::system::system_error { | |
119 | using system_error::system_error; | |
120 | }; | |
121 | ||
122 | struct bad_alloc : public error { | |
123 | bad_alloc() : error(errc::bad_alloc) {} | |
124 | bad_alloc(const char* what_arg) : error(errc::bad_alloc, what_arg) {} | |
125 | bad_alloc(const std::string& what_arg) : error(errc::bad_alloc, what_arg) {} | |
126 | }; | |
127 | struct end_of_buffer : public error { | |
128 | end_of_buffer() : error(errc::end_of_buffer) {} | |
129 | end_of_buffer(const char* what_arg) : error(errc::end_of_buffer, what_arg) {} | |
130 | end_of_buffer(const std::string& what_arg) | |
131 | : error(errc::end_of_buffer, what_arg) {} | |
132 | }; | |
133 | ||
134 | struct malformed_input : public error { | |
135 | malformed_input() : error(errc::malformed_input) {} | |
136 | malformed_input(const char* what_arg) | |
137 | : error(errc::malformed_input, what_arg) {} | |
138 | malformed_input(const std::string& what_arg) | |
139 | : error(errc::malformed_input, what_arg) {} | |
140 | }; | |
141 | struct error_code : public error { | |
142 | error_code(int r) : error(-r, boost::system::system_category()) {} | |
143 | error_code(int r, const char* what_arg) | |
144 | : error(-r, boost::system::system_category(), what_arg) {} | |
145 | error_code(int r, const std::string& what_arg) | |
146 | : error(-r, boost::system::system_category(), what_arg) {} | |
147 | }; | |
148 | } | |
149 | } | |
150 | ||
151 | #endif // COMMON_CEPH_ERROR_CODE |