status_codes.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include "status_codes.h"
  2. #include <stdexcept>
  3. #define SERVER_PREFIX "[SERVER] "
  4. #define MESSAGE_NOT_FOUND "Unknown response code."
  5. #define STATUS_CODE_NOT_INIT_ERROR \
  6. "Please call init method before use status_code object."
  7. #define INVALID_USER_ERROR "User not found."
  8. #define EXISTING_USER_ERROR "User already logged in."
  9. #define INVALID_SESSION_KEY_ERROR "Username and session key does not match."
  10. #define SESSION_KEY_NOT_FOUND_ERROR "Session key not found."
  11. #define INVALID_REQUEST_ERROR "Recived invalid request."
  12. #define INVALID_DATA_ERROR "Recived invalid data."
  13. #define EXISTING_RECORD_ERROR "Sensor already exists."
  14. #define NO_DATA_FOUND_ERROR "Sensor not found."
  15. #define NO_DB_FOUND_ERROR "Database not found."
  16. unordered_map<status, string> *status_codes::messages = nullptr;
  17. using std::bad_alloc;
  18. using std::logic_error;
  19. using std::runtime_error;
  20. void status_codes::init()
  21. {
  22. status_codes::messages = new unordered_map<status, string>;
  23. if (status_codes::messages == nullptr)
  24. throw bad_alloc();
  25. status_codes::messages->insert(
  26. { status::INVALID_USER, INVALID_USER_ERROR });
  27. status_codes::messages->insert(
  28. { status::EXISTING_USER, EXISTING_USER_ERROR });
  29. status_codes::messages->insert(
  30. { status::INVALID_SESSION_KEY, INVALID_SESSION_KEY_ERROR });
  31. status_codes::messages->insert(
  32. { status::SESSION_KEY_NOT_FOUND, SESSION_KEY_NOT_FOUND_ERROR });
  33. status_codes::messages->insert(
  34. { status::INVALID_REQUEST, INVALID_REQUEST_ERROR });
  35. status_codes::messages->insert(
  36. { status::INVALID_DATA, INVALID_DATA_ERROR });
  37. status_codes::messages->insert(
  38. { status::EXISTING_RECORD, EXISTING_RECORD_ERROR });
  39. status_codes::messages->insert(
  40. { status::NO_DATA_FOUND, NO_DATA_FOUND_ERROR });
  41. status_codes::messages->insert(
  42. { status::NO_DB_FOUND, NO_DB_FOUND_ERROR });
  43. }
  44. void status_codes::destroy()
  45. {
  46. if (status_codes::messages == nullptr)
  47. throw logic_error(STATUS_CODE_NOT_INIT_ERROR);
  48. delete messages;
  49. }
  50. string status_codes::get(status r)
  51. {
  52. if (status_codes::messages == nullptr)
  53. throw logic_error(STATUS_CODE_NOT_INIT_ERROR);
  54. if (messages->find(r) == messages->end())
  55. throw runtime_error(MESSAGE_NOT_FOUND);
  56. return SERVER_PREFIX + messages->at(r);
  57. }