• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

STEllAR-GROUP / hpx / #882

31 Aug 2023 07:44PM UTC coverage: 41.798% (-44.7%) from 86.546%
#882

push

19442 of 46514 relevant lines covered (41.8%)

126375.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

46.81
/libs/core/errors/src/error_code.cpp
1
//  Copyright (c) 2007-2025 Hartmut Kaiser
2
//  Copyright (c) 2011      Bryce Lelbach
3
//
4
//  SPDX-License-Identifier: BSL-1.0
5
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
6
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7

8
#include <hpx/config.hpp>
9
#include <hpx/errors/error_code.hpp>
10
#include <hpx/errors/exception.hpp>
11

12
#include <exception>
13
#include <string>
14
#include <system_error>
15

16
///////////////////////////////////////////////////////////////////////////////
17
namespace hpx {
18

19
    /// \cond NOINTERNAL
20
    inline constexpr char const* const error_names[] = {
21
        /*  0 */ "success",
22
        /*  1 */ "no_success",
23
        /*  2 */ "not_implemented",
24
        /*  3 */ "out_of_memory",
25
        /*  4 */ "bad_action_code",
26
        /*  5 */ "bad_component_type",
27
        /*  6 */ "network_error",
28
        /*  7 */ "version_too_new",
29
        /*  8 */ "version_too_old",
30
        /*  9 */ "version_unknown",
31
        /* 10 */ "unknown_component_address",
32
        /* 11 */ "duplicate_component_address",
33
        /* 12 */ "invalid_status",
34
        /* 13 */ "bad_parameter",
35
        /* 14 */ "internal_server_error",
36
        /* 15 */ "service_unavailable",
37
        /* 16 */ "bad_request",
38
        /* 17 */ "repeated_request",
39
        /* 18 */ "lock_error",
40
        /* 19 */ "duplicate_console",
41
        /* 20 */ "no_registered_console",
42
        /* 21 */ "startup_timed_out",
43
        /* 22 */ "uninitialized_value",
44
        /* 23 */ "bad_response_type",
45
        /* 24 */ "deadlock",
46
        /* 25 */ "assertion_failure",
47
        /* 26 */ "null_thread_id",
48
        /* 27 */ "invalid_data",
49
        /* 28 */ "yield_aborted",
50
        /* 29 */ "dynamic_link_failure",
51
        /* 30 */ "commandline_option_error",
52
        /* 31 */ "serialization_error",
53
        /* 32 */ "unhandled_exception",
54
        /* 33 */ "kernel_error",
55
        /* 34 */ "broken_task",
56
        /* 35 */ "task_moved",
57
        /* 36 */ "task_already_started",
58
        /* 37 */ "future_already_retrieved",
59
        /* 38 */ "promise_already_satisfied",
60
        /* 39 */ "future_does_not_support_cancellation",
61
        /* 40 */ "future_can_not_be_cancelled",
62
        /* 41 */ "no_state",
63
        /* 42 */ "broken_promise",
64
        /* 43 */ "thread_resource_error",
65
        /* 44 */ "future_cancelled",
66
        /* 45 */ "thread_cancelled",
67
        /* 46 */ "thread_not_interruptable",
68
        /* 47 */ "duplicate_component_id",
69
        /* 48 */ "unknown_error",
70
        /* 49 */ "bad_plugin_type",
71
        /* 50 */ "filesystem_error",
72
        /* 51 */ "bad_function_call",
73
        /* 52 */ "task_canceled_exception",
74
        /* 53 */ "task_block_not_active",
75
        /* 54 */ "out_of_range",
76
        /* 55 */ "length_error",
77
        /* 56 */ "migration_needs_retry",
78

79
        /*    */ ""};
80
    /// \endcond
81

82
    // Return a textual representation of a given error code
83
    char const* get_error_name(error value) noexcept
×
84
    {
85
        if (value >= hpx::error::success && value < hpx::error::last_error)
×
86
        {
87
            return error_names[static_cast<int>(value)];
×
88
        }
89
        if (value & hpx::error::system_error_flag)
×
90
        {
91
            return "system_error";
×
92
        }
93
        return "unknown";
94
    }
95

96
    namespace detail {
97

98
        class hpx_category : public std::error_category
99
        {
100
        public:
101
            [[nodiscard]] char const* name() const noexcept override
×
102
            {
103
                return "HPX";
×
104
            }
105

106
            [[nodiscard]] std::string message(int value) const override
241✔
107
            {
108
                if (value >= hpx::error::success &&
241✔
109
                    value < hpx::error::last_error)
110
                {
111
                    return std::string("HPX(") + error_names[value] +
482✔
112
                        ")";    //-V108
113
                }
114
                if (value & hpx::error::system_error_flag)
×
115
                {
116
                    return "HPX(system_error)";
×
117
                }
118
                return "HPX(unknown_error)";
×
119
            }
120
        };
121

122
        struct lightweight_hpx_category final : hpx_category
123
        {
124
        };
125

126
        // this doesn't add any text to the exception what() message
127
        class hpx_category_rethrow : public std::error_category
128
        {
129
        public:
130
            [[nodiscard]] char const* name() const noexcept override
×
131
            {
132
                return "";
×
133
            }
134

135
            [[nodiscard]] std::string message(int) const noexcept override
×
136
            {
137
                return {};
×
138
            }
139
        };
140

141
        struct lightweight_hpx_category_rethrow final : hpx_category_rethrow
142
        {
143
        };
144
    }    // namespace detail
145

146
    ///////////////////////////////////////////////////////////////////////////
147
    std::error_category const& get_hpx_category() noexcept
25,318✔
148
    {
149
        static detail::hpx_category hpx_category;
25,318✔
150
        return hpx_category;
25,318✔
151
    }
152

153
    std::error_category const& get_hpx_rethrow_category() noexcept
1✔
154
    {
155
        static detail::hpx_category_rethrow hpx_category_rethrow;
1✔
156
        return hpx_category_rethrow;
1✔
157
    }
158

159
    std::error_category const& get_lightweight_hpx_category() noexcept
61,674✔
160
    {
161
        static detail::lightweight_hpx_category lightweight_hpx_category;
61,674✔
162
        return lightweight_hpx_category;
61,674✔
163
    }
164

165
    std::error_category const& get_hpx_category(throwmode mode) noexcept
63,211✔
166
    {
167
        switch (mode)
63,211✔
168
        {
169
        case throwmode::rethrow:
1✔
170
            return get_hpx_rethrow_category();
1✔
171

172
        case throwmode::lightweight:
37,897✔
173
        case throwmode::lightweight_rethrow:
174
            return get_lightweight_hpx_category();
37,897✔
175

176
        case throwmode::plain:
177
        default:
178
            break;
179
        }
180
        return get_hpx_category();
25,313✔
181
    }
182

183
    ///////////////////////////////////////////////////////////////////////////
184
    error_code::error_code(error e, throwmode mode)
236✔
185
      : std::error_code(make_system_error_code(e, mode))
236✔
186
    {
187
        if (e != hpx::error::success && e != hpx::error::no_success &&
236✔
188
            !(mode & throwmode::lightweight))
189
        {
190
            exception_ = detail::get_exception(e, "", mode);
×
191
        }
192
    }
236✔
193

194
    error_code::error_code(
×
195
        error e, char const* func, char const* file, long line, throwmode mode)
×
196
      : std::error_code(make_system_error_code(e, mode))
×
197
    {
198
        if (e != hpx::error::success && e != hpx::error::no_success &&
×
199
            !(mode & throwmode::lightweight))
200
        {
201
            exception_ = detail::get_exception(e, "", mode, func, file, line);
×
202
        }
203
    }
×
204

205
    error_code::error_code(error e, char const* msg, throwmode mode)
×
206
      : std::error_code(make_system_error_code(e, mode))
×
207
    {
208
        if (e != hpx::error::success && e != hpx::error::no_success &&
×
209
            !(mode & throwmode::lightweight))
210
        {
211
            exception_ = detail::get_exception(e, msg, mode);
×
212
        }
213
    }
×
214

215
    error_code::error_code(error e, char const* msg, char const* func,
×
216
        char const* file, long line, throwmode mode)
×
217
      : std::error_code(make_system_error_code(e, mode))
×
218
    {
219
        if (e != hpx::error::success && e != hpx::error::no_success &&
×
220
            !(mode & throwmode::lightweight))
221
        {
222
            exception_ = detail::get_exception(e, msg, mode, func, file, line);
×
223
        }
224
    }
×
225

226
    error_code::error_code(error e, std::string const& msg, throwmode mode)
×
227
      : std::error_code(make_system_error_code(e, mode))
×
228
    {
229
        if (e != hpx::error::success && e != hpx::error::no_success &&
×
230
            !(mode & throwmode::lightweight))
231
        {
232
            exception_ = detail::get_exception(e, msg, mode);
×
233
        }
234
    }
×
235

236
    error_code::error_code(error e, std::string const& msg, char const* func,
1,504✔
237
        char const* file, long line, throwmode mode)
1,504✔
238
      : std::error_code(make_system_error_code(e, mode))
1,504✔
239
    {
240
        if (e != hpx::error::success && e != hpx::error::no_success &&
1,504✔
241
            !(mode & throwmode::lightweight))
242
        {
243
            exception_ = detail::get_exception(e, msg, mode, func, file, line);
×
244
        }
245
    }
1,504✔
246

247
    error_code::error_code(int err, hpx::exception const& e)
5✔
248
    {
249
        this->std::error_code::assign(err, get_hpx_category());
5✔
250
        exception_ = std::make_exception_ptr(e);
10✔
251
    }
5✔
252

253
    error_code::error_code(std::exception_ptr const& e)
1✔
254
      : std::error_code(
255
            make_system_error_code(get_error(e), throwmode::rethrow))
1✔
256
      , exception_(e)
1✔
257
    {
258
    }
1✔
259

260
    ///////////////////////////////////////////////////////////////////////////
261
    std::string error_code::get_message() const
×
262
    {
263
        if (exception_)
×
264
        {
265
            try
266
            {
267
                std::rethrow_exception(exception_);
×
268
            }
269
            catch (std::exception const& be)
×
270
            {
271
                return be.what();
×
272
            }
×
273
        }
274
        return get_error_what(*this);    // provide at least minimal error text
×
275
    }
276

277
    ///////////////////////////////////////////////////////////////////////////
278
    error_code::error_code(error_code const& rhs)
×
279
      : std::error_code(rhs.value() == hpx::error::success ?
×
280
                make_success_code(
281
                    (category() == get_lightweight_hpx_category()) ?
×
282
                        hpx::throwmode::lightweight :
283
                        hpx::throwmode::plain) :
284
                rhs)
285
      , exception_(rhs.exception_)
×
286
    {
287
    }
×
288

289
    ///////////////////////////////////////////////////////////////////////////
290
    error_code& error_code::operator=(error_code const& rhs)
23,773✔
291
    {
292
        if (this != &rhs)
23,773✔
293
        {
294
            if (rhs.value() == hpx::error::success)
23,773✔
295
            {
296
                // if the rhs is a success code, we maintain our throw mode
297
                this->std::error_code::operator=(make_success_code(
44,536✔
298
                    (category() == get_lightweight_hpx_category()) ?
22,268✔
299
                        hpx::throwmode::lightweight :
300
                        hpx::throwmode::plain));
301
            }
302
            else
303
            {
304
                this->std::error_code::operator=(rhs);
1,505✔
305
            }
306
            exception_ = rhs.exception_;
23,773✔
307
        }
308
        return *this;
23,773✔
309
    }
310
    /// \endcond
311
}    // namespace hpx
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc