Skip to content

Commit 2519815

Browse files
committed
Improve exception error messages
* Inherit exceptions from std::runtime_error * Refactor error message functions * Refactor path construction functions * Add path to error messages
1 parent 4f13b7f commit 2519815

2 files changed

Lines changed: 157 additions & 104 deletions

File tree

lib/libconfig.h++

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#define __libconfig_hpp
2525

2626
#include <stdio.h>
27-
#include <exception>
27+
#include <stdexcept>
2828
#include <string>
2929

3030
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
@@ -54,33 +54,42 @@ struct config_setting_t; // fwd decl
5454

5555
namespace libconfig {
5656

57-
class LIBCONFIGXX_API ConfigException : public std::exception { };
57+
struct LIBCONFIGXX_API ConfigException : public std::runtime_error
58+
{
59+
ConfigException(std::string const &message);
60+
61+
ConfigException(ConfigException const &other);
62+
ConfigException& operator=(ConfigException const &other);
63+
64+
virtual ~ConfigException() LIBCONFIGXX_NOEXCEPT;
65+
};
5866

5967
class Setting; // fwd decl
6068
class SettingIterator;
6169
class SettingConstIterator;
6270

6371
class LIBCONFIGXX_API SettingException : public ConfigException
6472
{
65-
public:
6673

67-
SettingException(const Setting &setting);
68-
SettingException(const Setting &setting, int idx);
69-
SettingException(const Setting &setting, const char *name);
70-
SettingException(const char *path);
74+
protected:
75+
76+
SettingException(char const *messagePrefix, const Setting &setting);
77+
SettingException(char const *messagePrefix, const Setting &setting, int idx);
78+
SettingException(char const *messagePrefix, const Setting &setting, const char *name);
79+
SettingException(char const *messagePrefix, std::string path);
80+
81+
public:
7182

7283
SettingException(const SettingException &other);
7384
SettingException& operator=(const SettingException &other);
7485

7586
virtual ~SettingException() LIBCONFIGXX_NOEXCEPT;
7687

77-
const char *getPath() const;
78-
79-
virtual const char *what() const LIBCONFIGXX_NOEXCEPT;
88+
std::string const & getPath() const;
8089

8190
private:
8291

83-
char *_path;
92+
std::string _path;
8493
};
8594

8695
class LIBCONFIGXX_API SettingTypeException : public SettingException
@@ -91,7 +100,9 @@ class LIBCONFIGXX_API SettingTypeException : public SettingException
91100
SettingTypeException(const Setting &setting, int idx);
92101
SettingTypeException(const Setting &setting, const char *name);
93102

94-
virtual const char *what() const LIBCONFIGXX_NOEXCEPT;
103+
private:
104+
105+
static const char * const ERROR_PREFIX;
95106
};
96107

97108
class LIBCONFIGXX_API SettingRangeException : public SettingException
@@ -102,7 +113,9 @@ class LIBCONFIGXX_API SettingRangeException : public SettingException
102113
SettingRangeException(const Setting &setting, int idx);
103114
SettingRangeException(const Setting &setting, const char *name);
104115

105-
virtual const char *what() const LIBCONFIGXX_NOEXCEPT;
116+
private:
117+
118+
static const char * const ERROR_PREFIX;
106119
};
107120

108121
class LIBCONFIGXX_API SettingNotFoundException : public SettingException
@@ -113,23 +126,21 @@ class LIBCONFIGXX_API SettingNotFoundException : public SettingException
113126
SettingNotFoundException(const Setting &setting, int idx);
114127
SettingNotFoundException(const Setting &setting, const char *name);
115128

116-
virtual const char *what() const LIBCONFIGXX_NOEXCEPT;
129+
private:
130+
131+
static const char * const ERROR_PREFIX;
117132
};
118133

119134
class LIBCONFIGXX_API SettingNameException : public SettingException
120135
{
121136
public:
122137

123138
SettingNameException(const Setting &setting, const char *name);
124-
125-
virtual const char *what() const LIBCONFIGXX_NOEXCEPT;
126139
};
127140

128-
class LIBCONFIGXX_API FileIOException : public ConfigException
141+
struct LIBCONFIGXX_API FileIOException : public ConfigException
129142
{
130-
public:
131-
132-
virtual const char *what() const LIBCONFIGXX_NOEXCEPT;
143+
FileIOException();
133144
};
134145

135146
class LIBCONFIGXX_API ParseException : public ConfigException
@@ -151,8 +162,6 @@ class LIBCONFIGXX_API ParseException : public ConfigException
151162
inline const char *getError() const
152163
{ return(_error); }
153164

154-
virtual const char *what() const LIBCONFIGXX_NOEXCEPT;
155-
156165
private:
157166

158167
const char *_file;

0 commit comments

Comments
 (0)