1 /**
2  * Copyright © 2024 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 <exception>
19 #include <filesystem>
20 #include <string>
21 
22 namespace phosphor::power::sequencer
23 {
24 
25 /**
26  * @class ConfigFileParserError
27  *
28  * An error that occurred while parsing a JSON configuration file.
29  */
30 class ConfigFileParserError : public std::exception
31 {
32   public:
33     // Specify which compiler-generated methods we want
34     ConfigFileParserError() = delete;
35     ConfigFileParserError(const ConfigFileParserError&) = default;
36     ConfigFileParserError(ConfigFileParserError&&) = default;
37     ConfigFileParserError& operator=(const ConfigFileParserError&) = default;
38     ConfigFileParserError& operator=(ConfigFileParserError&&) = default;
39     virtual ~ConfigFileParserError() = default;
40 
41     /**
42      * Constructor.
43      *
44      * @param pathName Configuration file path name
45      * @param error Error message
46      */
ConfigFileParserError(const std::filesystem::path & pathName,const std::string & error)47     explicit ConfigFileParserError(const std::filesystem::path& pathName,
48                                    const std::string& error) :
49         pathName{pathName},
50         error{"ConfigFileParserError: " + pathName.string() + ": " + error}
51     {}
52 
53     /**
54      * Returns the configuration file path name.
55      *
56      * @return path name
57      */
getPathName()58     const std::filesystem::path& getPathName()
59     {
60         return pathName;
61     }
62 
63     /**
64      * Returns the description of this error.
65      *
66      * @return error description
67      */
what() const68     const char* what() const noexcept override
69     {
70         return error.c_str();
71     }
72 
73   private:
74     /**
75      * Configuration file path name.
76      */
77     const std::filesystem::path pathName;
78 
79     /**
80      * Error message.
81      */
82     const std::string error{};
83 };
84 
85 } // namespace phosphor::power::sequencer
86