exception, curl: switch to a reusable numbered errors

Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
Simon Rozman 2025-06-17 16:45:09 +02:00
parent e0cbc4a90a
commit 7aa64cb625
2 changed files with 54 additions and 20 deletions

View File

@ -6,6 +6,7 @@
#pragma once
#include "compat.hpp"
#include "exception.hpp"
#include <curl/curl.h>
#include <memory>
#include <string>
@ -16,7 +17,7 @@ namespace stdex
///
/// CURL runtime error
///
class curl_runtime_error : public std::runtime_error
class curl_runtime_error : public num_runtime_error<CURLcode>
{
public:
///
@ -24,10 +25,9 @@ namespace stdex
///
/// \param[in] num CURL error code
///
curl_runtime_error(_In_ CURLcode num) :
runtime_error(curl_easy_strerror(num)),
m_num(num)
{}
curl_runtime_error(_In_ error_type num) : stdex::num_runtime_error<CURLcode>(num, curl_easy_strerror(num))
{
}
///
/// Constructs an exception
@ -35,10 +35,9 @@ namespace stdex
/// \param[in] num CURL error code
/// \param[in] msg Error message
///
curl_runtime_error(_In_ CURLcode num, _In_ const std::string& msg) :
runtime_error(msg + ": " + curl_easy_strerror(num)),
m_num(num)
{}
curl_runtime_error(_In_ error_type num, _In_ const std::string& msg) : stdex::num_runtime_error<CURLcode>(num, msg + ": " + curl_easy_strerror(num))
{
}
///
/// Constructs an exception
@ -46,21 +45,12 @@ namespace stdex
/// \param[in] num CURL error code
/// \param[in] msg Error message
///
curl_runtime_error(_In_ CURLcode num, _In_z_ const char *msg) :
runtime_error(std::string(msg) + ": " + curl_easy_strerror(num)),
m_num(num)
{}
///
/// Returns the error number
///
CURLcode number() const
curl_runtime_error(_In_ error_type num, _In_z_ const char *msg) : stdex::num_runtime_error<CURLcode>(num, std::string(msg) + ": " + curl_easy_strerror(num))
{
return m_num;
}
protected:
CURLcode m_num; ///< Numeric error code
error_type m_num; ///< Numeric error code
};
///

View File

@ -41,4 +41,48 @@ namespace stdex
catch (...) {}
return ex.what();
}
///
/// Numerical runtime error
///
template <typename _Tn>
class num_runtime_error : public std::runtime_error
{
public:
typedef _Tn error_type; ///< Error number type
public:
///
/// Constructs an exception
///
/// \param[in] num Numeric error code
/// \param[in] msg Error message
///
num_runtime_error(_In_ error_type num, _In_ const std::string& msg) :
m_num(num),
runtime_error(msg)
{}
///
/// Constructs an exception
///
/// \param[in] num Numeric error code
/// \param[in] msg Error message
///
num_runtime_error(_In_ error_type num, _In_z_ const char *msg) :
m_num(num),
runtime_error(msg)
{}
///
/// Returns the error number
///
error_type number() const
{
return m_num;
}
protected:
error_type m_num; ///< Numeric error code
};
}