Skip to content

Commit

Permalink
Improve exception error messages
Browse files Browse the repository at this point in the history
* Inherit exceptions from std::runtime_error
* Refactor error message functions
* Refactor path construction functions
* Add path to error messages
  • Loading branch information
muesli4 committed Dec 26, 2019
1 parent f53e5de commit a31c20e
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 94 deletions.
41 changes: 23 additions & 18 deletions lib/libconfig.h++
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#define __libconfig_hpp

#include <stdio.h>
#include <exception>
#include <stdexcept>
#include <string>

#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
Expand Down Expand Up @@ -54,14 +54,31 @@ struct config_setting_t; // fwd decl

namespace libconfig {

class LIBCONFIGXX_API ConfigException : public std::exception { };
struct LIBCONFIGXX_API ConfigException : public std::runtime_error
{
ConfigException();
ConfigException(std::string const &message);

ConfigException(ConfigException const &other);
ConfigException& operator=(ConfigException const &other);

virtual ~ConfigException() LIBCONFIGXX_NOEXCEPT;
};

class Setting; // fwd decl
class SettingIterator;
class SettingConstIterator;

class LIBCONFIGXX_API SettingException : public ConfigException
{

protected:

SettingException(char const *derivedType, const Setting &setting);
SettingException(char const *derivedType, const Setting &setting, int idx);
SettingException(char const *derivedType, const Setting &setting, const char *name);
SettingException(char const *derivedType, std::string path);

public:

SettingException(const Setting &setting);
Expand All @@ -74,13 +91,11 @@ class LIBCONFIGXX_API SettingException : public ConfigException

virtual ~SettingException() LIBCONFIGXX_NOEXCEPT;

const char *getPath() const;

virtual const char *what() const LIBCONFIGXX_NOEXCEPT;
std::string const & getPath() const;

private:

char *_path;
std::string _path;
};

class LIBCONFIGXX_API SettingTypeException : public SettingException
Expand All @@ -90,8 +105,6 @@ class LIBCONFIGXX_API SettingTypeException : public SettingException
SettingTypeException(const Setting &setting);
SettingTypeException(const Setting &setting, int idx);
SettingTypeException(const Setting &setting, const char *name);

virtual const char *what() const LIBCONFIGXX_NOEXCEPT;
};

class LIBCONFIGXX_API SettingNotFoundException : public SettingException
Expand All @@ -101,24 +114,18 @@ class LIBCONFIGXX_API SettingNotFoundException : public SettingException
SettingNotFoundException(const char *path);
SettingNotFoundException(const Setting &setting, int idx);
SettingNotFoundException(const Setting &setting, const char *name);

virtual const char *what() const LIBCONFIGXX_NOEXCEPT;
};

class LIBCONFIGXX_API SettingNameException : public SettingException
{
public:

SettingNameException(const Setting &setting, const char *name);

virtual const char *what() const LIBCONFIGXX_NOEXCEPT;
};

class LIBCONFIGXX_API FileIOException : public ConfigException
struct LIBCONFIGXX_API FileIOException : public ConfigException
{
public:

virtual const char *what() const LIBCONFIGXX_NOEXCEPT;
FileIOException();
};

class LIBCONFIGXX_API ParseException : public ConfigException
Expand All @@ -140,8 +147,6 @@ class LIBCONFIGXX_API ParseException : public ConfigException
inline const char *getError() const
{ return(_error); }

virtual const char *what() const LIBCONFIGXX_NOEXCEPT;

private:

const char *_file;
Expand Down
Loading

0 comments on commit a31c20e

Please sign in to comment.