exception.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include "ctypes.h"
  2. #include <string>
  3. #include <exception>
  4. namespace binom {
  5. /// Exceptions categories
  6. enum ErrCode : ui8 {
  7. // General exceptions
  8. any,
  9. memory_allocation_error,
  10. memory_free_error,
  11. out_of_range,
  12. file_open_error,
  13. invalid_data,
  14. // BinOM Exceptions
  15. binom_invalid_type,
  16. binom_out_of_range,
  17. binom_object_key_error,
  18. binom_query_invalid_field,
  19. binom_invalid_visitor,
  20. // BinOM DataBase Exceptions
  21. binomdb_invalid_file,
  22. binomdb_invalid_storage_version,
  23. binomdb_memory_management_error,
  24. binomdb_page_isnt_exist,
  25. binomdb_block_isnt_exist,
  26. // BinOM Lexer
  27. unexpected_expression
  28. };
  29. class Exception : public std::exception {
  30. ErrCode _code;
  31. const char* error_string = nullptr;
  32. static const char* ectos(ErrCode code) {
  33. switch (code) {
  34. case ErrCode::memory_allocation_error: return "Memory allocation error";
  35. case ErrCode::memory_free_error: return "Memory free error";
  36. case ErrCode::out_of_range: return "Out of range";
  37. case ErrCode::file_open_error: return "File open error";
  38. case ErrCode::invalid_data: return "Invalid data";
  39. case ErrCode::binom_invalid_type: return "Invalid BinOM type";
  40. case ErrCode::binom_out_of_range: return "Out of BinOM container range";
  41. case ErrCode::binom_object_key_error: return "Invalide object key";
  42. case ErrCode::binom_invalid_visitor: return "Invalide visitor";
  43. case ErrCode::binomdb_invalid_file: return "BinOM DB invalid file";
  44. case ErrCode::binomdb_invalid_storage_version:
  45. return "BinOM DB invalid file storage version";
  46. case ErrCode::binomdb_memory_management_error:
  47. return "BinOM DB memory management error";
  48. case ErrCode::binomdb_page_isnt_exist:
  49. return "BinOM DB page isn't exist";
  50. case ErrCode::binomdb_block_isnt_exist:
  51. return "BinOM DB block isn't exist";
  52. case ErrCode::binom_query_invalid_field:
  53. return "BinOM invalid Query field";
  54. case ErrCode::unexpected_expression: return "Unexpected expression";
  55. case ErrCode::any: return "Unknown exception";
  56. default: return "INVALID ErrCode!";
  57. }
  58. }
  59. public:
  60. Exception(const ErrCode code, const char* error_string) : std::exception(), _code(code), error_string(error_string) {}
  61. Exception(const ErrCode code) : std::exception(), _code(code), error_string(nullptr) {}
  62. ErrCode code() const {return _code;}
  63. const char* what() const noexcept {return ectos(_code);}
  64. std::string full() const {
  65. return error_string
  66. ? std::string(ectos(_code)) + ": " + error_string
  67. : std::string(ectos(_code));
  68. }
  69. [[noreturn]] inline void throwUp() {throw *this;}
  70. };
  71. }