From 8de04736690b8dde4ad52dd8a1e025acd218a42a Mon Sep 17 00:00:00 2001 From: Syping Date: Sun, 5 Apr 2026 00:09:18 +0200 Subject: [PATCH] curl_exception: implement exception class to throw libcurl errors --- CMakeLists.txt | 4 +++- src/core/curl_exception.cpp | 30 +++++++++++++++++++++++++++ src/core/curl_exception.h | 41 +++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 src/core/curl_exception.cpp create mode 100644 src/core/curl_exception.h diff --git a/CMakeLists.txt b/CMakeLists.txt index c9da687..34c997a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,11 +18,12 @@ cmake_minimum_required(VERSION 3.16) cmake_policy(VERSION 3.16...3.27) -project(dtranslatebot VERSION 0.3.3 LANGUAGES CXX) +project(dtranslatebot VERSION 0.4.0 LANGUAGES CXX) include(GNUInstallDirs) # dtranslatebot Source files set(DTRANSLATEBOT_HEADERS + src/core/curl_exception.h src/core/database.h src/core/http_headers.h src/core/http_request.h @@ -43,6 +44,7 @@ set(DTRANSLATEBOT_HEADERS src/translator/stub/stub.h ) set(DTRANSLATEBOT_SOURCES + src/core/curl_exception.cpp src/core/database.cpp src/core/http_headers.cpp src/core/http_request.cpp diff --git a/src/core/curl_exception.cpp b/src/core/curl_exception.cpp new file mode 100644 index 0000000..e39931e --- /dev/null +++ b/src/core/curl_exception.cpp @@ -0,0 +1,30 @@ +/***************************************************************************** +* dtranslatebot Discord Translate Bot +* Copyright (C) 2026 Syping +* +* Redistribution and use in source and binary forms, with or without modification, +* are permitted provided that the following conditions are met: +* +* 1. Redistributions of source code must retain the above copyright notice, +* this list of conditions and the following disclaimer. +* +* 2. Redistributions in binary form must reproduce the above copyright notice, +* this list of conditions and the following disclaimer in the documentation +* and/or other materials provided with the distribution. +* +* This software is provided as-is, no warranties are given to you, we are not +* responsible for anything with use of the software, you are self responsible. +*****************************************************************************/ + +#include "curl_exception.h" +using namespace bot::exception; + +curl_exception::curl_exception(const std::string &message, CURLcode error_code) : m_message(message), m_error_code(error_code) { +} + +curl_exception::~curl_exception() noexcept { +} + +const char* curl_exception::what() const noexcept { + return m_message.c_str(); +} diff --git a/src/core/curl_exception.h b/src/core/curl_exception.h new file mode 100644 index 0000000..0fddb24 --- /dev/null +++ b/src/core/curl_exception.h @@ -0,0 +1,41 @@ +/***************************************************************************** +* dtranslatebot Discord Translate Bot +* Copyright (C) 2026 Syping +* +* Redistribution and use in source and binary forms, with or without modification, +* are permitted provided that the following conditions are met: +* +* 1. Redistributions of source code must retain the above copyright notice, +* this list of conditions and the following disclaimer. +* +* 2. Redistributions in binary form must reproduce the above copyright notice, +* this list of conditions and the following disclaimer in the documentation +* and/or other materials provided with the distribution. +* +* This software is provided as-is, no warranties are given to you, we are not +* responsible for anything with use of the software, you are self responsible. +*****************************************************************************/ + +#ifndef CURL_EXCEPTION_H +#define CURL_EXCEPTION_H + +#include +#include +#include + +namespace bot { + namespace exception { + class curl_exception : public std::exception { + public: + explicit curl_exception(const std::string &message, CURLcode error_code); + virtual ~curl_exception() noexcept; + const char* what() const noexcept override; + + private: + const std::string m_message; + const CURLcode m_error_code; + }; + } +}; + +#endif // CURL_EXCEPTION_H