blob: 965deb556ff8a781f369b6df5b6609a40fc0fde3 [file] [log] [blame]
Shawn McCarney55f496c2020-04-09 16:19:14 -05001/**
2 * Copyright © 2020 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "exception_utils.hpp"
18
19namespace phosphor::power::regulators::exception_utils
20{
21
Shawn McCarney0414f0e2021-03-17 16:36:03 -050022std::vector<std::exception_ptr> getExceptions(std::exception_ptr eptr)
23{
24 std::vector<std::exception_ptr> exceptions;
25 internal::getExceptions(eptr, exceptions);
26 return exceptions;
27}
28
Shawn McCarney55f496c2020-04-09 16:19:14 -050029std::vector<std::string> getMessages(const std::exception& e)
30{
31 std::vector<std::string> messages{};
32 internal::getMessages(e, messages);
33 return messages;
34}
35
Shawn McCarney55f496c2020-04-09 16:19:14 -050036namespace internal
37{
38
Shawn McCarney0414f0e2021-03-17 16:36:03 -050039void getExceptions(std::exception_ptr eptr,
40 std::vector<std::exception_ptr>& exceptions)
41{
42 // Verify exception pointer is not null
43 if (eptr)
44 {
45 // If this exception is nested, add inner exception(s) to vector
46 try
47 {
48 std::rethrow_exception(eptr);
49 }
50 catch (const std::nested_exception& e)
51 {
52 getExceptions(e.nested_ptr(), exceptions);
53 }
54 catch (...)
Adriana Kobylak0c9a33d2021-09-13 18:05:09 +000055 {}
Shawn McCarney0414f0e2021-03-17 16:36:03 -050056
57 // Append this exception to vector
58 exceptions.emplace_back(eptr);
59 }
60}
61
Shawn McCarney55f496c2020-04-09 16:19:14 -050062void getMessages(const std::exception& e, std::vector<std::string>& messages)
63{
64 // If this exception is nested, get messages from inner exception(s)
65 try
66 {
67 std::rethrow_if_nested(e);
68 }
69 catch (const std::exception& inner)
70 {
71 getMessages(inner, messages);
72 }
73 catch (...)
Adriana Kobylak0c9a33d2021-09-13 18:05:09 +000074 {}
Shawn McCarney55f496c2020-04-09 16:19:14 -050075
76 // Append error message from this exception
77 messages.emplace_back(e.what());
78}
79
80} // namespace internal
81
82} // namespace phosphor::power::regulators::exception_utils