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

STEllAR-GROUP / hpx / #853

19 Dec 2022 01:01AM UTC coverage: 86.287% (+0.4%) from 85.912%
#853

push

StellarBot
Merge #6109

6109: Modernize serialization module r=hkaiser a=hkaiser

- flyby separate serialization of Boost types

working towards https://github.com/STEllAR-GROUP/hpx/issues/5497

Co-authored-by: Hartmut Kaiser <hartmut.kaiser@gmail.com>

53 of 53 new or added lines in 6 files covered. (100.0%)

173939 of 201582 relevant lines covered (86.29%)

1931657.12 hits per line

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

64.63
/libs/core/errors/src/exception.cpp
1
//  Copyright (c) 2007-2022 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/assert.hpp>
10
#include <hpx/errors/error.hpp>
11
#include <hpx/errors/error_code.hpp>
12
#include <hpx/errors/exception.hpp>
13
#include <hpx/errors/exception_info.hpp>
14
#include <hpx/errors/exception_list.hpp>
15
#include <hpx/modules/format.hpp>
16
#include <hpx/modules/logging.hpp>
17

18
#if defined(HPX_WINDOWS)
19
#include <process.h>
20
#elif defined(HPX_HAVE_UNISTD_H)
21
#include <unistd.h>
22
#endif
23

24
#include <algorithm>
25
#include <atomic>
26
#include <cstddef>
27
#include <cstdint>
28
#include <exception>
29
#include <memory>
30
#include <stdexcept>
31
#include <string>
32
#include <system_error>
33
#include <utility>
34
#include <vector>
35

36
namespace hpx {
37
    ///////////////////////////////////////////////////////////////////////////
38
    /// Construct a hpx::exception from a \a hpx::error.
39
    ///
40
    /// \param e    The parameter \p e holds the hpx::error code the new
41
    ///             exception should encapsulate.
42
    exception::exception(hpx::error e)
91,561✔
43
      : std::system_error(make_error_code(e, throwmode::plain))
91,561✔
44
    {
91,561✔
45
        HPX_ASSERT((e >= hpx::error::success && e < hpx::error::last_error) ||
91,567✔
46
            (e & hpx::error::system_error_flag));
47
        if (e != hpx::error::success)
91,565✔
48
        {
49
            LERR_(error).format("created exception: {}", this->what());
736✔
50
        }
736✔
51
    }
91,565✔
52

53
    /// Construct a hpx::exception from a boost#system_error.
54
    exception::exception(std::system_error const& e)
×
55
      : std::system_error(e)
×
56
    {
×
57
        LERR_(error).format("created exception: {}", this->what());
×
58
    }
×
59

60
    /// Construct a hpx::exception from a boost#system#error_code (this is
61
    /// new for Boost V1.69).
62
    exception::exception(std::error_code const& e)
×
63
      : std::system_error(e)
×
64
    {
×
65
        LERR_(error).format("created exception: {}", this->what());
×
66
    }
×
67

68
    /// Construct a hpx::exception from a \a hpx::error and an error message.
69
    ///
70
    /// \param e      The parameter \p e holds the hpx::error code the new
71
    ///               exception should encapsulate.
72
    /// \param msg    The parameter \p msg holds the error message the new
73
    ///               exception should encapsulate.
74
    /// \param mode   The parameter \p mode specifies whether the returned
75
    ///               hpx::error_code belongs to the error category
76
    ///               \a hpx_category (if mode is \a throwmode::plain, this is the
77
    ///               default) or to the category \a hpx_category_rethrow
78
    ///               (if mode is \a throwmode::rethrow).
79
    exception::exception(hpx::error e, char const* msg, throwmode mode)
×
80
      : std::system_error(make_system_error_code(e, mode), msg)
×
81
    {
×
82
        HPX_ASSERT((e >= hpx::error::success && e < hpx::error::last_error) ||
×
83
            (e & hpx::error::system_error_flag));
84
        if (e != hpx::error::success)
×
85
        {
86
            LERR_(error).format("created exception: {}", this->what());
×
87
        }
×
88
    }
×
89

90
    /// Construct a hpx::exception from a \a hpx::error and an error message.
91
    ///
92
    /// \param e      The parameter \p e holds the hpx::error code the new
93
    ///               exception should encapsulate.
94
    /// \param msg    The parameter \p msg holds the error message the new
95
    ///               exception should encapsulate.
96
    /// \param mode   The parameter \p mode specifies whether the returned
97
    ///               hpx::error_code belongs to the error category
98
    ///               \a hpx_category (if mode is \a throwmode::plain, this is the
99
    ///               default) or to the category \a hpx_category_rethrow
100
    ///               (if mode is \a throwmode::rethrow).
101
    exception::exception(hpx::error e, std::string const& msg, throwmode mode)
4,543✔
102
      : std::system_error(make_system_error_code(e, mode), msg)
4,543✔
103
    {
9,086✔
104
        HPX_ASSERT((e >= hpx::error::success && e < hpx::error::last_error) ||
4,543✔
105
            (e & hpx::error::system_error_flag));
106
        if (e != hpx::error::success)
4,543✔
107
        {
108
            LERR_(error).format("created exception: {}", this->what());
4,543✔
109
        }
4,543✔
110
    }
4,543✔
111

112
    /// Destruct a hpx::exception
113
    ///
114
    /// \throws nothing
115
    exception::~exception() = default;
103,895✔
116

117
    /// The function \a get_error() returns the hpx::error code stored
118
    /// in the referenced instance of a hpx::exception. It returns
119
    /// the hpx::error code this exception instance was constructed
120
    /// from.
121
    ///
122
    /// \throws nothing
123
    error exception::get_error() const noexcept
2,554✔
124
    {
125
        return static_cast<error>(this->std::system_error::code().value());
2,554✔
126
    }
127

128
    /// The function \a get_error_code() returns a hpx::error_code which
129
    /// represents the same error condition as this hpx::exception instance.
130
    ///
131
    /// \param mode   The parameter \p mode specifies whether the returned
132
    ///               hpx::error_code belongs to the error category
133
    ///               \a hpx_category (if mode is \a throwmode::plain, this is the
134
    ///               default) or to the category \a hpx_category_rethrow
135
    ///               (if mode is \a throwmode::rethrow).
136
    error_code exception::get_error_code(throwmode mode) const noexcept
2,589✔
137
    {
138
        (void) mode;
139
        return error_code(this->std::system_error::code().value(), *this);
2,589✔
140
    }
141

142
    static custom_exception_info_handler_type custom_exception_info_handler;
1,249✔
143

144
    void set_custom_exception_info_handler(custom_exception_info_handler_type f)
1,219✔
145
    {
146
        custom_exception_info_handler = HPX_MOVE(f);
1,219✔
147
    }
1,219✔
148

149
    static pre_exception_handler_type pre_exception_handler;
1,249✔
150

151
    void set_pre_exception_handler(pre_exception_handler_type f)
1,219✔
152
    {
153
        pre_exception_handler = HPX_MOVE(f);
1,219✔
154
    }
1,219✔
155
}    // namespace hpx
156

157
namespace hpx::detail {
158

159
    template <typename Exception>
160
    HPX_CORE_EXPORT std::exception_ptr construct_lightweight_exception(
63✔
161
        Exception const& e, std::string const& func, std::string const& file,
162
        long line)
163
    {
164
        // create a std::exception_ptr object encapsulating the Exception to
165
        // be thrown and annotate it with all the local information we have
166
        try
167
        {
168
            throw_with_info(e,
126✔
169
                std::move(
63✔
170
                    hpx::exception_info().set(hpx::detail::throw_function(func),
126✔
171
                        hpx::detail::throw_file(file),
63✔
172
                        hpx::detail::throw_line(line))));
63✔
173
        }
63✔
174
        catch (...)
175
        {
176
            return std::current_exception();
63✔
177
        }
63✔
178

179
        // need this return to silence a warning with icc
180
        HPX_ASSERT(false);    // -V779
×
181
        return std::exception_ptr();
×
182
    }
126✔
183

184
    template <typename Exception>
185
    HPX_CORE_EXPORT std::exception_ptr construct_lightweight_exception(
4✔
186
        Exception const& e)
187
    {
188
        // create a std::exception_ptr object encapsulating the Exception to
189
        // be thrown and annotate it with all the local information we have
190
        try
191
        {
192
            hpx::throw_with_info(e);
4✔
193
        }
4✔
194
        catch (...)
195
        {
196
            return std::current_exception();
4✔
197
        }
4✔
198

199
        // need this return to silence a warning with icc
200
        HPX_ASSERT(false);    // -V779
×
201
        return std::exception_ptr();
×
202
    }
8✔
203

204
    template HPX_CORE_EXPORT std::exception_ptr construct_lightweight_exception(
205
        hpx::thread_interrupted const&);
206
    template HPX_CORE_EXPORT std::exception_ptr construct_lightweight_exception(
207
        hpx::exception_list const&);
208

209
    template <typename Exception>
210
    HPX_CORE_EXPORT std::exception_ptr construct_custom_exception(
2,589✔
211
        Exception const& e, std::string const& func, std::string const& file,
212
        long line, std::string const& auxinfo)
213
    {
214
        if (!custom_exception_info_handler)
2,589✔
215
        {
216
            return construct_lightweight_exception(e, func, file, line);
63✔
217
        }
218

219
        // create a std::exception_ptr object encapsulating the Exception to
220
        // be thrown and annotate it with information provided by the hook
221
        try
222
        {
223
            throw_with_info(
2,526✔
224
                e, custom_exception_info_handler(func, file, line, auxinfo));
2,526✔
225
        }
2,526✔
226
        catch (...)
227
        {
228
            return std::current_exception();
2,526✔
229
        }
2,526✔
230

231
        // need this return to silence a warning with icc
232
        HPX_ASSERT(false);    // -V779
×
233
        return std::exception_ptr();
×
234
    }
5,115✔
235

236
    ///////////////////////////////////////////////////////////////////////////
237
    template <typename Exception>
238
    inline constexpr bool is_of_lightweight_hpx_category(
×
239
        Exception const&) noexcept
240
    {
241
        return false;
×
242
    }
243

244
    inline bool is_of_lightweight_hpx_category(hpx::exception const& e) noexcept
2,589✔
245
    {
246
        return e.get_error_code().category() == get_lightweight_hpx_category();
2,589✔
247
    }
248

249
    ///////////////////////////////////////////////////////////////////////////
250
    std::exception_ptr access_exception(error_code const& e)
4,014✔
251
    {
252
        return e.exception_;
4,014✔
253
    }
254

255
    template <typename Exception>
256
    HPX_CORE_EXPORT std::exception_ptr get_exception(Exception const& e,
2,589✔
257
        std::string const& func, std::string const& file, long line,
258
        std::string const& auxinfo)
259
    {
260
        if (is_of_lightweight_hpx_category(e))
2,588✔
261
        {
262
            return construct_lightweight_exception(e, func, file, line);
×
263
        }
264

265
        return construct_custom_exception(e, func, file, line, auxinfo);
2,589✔
266
    }
2,589✔
267

268
    template <typename Exception>
269
    HPX_CORE_EXPORT void throw_exception(Exception const& e,
1,761✔
270
        std::string const& func, std::string const& file, long line)
271
    {
272
        if (pre_exception_handler)
1,761✔
273
        {
274
            pre_exception_handler();
1,718✔
275
        }
1,718✔
276

277
        std::rethrow_exception(get_exception(e, func, file, line));
1,761✔
278
    }
1,761✔
279

280
    ///////////////////////////////////////////////////////////////////////////
281
    template HPX_CORE_EXPORT std::exception_ptr get_exception(
282
        hpx::exception const&, std::string const&, std::string const&, long,
283
        std::string const&);
284

285
    template HPX_CORE_EXPORT std::exception_ptr get_exception(
286
        std::system_error const&, std::string const&, std::string const&, long,
287
        std::string const&);
288

289
    template HPX_CORE_EXPORT std::exception_ptr get_exception(
290
        std::exception const&, std::string const&, std::string const&, long,
291
        std::string const&);
292
    template HPX_CORE_EXPORT std::exception_ptr get_exception(
293
        hpx::detail::std_exception const&, std::string const&,
294
        std::string const&, long, std::string const&);
295
    template HPX_CORE_EXPORT std::exception_ptr get_exception(
296
        std::bad_exception const&, std::string const&, std::string const&, long,
297
        std::string const&);
298
    template HPX_CORE_EXPORT std::exception_ptr get_exception(
299
        hpx::detail::bad_exception const&, std::string const&,
300
        std::string const&, long, std::string const&);
301
    template HPX_CORE_EXPORT std::exception_ptr get_exception(
302
        std::bad_typeid const&, std::string const&, std::string const&, long,
303
        std::string const&);
304
    template HPX_CORE_EXPORT std::exception_ptr get_exception(
305
        hpx::detail::bad_typeid const&, std::string const&, std::string const&,
306
        long, std::string const&);
307
    template HPX_CORE_EXPORT std::exception_ptr get_exception(
308
        std::bad_cast const&, std::string const&, std::string const&, long,
309
        std::string const&);
310
    template HPX_CORE_EXPORT std::exception_ptr get_exception(
311
        hpx::detail::bad_cast const&, std::string const&, std::string const&,
312
        long, std::string const&);
313
    template HPX_CORE_EXPORT std::exception_ptr get_exception(
314
        std::bad_alloc const&, std::string const&, std::string const&, long,
315
        std::string const&);
316
    template HPX_CORE_EXPORT std::exception_ptr get_exception(
317
        hpx::detail::bad_alloc const&, std::string const&, std::string const&,
318
        long, std::string const&);
319
    template HPX_CORE_EXPORT std::exception_ptr get_exception(
320
        std::logic_error const&, std::string const&, std::string const&, long,
321
        std::string const&);
322
    template HPX_CORE_EXPORT std::exception_ptr get_exception(
323
        std::runtime_error const&, std::string const&, std::string const&, long,
324
        std::string const&);
325
    template HPX_CORE_EXPORT std::exception_ptr get_exception(
326
        std::out_of_range const&, std::string const&, std::string const&, long,
327
        std::string const&);
328
    template HPX_CORE_EXPORT std::exception_ptr get_exception(
329
        std::invalid_argument const&, std::string const&, std::string const&,
330
        long, std::string const&);
331

332
    ///////////////////////////////////////////////////////////////////////////
333
    template HPX_CORE_EXPORT void throw_exception(
334
        hpx::exception const&, std::string const&, std::string const&, long);
335

336
    template HPX_CORE_EXPORT void throw_exception(
337
        std::system_error const&, std::string const&, std::string const&, long);
338

339
    template HPX_CORE_EXPORT void throw_exception(
340
        std::exception const&, std::string const&, std::string const&, long);
341
    template HPX_CORE_EXPORT void throw_exception(
342
        hpx::detail::std_exception const&, std::string const&,
343
        std::string const&, long);
344
    template HPX_CORE_EXPORT void throw_exception(std::bad_exception const&,
345
        std::string const&, std::string const&, long);
346
    template HPX_CORE_EXPORT void throw_exception(
347
        hpx::detail::bad_exception const&, std::string const&,
348
        std::string const&, long);
349
    template HPX_CORE_EXPORT void throw_exception(
350
        std::bad_typeid const&, std::string const&, std::string const&, long);
351
    template HPX_CORE_EXPORT void throw_exception(
352
        hpx::detail::bad_typeid const&, std::string const&, std::string const&,
353
        long);
354
    template HPX_CORE_EXPORT void throw_exception(
355
        std::bad_cast const&, std::string const&, std::string const&, long);
356
    template HPX_CORE_EXPORT void throw_exception(hpx::detail::bad_cast const&,
357
        std::string const&, std::string const&, long);
358
    template HPX_CORE_EXPORT void throw_exception(
359
        std::bad_alloc const&, std::string const&, std::string const&, long);
360
    template HPX_CORE_EXPORT void throw_exception(hpx::detail::bad_alloc const&,
361
        std::string const&, std::string const&, long);
362
    template HPX_CORE_EXPORT void throw_exception(
363
        std::logic_error const&, std::string const&, std::string const&, long);
364
    template HPX_CORE_EXPORT void throw_exception(std::runtime_error const&,
365
        std::string const&, std::string const&, long);
366
    template HPX_CORE_EXPORT void throw_exception(
367
        std::out_of_range const&, std::string const&, std::string const&, long);
368
    template HPX_CORE_EXPORT void throw_exception(std::invalid_argument const&,
369
        std::string const&, std::string const&, long);
370
}    // namespace hpx::detail
371

372
///////////////////////////////////////////////////////////////////////////////
373
namespace hpx {
374

375
    ///////////////////////////////////////////////////////////////////////////
376
    /// Return the error message.
377
    std::string get_error_what(hpx::exception_info const& xi)
2✔
378
    {
379
        // Try a cast to std::exception - this should handle boost.system
380
        // error codes in addition to the standard library exceptions.
381
        std::exception const* se = dynamic_cast<std::exception const*>(&xi);
2✔
382
        return se ? se->what() : std::string("<unknown>");
2✔
383
    }
×
384

385
    std::string get_error_what(std::exception_ptr const& e)
1,959✔
386
    {
387
        try
388
        {
389
            std::rethrow_exception(e);
1,959✔
390
        }
1,959✔
391
        catch (hpx::thread_interrupted const&)
392
        {
393
            return "thread_interrupted";
×
394
        }
×
395
        catch (std::exception const& e)
396
        {
397
            return get_error_what(e);
1,947✔
398
        }
1,947✔
399
        catch (...)
400
        {
401
            return "<unknown>";
12✔
402
        }
1,959✔
403
    }
3,918✔
404

405
    ///////////////////////////////////////////////////////////////////////////
406
    error get_error(hpx::exception const& e)
×
407
    {
408
        return static_cast<hpx::error>(e.get_error());
×
409
    }
410

411
    error get_error(hpx::error_code const& e)
×
412
    {
413
        return static_cast<hpx::error>(e.value());
×
414
    }
415

416
    error get_error(std::exception_ptr const& e)
8,186✔
417
    {
418
        try
419
        {
420
            std::rethrow_exception(e);
8,186✔
421
        }
8,186✔
422
        catch (hpx::thread_interrupted const&)
423
        {
424
            return hpx::error::thread_cancelled;
1✔
425
        }
1✔
426
        catch (hpx::exception const& he)
427
        {
428
            return he.get_error();
2,483✔
429
        }
2,484✔
430
        catch (std::system_error const& e)
431
        {
432
            int code = e.code().value();
×
433
            if (code < hpx::error::success || code >= hpx::error::last_error)
×
434
            {
435
                code |= hpx::error::system_error_flag;
×
436
            }
×
437
            return static_cast<hpx::error>(code);
×
438
        }
2,483✔
439
        catch (...)
440
        {
441
            return hpx::error::unknown_error;
5,702✔
442
        }
5,702✔
443
    }
16,372✔
444

445
    /// Return the function name from which the exception was thrown.
446
    std::string get_error_function_name(hpx::exception_info const& xi)
7✔
447
    {
448
        std::string const* function = xi.get<hpx::detail::throw_function>();
7✔
449
        if (function)
7✔
450
            return *function;
7✔
451

452
        return std::string();
×
453
    }
7✔
454

455
    /// Return the (source code) file name of the function from which the
456
    /// exception was thrown.
457
    std::string get_error_file_name(hpx::exception_info const& xi)
×
458
    {
459
        std::string const* file = xi.get<hpx::detail::throw_file>();
×
460
        if (file)
×
461
            return *file;
×
462

463
        return "<unknown>";
×
464
    }
×
465

466
    /// Return the line number in the (source code) file of the function from
467
    /// which the exception was thrown.
468
    long get_error_line_number(hpx::exception_info const& xi)
×
469
    {
470
        long const* line = xi.get<hpx::detail::throw_line>();
×
471
        if (line)
×
472
            return *line;
×
473
        return -1;
×
474
    }
×
475

476
}    // 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