Skip to content

Commit 5432ed0

Browse files
committed
TCP input TLS - Add helper functions for throwing OpenSSL errors.
1 parent 30901d9 commit 5432ed0

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* \file
3+
* \author Jakub Antonín Štigler <[email protected]>
4+
* \brief Helper functions for forming OpenSSL error messages. (header file)
5+
* \date 2025
6+
*
7+
* Copyright: (C) 2023 CESNET, z.s.p.o.
8+
* SPDX-License-Identifier: BSD-3-Clause
9+
*/
10+
11+
#pragma once
12+
13+
#include <array>
14+
#include <sstream>
15+
#include <string>
16+
17+
#include <openssl/err.h>
18+
19+
namespace tcp_in {
20+
namespace tls {
21+
22+
/**
23+
* @brief Throws exception with the given message appended with arguments and OpenSSL error message
24+
* based on error code.
25+
* @param code Error code of the OpenSSL message.
26+
* @param msg Message describing the error.
27+
*/
28+
[[noreturn]]
29+
static void throw_ssl_err(int code, std::ostringstream msg) {
30+
std::array<char, 256> err;
31+
ERR_error_string_n(code, err.data(), err.size());
32+
msg << err.data();
33+
34+
auto res = msg.str();
35+
if (*res.rbegin() == '.') {
36+
res.pop_back();
37+
res += ": ";
38+
}
39+
40+
throw std::runtime_error(res + err.data());
41+
}
42+
43+
/**
44+
* @brief Throws exception with the given message appended with arguments and OpenSSL error message
45+
* based on error code.
46+
* @param code Error code of the OpenSSL message.
47+
* @param msg Partial message describing the error.
48+
* @param arg Next argument to be appended to the message.
49+
* @param args This will be converted to string and appended to the message.
50+
*/
51+
template<typename ARG, typename... ARGS>
52+
[[noreturn]]
53+
void throw_ssl_err(int code, std::ostringstream msg, ARG &&arg, ARGS &&...args) {
54+
msg << arg;
55+
throw_ssl_err(code, std::move(msg), std::forward<ARGS>(args)...);
56+
}
57+
58+
/**
59+
* @brief Throws exception with the given message appended with arguments and OpenSSL error message
60+
* based on error code.
61+
* @param code Error code of the OpenSSL message.
62+
* @param what Message describing the error.
63+
* @param args This will be converted to string and appended to the message.
64+
*/
65+
template<typename... ARGS>
66+
[[noreturn]]
67+
void throw_ssl_err(int code, const std::string &what, ARGS &&...args) {
68+
std::ostringstream res;
69+
res << what;
70+
throw_ssl_err(code, std::move(res), std::forward<ARGS>(args)...);
71+
}
72+
73+
/**
74+
* @brief Throws exception with the given message appended with arguments and error message from top
75+
* of the OpenSSL error stack. The error is popped from the error stack.
76+
* @param what Message describing the error.
77+
* @param args This will be converted to string and appended to the message.
78+
*/
79+
template<typename... ARGS>
80+
[[noreturn]]
81+
void throw_ssl_err(const std::string &what, ARGS &&...args) {
82+
throw_ssl_err(ERR_get_error(), what, std::forward<ARGS>(args)...);
83+
}
84+
85+
} // namespace tls
86+
} // namespace tcp_in

0 commit comments

Comments
 (0)