You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.1 KiB

1 year ago
  1. // Copyright Toru Niina 2017.
  2. // Distributed under the MIT License.
  3. #ifndef TOML11_EXCEPTION_HPP
  4. #define TOML11_EXCEPTION_HPP
  5. #include <array>
  6. #include <string>
  7. #include <stdexcept>
  8. #include <cstring>
  9. #include "source_location.hpp"
  10. namespace toml
  11. {
  12. struct file_io_error : public std::runtime_error
  13. {
  14. public:
  15. file_io_error(int errnum, const std::string& msg, const std::string& fname)
  16. : std::runtime_error(msg + " \"" + fname + "\": errno = " + std::to_string(errnum)),
  17. errno_(errnum)
  18. {}
  19. int get_errno() const noexcept {return errno_;}
  20. private:
  21. int errno_;
  22. };
  23. struct exception : public std::exception
  24. {
  25. public:
  26. explicit exception(const source_location& loc): loc_(loc) {}
  27. virtual ~exception() noexcept override = default;
  28. virtual const char* what() const noexcept override {return "";}
  29. virtual source_location const& location() const noexcept {return loc_;}
  30. protected:
  31. source_location loc_;
  32. };
  33. struct syntax_error : public toml::exception
  34. {
  35. public:
  36. explicit syntax_error(const std::string& what_arg, const source_location& loc)
  37. : exception(loc), what_(what_arg)
  38. {}
  39. virtual ~syntax_error() noexcept override = default;
  40. virtual const char* what() const noexcept override {return what_.c_str();}
  41. protected:
  42. std::string what_;
  43. };
  44. struct type_error : public toml::exception
  45. {
  46. public:
  47. explicit type_error(const std::string& what_arg, const source_location& loc)
  48. : exception(loc), what_(what_arg)
  49. {}
  50. virtual ~type_error() noexcept override = default;
  51. virtual const char* what() const noexcept override {return what_.c_str();}
  52. protected:
  53. std::string what_;
  54. };
  55. struct internal_error : public toml::exception
  56. {
  57. public:
  58. explicit internal_error(const std::string& what_arg, const source_location& loc)
  59. : exception(loc), what_(what_arg)
  60. {}
  61. virtual ~internal_error() noexcept override = default;
  62. virtual const char* what() const noexcept override {return what_.c_str();}
  63. protected:
  64. std::string what_;
  65. };
  66. } // toml
  67. #endif // TOML_EXCEPTION