1 | // SPDX-License-Identifier: LGPL-2.0-or-later |
---|---|
2 | |
3 | #include "exception_p.h" |
4 | |
5 | #include <exception> |
6 | |
7 | #include <QUnhandledException> |
8 | |
9 | namespace |
10 | { |
11 | std::optional<KCrash::ExceptionMetadata> qUnhandledExceptionMetadata(const QUnhandledException &exception) |
12 | { |
13 | auto exceptionPtr = exception.exception(); |
14 | if (!exceptionPtr) { |
15 | return {}; |
16 | } |
17 | |
18 | try { |
19 | std::rethrow_exception(exceptionPtr); |
20 | } catch (const std::exception &e) { |
21 | return KCrash::ExceptionMetadata{ |
22 | .ptr = exceptionPtr, |
23 | .klass = typeid(e).name(), |
24 | .what = e.what(), |
25 | }; |
26 | } |
27 | |
28 | return {}; |
29 | } |
30 | } // namespace |
31 | |
32 | namespace KCrash |
33 | { |
34 | std::optional<ExceptionMetadata> exceptionMetadata() |
35 | { |
36 | auto exceptionPtr = std::current_exception(); |
37 | if (!exceptionPtr) { |
38 | return {}; |
39 | } |
40 | |
41 | try { |
42 | std::rethrow_exception(exceptionPtr); |
43 | } catch (const QUnhandledException &e) { |
44 | return qUnhandledExceptionMetadata(exception: e); |
45 | } catch (const std::bad_alloc &e) { |
46 | return {}; |
47 | } catch (const std::exception &e) { |
48 | return KCrash::ExceptionMetadata{ |
49 | .ptr = exceptionPtr, |
50 | .klass = typeid(e).name(), |
51 | .what = e.what(), |
52 | }; |
53 | } |
54 | |
55 | return {}; |
56 | } |
57 | } // namespace KCrash |
58 |