1*8f0d1428SShawn McCarney /**
2*8f0d1428SShawn McCarney  * Copyright © 2020 IBM Corporation
3*8f0d1428SShawn McCarney  *
4*8f0d1428SShawn McCarney  * Licensed under the Apache License, Version 2.0 (the "License");
5*8f0d1428SShawn McCarney  * you may not use this file except in compliance with the License.
6*8f0d1428SShawn McCarney  * You may obtain a copy of the License at
7*8f0d1428SShawn McCarney  *
8*8f0d1428SShawn McCarney  *     http://www.apache.org/licenses/LICENSE-2.0
9*8f0d1428SShawn McCarney  *
10*8f0d1428SShawn McCarney  * Unless required by applicable law or agreed to in writing, software
11*8f0d1428SShawn McCarney  * distributed under the License is distributed on an "AS IS" BASIS,
12*8f0d1428SShawn McCarney  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8f0d1428SShawn McCarney  * See the License for the specific language governing permissions and
14*8f0d1428SShawn McCarney  * limitations under the License.
15*8f0d1428SShawn McCarney  */
16*8f0d1428SShawn McCarney #pragma once
17*8f0d1428SShawn McCarney 
18*8f0d1428SShawn McCarney #include "action.hpp"
19*8f0d1428SShawn McCarney 
20*8f0d1428SShawn McCarney #include <exception>
21*8f0d1428SShawn McCarney #include <string>
22*8f0d1428SShawn McCarney 
23*8f0d1428SShawn McCarney namespace phosphor::power::regulators
24*8f0d1428SShawn McCarney {
25*8f0d1428SShawn McCarney 
26*8f0d1428SShawn McCarney /**
27*8f0d1428SShawn McCarney  * @class ActionError
28*8f0d1428SShawn McCarney  *
29*8f0d1428SShawn McCarney  * An error that occurred while executing an action.
30*8f0d1428SShawn McCarney  *
31*8f0d1428SShawn McCarney  * This exception describes the action that failed.  If the cause of the failure
32*8f0d1428SShawn McCarney  * was another exception (such as I2CException), the other exception can be
33*8f0d1428SShawn McCarney  * nested inside the ActionError using std::throw_with_nested().
34*8f0d1428SShawn McCarney  */
35*8f0d1428SShawn McCarney class ActionError : public std::exception
36*8f0d1428SShawn McCarney {
37*8f0d1428SShawn McCarney   public:
38*8f0d1428SShawn McCarney     // Specify which compiler-generated methods we want
39*8f0d1428SShawn McCarney     ActionError() = delete;
40*8f0d1428SShawn McCarney     ActionError(const ActionError&) = default;
41*8f0d1428SShawn McCarney     ActionError(ActionError&&) = default;
42*8f0d1428SShawn McCarney     ActionError& operator=(const ActionError&) = default;
43*8f0d1428SShawn McCarney     ActionError& operator=(ActionError&&) = default;
44*8f0d1428SShawn McCarney     virtual ~ActionError() = default;
45*8f0d1428SShawn McCarney 
46*8f0d1428SShawn McCarney     /**
47*8f0d1428SShawn McCarney      * Constructor.
48*8f0d1428SShawn McCarney      *
49*8f0d1428SShawn McCarney      * @param action action that was executed
50*8f0d1428SShawn McCarney      * @param error error message
51*8f0d1428SShawn McCarney      */
ActionError(const Action & action,const std::string & error="")52*8f0d1428SShawn McCarney     explicit ActionError(const Action& action, const std::string& error = "") :
53*8f0d1428SShawn McCarney         message{"ActionError: " + action.toString()}
54*8f0d1428SShawn McCarney     {
55*8f0d1428SShawn McCarney         if (error.length() > 0)
56*8f0d1428SShawn McCarney         {
57*8f0d1428SShawn McCarney             message += ": ";
58*8f0d1428SShawn McCarney             message += error;
59*8f0d1428SShawn McCarney         }
60*8f0d1428SShawn McCarney 
61*8f0d1428SShawn McCarney         // Note: Do not store a reference or pointer to the Action.  It may be
62*8f0d1428SShawn McCarney         // destructed (out of scope) before the exception is caught.
63*8f0d1428SShawn McCarney     }
64*8f0d1428SShawn McCarney 
65*8f0d1428SShawn McCarney     /**
66*8f0d1428SShawn McCarney      * Returns the description of this error.
67*8f0d1428SShawn McCarney      *
68*8f0d1428SShawn McCarney      * @return error description
69*8f0d1428SShawn McCarney      */
what() const70*8f0d1428SShawn McCarney     const char* what() const noexcept override
71*8f0d1428SShawn McCarney     {
72*8f0d1428SShawn McCarney         return message.c_str();
73*8f0d1428SShawn McCarney     }
74*8f0d1428SShawn McCarney 
75*8f0d1428SShawn McCarney   private:
76*8f0d1428SShawn McCarney     /**
77*8f0d1428SShawn McCarney      * Message describing this exception.
78*8f0d1428SShawn McCarney      */
79*8f0d1428SShawn McCarney     std::string message{};
80*8f0d1428SShawn McCarney };
81*8f0d1428SShawn McCarney 
82*8f0d1428SShawn McCarney } // namespace phosphor::power::regulators
83