Shawn McCarney | 55f496c | 2020-04-09 16:19:14 -0500 | [diff] [blame] | 1 | /** |
| 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 | |
| 19 | namespace phosphor::power::regulators::exception_utils |
| 20 | { |
| 21 | |
Shawn McCarney | 0414f0e | 2021-03-17 16:36:03 -0500 | [diff] [blame] | 22 | std::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 McCarney | 55f496c | 2020-04-09 16:19:14 -0500 | [diff] [blame] | 29 | std::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 McCarney | 55f496c | 2020-04-09 16:19:14 -0500 | [diff] [blame] | 36 | namespace internal |
| 37 | { |
| 38 | |
Shawn McCarney | 0414f0e | 2021-03-17 16:36:03 -0500 | [diff] [blame] | 39 | void 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 Kobylak | 0c9a33d | 2021-09-13 18:05:09 +0000 | [diff] [blame] | 55 | {} |
Shawn McCarney | 0414f0e | 2021-03-17 16:36:03 -0500 | [diff] [blame] | 56 | |
| 57 | // Append this exception to vector |
| 58 | exceptions.emplace_back(eptr); |
| 59 | } |
| 60 | } |
| 61 | |
Shawn McCarney | 55f496c | 2020-04-09 16:19:14 -0500 | [diff] [blame] | 62 | void 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 Kobylak | 0c9a33d | 2021-09-13 18:05:09 +0000 | [diff] [blame] | 74 | {} |
Shawn McCarney | 55f496c | 2020-04-09 16:19:14 -0500 | [diff] [blame] | 75 | |
| 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 |