• 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

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

7
#include <hpx/assert.hpp>
8
#include <hpx/modules/errors.hpp>
9
#include <hpx/modules/serialization.hpp>
10
#include <hpx/runtime_local/custom_exception_info.hpp>
11
#include <hpx/runtime_local/detail/serialize_exception.hpp>
12

13
#if ASIO_HAS_BOOST_THROW_EXCEPTION != 0
14
#include <boost/exception/diagnostic_information.hpp>
15
#include <boost/exception/exception.hpp>
16
#endif
17

18
#include <cstddef>
19
#include <cstdint>
20
#include <exception>
21
#include <stdexcept>
22
#include <string>
23
#include <system_error>
24
#include <typeinfo>
25

26
#include <hpx/config/warnings_prefix.hpp>
27

28
///////////////////////////////////////////////////////////////////////////////
29
namespace hpx::runtime_local::detail {
30

31
    ///////////////////////////////////////////////////////////////////////////
×
32
    void save_custom_exception(hpx::serialization::output_archive& ar,
33
        std::exception_ptr const& ep, unsigned int /* version */)
34
    {
×
35
        hpx::util::exception_type type =
36
            hpx::util::exception_type::unknown_exception;
37
        std::string what;
38
        hpx::error err_value = hpx::error::success;
39
        std::string err_message;
40

×
41
        std::uint32_t throw_locality_ = 0;
42
        std::string throw_hostname_;
×
43
        std::int64_t throw_pid_ = -1;
×
44
        std::size_t throw_shepherd_ = 0;
×
45
        std::size_t throw_thread_id_ = 0;
46
        std::string throw_thread_name_;
47
        std::string throw_function_;
48
        std::string throw_file_;
49
        std::string throw_back_trace_;
×
50
        long throw_line_ = 0;
51
        std::string throw_env_;
52
        std::string throw_config_;
53
        std::string throw_state_;
54
        std::string throw_auxinfo_;
55

56
        // retrieve information related to exception_info
57
        try
58
        {
×
59
            std::rethrow_exception(ep);
60
        }
×
61
        catch (exception_info const& xi)
62
        {
63
            if (std::string const* function =
×
64
                    xi.get<hpx::detail::throw_function>())
65
            {
66
                throw_function_ = *function;
67
            }
×
68

69
            if (std::string const* file = xi.get<hpx::detail::throw_file>())
70
            {
71
                throw_file_ = *file;
×
72
            }
×
73

74
            if (long const* line = xi.get<hpx::detail::throw_line>())
75
            {
76
                throw_line_ = *line;
×
77
            }
×
78

79
            if (std::uint32_t const* locality =
80
                    xi.get<hpx::detail::throw_locality>())
81
            {
×
82
                throw_locality_ = *locality;
83
            }
84

85
            if (std::string const* hostname_ =
×
86
                    xi.get<hpx::detail::throw_hostname>())
×
87
            {
88
                throw_hostname_ = *hostname_;
89
            }
×
90

×
91
            if (std::int64_t const* pid_ = xi.get<hpx::detail::throw_pid>())
92
            {
93
                throw_pid_ = *pid_;
94
            }
×
95

×
96
            if (std::size_t const* shepherd =
97
                    xi.get<hpx::detail::throw_shepherd>())
98
            {
99
                throw_shepherd_ = *shepherd;
×
100
            }
101

102
            if (std::size_t const* thread_id =
103
                    xi.get<hpx::detail::throw_thread_id>())
104
            {
×
105
                throw_thread_id_ = *thread_id;
106
            }
107

108
            if (std::string const* thread_name =
×
109
                    xi.get<hpx::detail::throw_thread_name>())
110
            {
111
                throw_thread_name_ = *thread_name;
112
            }
×
113

114
            if (std::string const* back_trace =
115
                    xi.get<hpx::detail::throw_stacktrace>())
116
            {
×
117
                throw_back_trace_ = *back_trace;
118
            }
119

120
            if (std::string const* env_ = xi.get<hpx::detail::throw_env>())
×
121
            {
122
                throw_env_ = *env_;
×
123
            }
×
124

125
            if (std::string const* config_ =
126
                    xi.get<hpx::detail::throw_config>())
×
127
            {
128
                throw_config_ = *config_;
129
            }
130

131
            if (std::string const* state_ = xi.get<hpx::detail::throw_state>())
×
132
            {
133
                throw_state_ = *state_;
×
134
            }
135

×
136
            if (std::string const* auxinfo_ =
137
                    xi.get<hpx::detail::throw_auxinfo>())
138
            {
×
139
                throw_auxinfo_ = *auxinfo_;
×
140
            }
141
        }
×
142
        // NOLINTNEXTLINE(bugprone-empty-catch)
×
143
        catch (...)
×
144
        {    //-V565
×
145
            // do nothing
×
146
        }
147

×
148
        // figure out concrete underlying exception type
×
149
        try
×
150
        {
×
151
            std::rethrow_exception(ep);
×
152
        }
×
153
        catch (hpx::thread_interrupted const&)
154
        {
×
155
            type = hpx::util::exception_type::hpx_thread_interrupted_exception;
×
156
            what = "hpx::thread_interrupted";
×
157
            err_value = hpx::error::thread_cancelled;
×
158
        }
159
        catch (hpx::exception const& e)
×
160
        {
×
161
            type = hpx::util::exception_type::hpx_exception;
×
162
            what = e.what();
×
163
            err_value = e.get_error();
164
        }
×
165
        catch (std::system_error const& e)
×
166
        {
×
167
            type = hpx::util::exception_type::std_system_error;
×
168
            what = e.what();
169
            err_value = static_cast<hpx::error>(e.code().value());
×
170
            err_message = e.code().message();
×
171
        }
×
172
        catch (std::runtime_error const& e)
×
173
        {
174
            type = hpx::util::exception_type::std_runtime_error;
×
175
            what = e.what();
×
176
        }
×
177
        catch (std::invalid_argument const& e)
×
178
        {
179
            type = hpx::util::exception_type::std_invalid_argument;
×
180
            what = e.what();
×
181
        }
×
182
        catch (std::out_of_range const& e)
×
183
        {
184
            type = hpx::util::exception_type::std_out_of_range;
×
185
            what = e.what();
×
186
        }
×
187
        catch (std::logic_error const& e)
×
188
        {
189
            type = hpx::util::exception_type::std_logic_error;
×
190
            what = e.what();
×
191
        }
×
192
        catch (std::bad_alloc const& e)
×
193
        {
194
            type = hpx::util::exception_type::std_bad_alloc;
×
195
            what = e.what();
×
196
        }
×
197
        catch (std::bad_cast const& e)
198
        {
199
            type = hpx::util::exception_type::std_bad_cast;
200
            what = e.what();
201
        }
202
        catch (std::bad_typeid const& e)
203
        {
204
            type = hpx::util::exception_type::std_bad_typeid;
×
205
            what = e.what();
206
        }
×
207
        catch (std::bad_exception const& e)
208
        {
×
209
            type = hpx::util::exception_type::std_bad_exception;
210
            what = e.what();
211
        }
212
        catch (std::exception const& e)
213
        {
214
            type = hpx::util::exception_type::std_exception;
215
            what = e.what();
216
        }
217
#if ASIO_HAS_BOOST_THROW_EXCEPTION != 0
×
218
        catch (boost::exception const& e)
219
        {
220
            type = hpx::util::boost_exception;
×
221
            what = boost::diagnostic_information(e);
222
        }
223
#endif
×
224
        catch (...)
225
        {
226
            type = hpx::util::exception_type::unknown_exception;
×
227
            what = "unknown exception";
228
        }
229

×
230
        // clang-format off
231
        ar & type & what & throw_function_ & throw_file_ & throw_line_ &
232
            throw_locality_ & throw_hostname_ & throw_pid_ & throw_shepherd_ &
233
            throw_thread_id_ & throw_thread_name_ & throw_back_trace_ &
×
234
            throw_env_ & throw_config_ & throw_state_ & throw_auxinfo_;
235
        // clang-format on
236

237
        if (hpx::util::exception_type::hpx_exception == type)
238
        {
239
            // clang-format off
240
            ar << static_cast<int>(err_value);
241
            // clang-format on
242
        }
243
        else if (hpx::util::exception_type::std_system_error == type)
244
        {
245
            // clang-format off
246
            ar << static_cast<int>(err_value) << err_message;
247
            // clang-format on
248
        }
249
    }
250

251
    ///////////////////////////////////////////////////////////////////////////
252
    void load_custom_exception(hpx::serialization::input_archive& ar,
253
        std::exception_ptr& e, unsigned int /*version*/)
254
    {
255
        hpx::util::exception_type type =
256
            hpx::util::exception_type::unknown_exception;
257
        std::string what;
258
        hpx::error err_value = hpx::error::success;
259
        std::string err_message;
260

261
        std::uint32_t throw_locality_ = 0;
262
        std::string throw_hostname_;
263
        std::int64_t throw_pid_ = -1;
264
        std::size_t throw_shepherd_ = 0;
×
265
        std::size_t throw_thread_id_ = 0;
266
        std::string throw_thread_name_;
267
        std::string throw_function_;
268
        std::string throw_file_;
269
        std::string throw_back_trace_;
×
270
        int throw_line_ = 0;
271
        std::string throw_env_;
272
        std::string throw_config_;
×
273
        std::string throw_state_;
274
        std::string throw_auxinfo_;
275

276
        // clang-format off
277
        ar & type & what & throw_function_ & throw_file_ & throw_line_ &
×
278
            throw_locality_ & throw_hostname_ & throw_pid_ & throw_shepherd_ &
279
            throw_thread_id_ & throw_thread_name_ & throw_back_trace_ &
280
            throw_env_ & throw_config_ & throw_state_ & throw_auxinfo_;
281
        // clang-format on
×
282

283
        if (hpx::util::exception_type::hpx_exception == type)
×
284
        {
285
            // clang-format off
286
            int error_code = 0;
287
            ar >> error_code;
288
            err_value = static_cast<hpx::error>(error_code);
×
289
            // clang-format on
×
290
        }
×
291
        else if (hpx::util::exception_type::std_system_error == type)
292
        {
293
            // clang-format off
294
            int error_code = 0;
295
            ar >> error_code >> err_message;
×
296
            err_value = static_cast<hpx::error>(error_code);
297
            // clang-format on
298
        }
×
299

×
300
        switch (type)
×
301
        {
302
        default:
303
        case hpx::util::exception_type::std_exception:
304
            [[fallthrough]];
305

×
306
        case hpx::util::exception_type::unknown_exception:
307
            e = hpx::detail::construct_exception(
×
308
                hpx::detail::std_exception(what),
×
309
                hpx::detail::construct_exception_info(throw_function_,
×
310
                    throw_file_, throw_line_, throw_back_trace_,
311
                    throw_locality_, throw_hostname_, throw_pid_,
312
                    throw_shepherd_, throw_thread_id_, throw_thread_name_,
313
                    throw_env_, throw_config_, throw_state_, throw_auxinfo_));
314
            break;
×
315

316
        // standard exceptions
×
317
        case hpx::util::exception_type::std_runtime_error:
×
318
            e = hpx::detail::construct_exception(std::runtime_error(what),
×
319
                hpx::detail::construct_exception_info(throw_function_,
320
                    throw_file_, throw_line_, throw_back_trace_,
321
                    throw_locality_, throw_hostname_, throw_pid_,
322
                    throw_shepherd_, throw_thread_id_, throw_thread_name_,
323
                    throw_env_, throw_config_, throw_state_, throw_auxinfo_));
×
324
            break;
325

×
326
        case hpx::util::exception_type::std_invalid_argument:
×
327
            e = hpx::detail::construct_exception(std::invalid_argument(what),
×
328
                hpx::detail::construct_exception_info(throw_function_,
329
                    throw_file_, throw_line_, throw_back_trace_,
330
                    throw_locality_, throw_hostname_, throw_pid_,
331
                    throw_shepherd_, throw_thread_id_, throw_thread_name_,
332
                    throw_env_, throw_config_, throw_state_, throw_auxinfo_));
×
333
            break;
334

×
335
        case hpx::util::exception_type::std_out_of_range:
×
336
            e = hpx::detail::construct_exception(std::out_of_range(what),
×
337
                hpx::detail::construct_exception_info(throw_function_,
338
                    throw_file_, throw_line_, throw_back_trace_,
339
                    throw_locality_, throw_hostname_, throw_pid_,
340
                    throw_shepherd_, throw_thread_id_, throw_thread_name_,
341
                    throw_env_, throw_config_, throw_state_, throw_auxinfo_));
×
342
            break;
343

×
344
        case hpx::util::exception_type::std_logic_error:
×
345
            e = hpx::detail::construct_exception(std::logic_error(what),
×
346
                hpx::detail::construct_exception_info(throw_function_,
347
                    throw_file_, throw_line_, throw_back_trace_,
348
                    throw_locality_, throw_hostname_, throw_pid_,
349
                    throw_shepherd_, throw_thread_id_, throw_thread_name_,
350
                    throw_env_, throw_config_, throw_state_, throw_auxinfo_));
×
351
            break;
352

×
353
        case hpx::util::exception_type::std_bad_alloc:
×
354
            e = hpx::detail::construct_exception(hpx::detail::bad_alloc(what),
×
355
                hpx::detail::construct_exception_info(throw_function_,
356
                    throw_file_, throw_line_, throw_back_trace_,
357
                    throw_locality_, throw_hostname_, throw_pid_,
358
                    throw_shepherd_, throw_thread_id_, throw_thread_name_,
359
                    throw_env_, throw_config_, throw_state_, throw_auxinfo_));
×
360
            break;
×
361

×
362
        case hpx::util::exception_type::std_bad_cast:
×
363
            e = hpx::detail::construct_exception(hpx::detail::bad_cast(what),
×
364
                hpx::detail::construct_exception_info(throw_function_,
365
                    throw_file_, throw_line_, throw_back_trace_,
366
                    throw_locality_, throw_hostname_, throw_pid_,
367
                    throw_shepherd_, throw_thread_id_, throw_thread_name_,
368
                    throw_env_, throw_config_, throw_state_, throw_auxinfo_));
×
369
            break;
370

371
        case hpx::util::exception_type::std_bad_typeid:
372
            e = hpx::detail::construct_exception(hpx::detail::bad_typeid(what),
373
                hpx::detail::construct_exception_info(throw_function_,
374
                    throw_file_, throw_line_, throw_back_trace_,
375
                    throw_locality_, throw_hostname_, throw_pid_,
376
                    throw_shepherd_, throw_thread_id_, throw_thread_name_,
377
                    throw_env_, throw_config_, throw_state_, throw_auxinfo_));
378
            break;
×
379
        case hpx::util::exception_type::std_bad_exception:
380
            e = hpx::detail::construct_exception(
381
                hpx::detail::bad_exception(what),
382
                hpx::detail::construct_exception_info(throw_function_,
383
                    throw_file_, throw_line_, throw_back_trace_,
×
384
                    throw_locality_, throw_hostname_, throw_pid_,
×
385
                    throw_shepherd_, throw_thread_id_, throw_thread_name_,
386
                    throw_env_, throw_config_, throw_state_, throw_auxinfo_));
×
387
            break;
388

389
#if ASIO_HAS_BOOST_THROW_EXCEPTION != 0
390
        // boost exceptions
391
        case hpx::util::boost_exception:
×
392
            HPX_ASSERT(false);    // shouldn't happen
393
            break;
394
#endif
×
395

×
396
        // boost::system::system_error
×
397
        case hpx::util::exception_type::boost_system_error:
×
398
            [[fallthrough]];
399

400
        // std::system_error
401
        case hpx::util::exception_type::std_system_error:
402
            e = hpx::detail::construct_exception(
×
403
                std::system_error(static_cast<int>(err_value),
404
                    std::system_category(), err_message),
405
                hpx::detail::construct_exception_info(throw_function_,
×
406
                    throw_file_, throw_line_, throw_back_trace_,
×
407
                    throw_locality_, throw_hostname_, throw_pid_,
×
408
                    throw_shepherd_, throw_thread_id_, throw_thread_name_,
×
409
                    throw_env_, throw_config_, throw_state_, throw_auxinfo_));
410
            break;
×
411

412
        // hpx::exception
413
        case hpx::util::exception_type::hpx_exception:
414
            e = hpx::detail::construct_exception(
415
                hpx::exception(err_value, what, hpx::throwmode::rethrow),
416
                hpx::detail::construct_exception_info(throw_function_,
417
                    throw_file_, throw_line_, throw_back_trace_,
418
                    throw_locality_, throw_hostname_, throw_pid_,
419
                    throw_shepherd_, throw_thread_id_, throw_thread_name_,
420
                    throw_env_, throw_config_, throw_state_, throw_auxinfo_));
421
            break;
422

423
        // hpx::thread_interrupted
424
        case hpx::util::exception_type::hpx_thread_interrupted_exception:
425
            e = hpx::detail::construct_lightweight_exception(
426
                hpx::thread_interrupted());
427
            break;
428
        }
429
    }
430
}    // namespace hpx::runtime_local::detail
431

432
#include <hpx/config/warnings_suffix.hpp>
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