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 #pragma once 17 18 #include "journal.hpp" 19 20 #include <exception> 21 #include <string> 22 #include <vector> 23 24 /** 25 * @namespace exception_utils 26 * 27 * Contains utility functions for handling exceptions. 28 */ 29 namespace phosphor::power::regulators::exception_utils 30 { 31 32 /** 33 * Returns a vector containing the specified exception and any nested inner 34 * exceptions. 35 * 36 * If the exception contains nested inner exceptions, the returned vector will 37 * be ordered from innermost exception to outermost exception. 38 * 39 * This function makes it easier to handle nested exceptions. You can iterate 40 * over them in a simple loop instead of writing a recursive function. 41 * 42 * @param eptr exception pointer 43 * @return vector of exceptions, from innermost to outermost 44 */ 45 std::vector<std::exception_ptr> getExceptions(std::exception_ptr eptr); 46 47 /** 48 * Gets the error messages from the specified exception and any nested inner 49 * exceptions. 50 * 51 * If the exception contains nested inner exceptions, the messages in the 52 * returned vector will be ordered from innermost exception to outermost 53 * exception. 54 * 55 * @param e exception 56 * @return error messages from exceptions 57 */ 58 std::vector<std::string> getMessages(const std::exception& e); 59 60 /* 61 * Internal implementation details 62 */ 63 namespace internal 64 { 65 66 /** 67 * Builds a vector containing the specified exception and any nested inner 68 * exceptions. 69 * 70 * Stores the exceptions in the specified vector, from innermost exception to 71 * outermost exception. 72 * 73 * @param eptr exception pointer 74 * @param exceptions vector where exceptions will be stored 75 */ 76 void getExceptions(std::exception_ptr eptr, 77 std::vector<std::exception_ptr>& exceptions); 78 79 /** 80 * Gets the error messages from the specified exception and any nested inner 81 * exceptions. 82 * 83 * Stores the error messages in the specified vector, from innermost exception 84 * to outermost exception. 85 * 86 * @param e exception 87 * @param messages vector where error messages will be stored 88 */ 89 void getMessages(const std::exception& e, std::vector<std::string>& messages); 90 91 } // namespace internal 92 93 } // namespace phosphor::power::regulators::exception_utils 94