From c8db6db855002decd9f6d4bf567be36b52aed367 Mon Sep 17 00:00:00 2001 From: Eyal Rozenberg Date: Wed, 3 May 2023 22:21:40 +0300 Subject: [PATCH] Fixes #511: Can now construct errors with fully-overriden `what()` message --- src/cuda/api/error.hpp | 11 +++++++++++ src/cuda/rtc/error.hpp | 16 ++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/cuda/api/error.hpp b/src/cuda/api/error.hpp index f86703dc..20c9c0cc 100644 --- a/src/cuda/api/error.hpp +++ b/src/cuda/api/error.hpp @@ -290,6 +290,17 @@ class runtime_error : public ::std::runtime_error { runtime_error(status::named_t error_code, ::std::string&& what_arg) : runtime_error(static_cast(error_code), what_arg) { } +protected: + runtime_error(status_t error_code, ::std::runtime_error&& err) : + ::std::runtime_error(::std::move(err)), code_(error_code) + { } + +public: + static runtime_error with_message_override(status_t error_code, ::std::string complete_what_arg) + { + return runtime_error(error_code, ::std::runtime_error(::std::move(complete_what_arg))); + } + /** * Obtain the CUDA status code which resulted in this error being thrown. */ diff --git a/src/cuda/rtc/error.hpp b/src/cuda/rtc/error.hpp index d2225604..daf7e16c 100644 --- a/src/cuda/rtc/error.hpp +++ b/src/cuda/rtc/error.hpp @@ -128,8 +128,8 @@ class runtime_error : public ::std::runtime_error { code_(error_code) { } // I wonder if I should do this the other way around - runtime_error(status_t error_code, const ::std::string& what_arg) : - ::std::runtime_error(what_arg + ": " + describe(error_code)), + runtime_error(status_t error_code, ::std::string what_arg) : + ::std::runtime_error(::std::move(what_arg) + ": " + describe(error_code)), code_(error_code) { } ///@endcond @@ -138,6 +138,17 @@ class runtime_error : public ::std::runtime_error { runtime_error(status::named_t error_code, const ::std::string& what_arg) : runtime_error(static_cast>(error_code), what_arg) { } +protected: + runtime_error(status_t error_code, ::std::runtime_error err) : + ::std::runtime_error(::std::move(err)), code_(error_code) + { } + +public: + static runtime_error with_message_override(status_t error_code, ::std::string complete_what_arg) + { + return runtime_error(error_code, ::std::runtime_error(complete_what_arg)); + } + /** * Obtain the CUDA status code which resulted in this error being thrown. */ @@ -147,6 +158,7 @@ class runtime_error : public ::std::runtime_error { status_t code_; }; + } // namespace rtc // TODO: The following could use ::std::optional arguments - which would