xref: /openbmc/phosphor-power/phosphor-regulators/src/exception_utils.cpp (revision 0c9a33d66f74a76c1842990f41da678f79fa0c05)
155f496c5SShawn McCarney /**
255f496c5SShawn McCarney  * Copyright © 2020 IBM Corporation
355f496c5SShawn McCarney  *
455f496c5SShawn McCarney  * Licensed under the Apache License, Version 2.0 (the "License");
555f496c5SShawn McCarney  * you may not use this file except in compliance with the License.
655f496c5SShawn McCarney  * You may obtain a copy of the License at
755f496c5SShawn McCarney  *
855f496c5SShawn McCarney  *     http://www.apache.org/licenses/LICENSE-2.0
955f496c5SShawn McCarney  *
1055f496c5SShawn McCarney  * Unless required by applicable law or agreed to in writing, software
1155f496c5SShawn McCarney  * distributed under the License is distributed on an "AS IS" BASIS,
1255f496c5SShawn McCarney  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1355f496c5SShawn McCarney  * See the License for the specific language governing permissions and
1455f496c5SShawn McCarney  * limitations under the License.
1555f496c5SShawn McCarney  */
1655f496c5SShawn McCarney 
1755f496c5SShawn McCarney #include "exception_utils.hpp"
1855f496c5SShawn McCarney 
1955f496c5SShawn McCarney namespace phosphor::power::regulators::exception_utils
2055f496c5SShawn McCarney {
2155f496c5SShawn McCarney 
getExceptions(std::exception_ptr eptr)220414f0ecSShawn McCarney std::vector<std::exception_ptr> getExceptions(std::exception_ptr eptr)
230414f0ecSShawn McCarney {
240414f0ecSShawn McCarney     std::vector<std::exception_ptr> exceptions;
250414f0ecSShawn McCarney     internal::getExceptions(eptr, exceptions);
260414f0ecSShawn McCarney     return exceptions;
270414f0ecSShawn McCarney }
280414f0ecSShawn McCarney 
getMessages(const std::exception & e)2955f496c5SShawn McCarney std::vector<std::string> getMessages(const std::exception& e)
3055f496c5SShawn McCarney {
3155f496c5SShawn McCarney     std::vector<std::string> messages{};
3255f496c5SShawn McCarney     internal::getMessages(e, messages);
3355f496c5SShawn McCarney     return messages;
3455f496c5SShawn McCarney }
3555f496c5SShawn McCarney 
3655f496c5SShawn McCarney namespace internal
3755f496c5SShawn McCarney {
3855f496c5SShawn McCarney 
getExceptions(std::exception_ptr eptr,std::vector<std::exception_ptr> & exceptions)390414f0ecSShawn McCarney void getExceptions(std::exception_ptr eptr,
400414f0ecSShawn McCarney                    std::vector<std::exception_ptr>& exceptions)
410414f0ecSShawn McCarney {
420414f0ecSShawn McCarney     // Verify exception pointer is not null
430414f0ecSShawn McCarney     if (eptr)
440414f0ecSShawn McCarney     {
450414f0ecSShawn McCarney         // If this exception is nested, add inner exception(s) to vector
460414f0ecSShawn McCarney         try
470414f0ecSShawn McCarney         {
480414f0ecSShawn McCarney             std::rethrow_exception(eptr);
490414f0ecSShawn McCarney         }
500414f0ecSShawn McCarney         catch (const std::nested_exception& e)
510414f0ecSShawn McCarney         {
520414f0ecSShawn McCarney             getExceptions(e.nested_ptr(), exceptions);
530414f0ecSShawn McCarney         }
540414f0ecSShawn McCarney         catch (...)
55*0c9a33d6SAdriana Kobylak         {}
560414f0ecSShawn McCarney 
570414f0ecSShawn McCarney         // Append this exception to vector
580414f0ecSShawn McCarney         exceptions.emplace_back(eptr);
590414f0ecSShawn McCarney     }
600414f0ecSShawn McCarney }
610414f0ecSShawn McCarney 
getMessages(const std::exception & e,std::vector<std::string> & messages)6255f496c5SShawn McCarney void getMessages(const std::exception& e, std::vector<std::string>& messages)
6355f496c5SShawn McCarney {
6455f496c5SShawn McCarney     // If this exception is nested, get messages from inner exception(s)
6555f496c5SShawn McCarney     try
6655f496c5SShawn McCarney     {
6755f496c5SShawn McCarney         std::rethrow_if_nested(e);
6855f496c5SShawn McCarney     }
6955f496c5SShawn McCarney     catch (const std::exception& inner)
7055f496c5SShawn McCarney     {
7155f496c5SShawn McCarney         getMessages(inner, messages);
7255f496c5SShawn McCarney     }
7355f496c5SShawn McCarney     catch (...)
74*0c9a33d6SAdriana Kobylak     {}
7555f496c5SShawn McCarney 
7655f496c5SShawn McCarney     // Append error message from this exception
7755f496c5SShawn McCarney     messages.emplace_back(e.what());
7855f496c5SShawn McCarney }
7955f496c5SShawn McCarney 
8055f496c5SShawn McCarney } // namespace internal
8155f496c5SShawn McCarney 
8255f496c5SShawn McCarney } // namespace phosphor::power::regulators::exception_utils
83