Skip to content

Commit 7d9eb0d

Browse files
impro: better error reporting for failed ssl contexts
1 parent 3c6e4e9 commit 7d9eb0d

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

include/dpp/ssl_context.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,4 @@ wrapped_ssl_ctx* generate_ssl_context(uint16_t port = 0, const std::string &priv
4949
*/
5050
void release_ssl_context(uint16_t port = 0);
5151

52-
};
52+
};

include/dpp/wrapped_ssl_ctx.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
************************************************************************************/
2222
#include <dpp/exception.h>
2323
#include <openssl/ssl.h>
24+
#include <openssl/err.h>
25+
#include <string>
2426
#pragma once
2527

2628
namespace dpp::detail {
@@ -36,14 +38,28 @@ struct wrapped_ssl_ctx {
3638
*/
3739
SSL_CTX *context{nullptr};
3840

41+
/**
42+
* @brief Get last SSL error message
43+
* @return SSL error message
44+
*/
45+
std::string get_ssl_error() {
46+
unsigned long error_code = ERR_get_error();
47+
if (error_code == 0) {
48+
return "No error";
49+
}
50+
char error_buffer[1024]{0};
51+
ERR_error_string_n(error_code, error_buffer, sizeof(error_buffer));
52+
return std::string(error_buffer);
53+
}
54+
3955
/**
4056
* @brief Create a wrapped SSL context
4157
* @param is_server true to create a server context, false to create a client context
4258
* @throws dpp::connection_exception if context could not be created
4359
*/
4460
explicit wrapped_ssl_ctx(bool is_server = false) : context(SSL_CTX_new(is_server ? TLS_server_method() : TLS_client_method())) {
4561
if (context == nullptr) {
46-
throw dpp::connection_exception(err_ssl_context, "Failed to create SSL client context!");
62+
throw dpp::connection_exception(err_ssl_context, "Failed to create SSL client context: " + get_ssl_error());
4763
}
4864
}
4965

@@ -87,4 +103,4 @@ struct wrapped_ssl_ctx {
87103
}
88104
};
89105

90-
};
106+
};

src/dpp/ssl_context.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,4 @@ wrapped_ssl_ctx* generate_ssl_context(uint16_t port, const std::string &private_
8585
return contexts.back().second.get();
8686
}
8787

88-
}
88+
}

0 commit comments

Comments
 (0)