ExceptionOpenssl.hpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #pragma once
  2. #include "Exception.hpp"
  3. #include <openssl/err.h>
  4. namespace nkg {
  5. class OpensslError final : public Exception {
  6. private:
  7. unsigned long _ErrorCode;
  8. std::xstring _ErrorString;
  9. public:
  10. OpensslError(PCTSTR SourceFile, SIZE_T SourceLine, unsigned long OpensslErrorCode, PCTSTR CustomMessage) noexcept :
  11. Exception(SourceFile, SourceLine, CustomMessage),
  12. _ErrorCode(OpensslErrorCode)
  13. {
  14. static bool CryptoStringsLoaded = false;
  15. if (CryptoStringsLoaded == false) {
  16. ERR_load_crypto_strings();
  17. CryptoStringsLoaded = true;
  18. }
  19. _ErrorString = std::xstring(std::xstring_extension{}, ERR_reason_error_string(_ErrorCode), CP_UTF8);
  20. }
  21. [[nodiscard]]
  22. virtual bool HasErrorCode() const noexcept override {
  23. return true;
  24. }
  25. [[nodiscard]]
  26. virtual ULONG_PTR ErrorCode() const noexcept override {
  27. return _ErrorCode;
  28. }
  29. [[nodiscard]]
  30. virtual PCTSTR ErrorString() const noexcept override {
  31. return _ErrorString.c_str();
  32. }
  33. };
  34. }