stdex/include/stdex/exception.hpp
Simon Rozman be273d4263 exception: Add exception_msg helper
Signed-off-by: Simon Rozman <simon@rozman.si>
2025-05-13 09:36:22 +02:00

44 lines
862 B
C++

/*
SPDX-License-Identifier: MIT
Copyright © 2023-2025 Amebis
*/
#pragma once
#include "compat.hpp"
#include <stdexcept>
namespace stdex
{
///
/// User cancelled exception
///
class user_cancelled : public std::runtime_error
{
public:
///
/// Constructs an exception
///
/// \param[in] msg Error message
///
user_cancelled(_In_opt_z_ const char* msg = "operation cancelled") : runtime_error(msg) {}
};
///
/// Concatenates error messages when exception is nested.
///
/// \param ex Exception
///
/// \returns Concatenated exception messages with ": " delimiter.
///
inline std::string exception_msg(const std::exception& ex)
{
try { std::rethrow_if_nested(ex); }
catch (const std::exception& nested_ex) {
return std::string(ex.what()) + ": " + exception_msg(nested_ex);
}
catch (...) {}
return ex.what();
}
}