31#ifndef DW_FRAMEWORK_EXCEPTION_HPP_
32#define DW_FRAMEWORK_EXCEPTION_HPP_
35#include <dw/core/base/ExceptionWithStackTrace.hpp>
36#include <dw/core/base/Status.h>
37#include <dw/core/container/BaseString.hpp>
38#include <nvscierror.h>
40#define THROW_ON_PARAM_NULL(param) \
41 if (param == nullptr) \
43 throw dw::framework::Exception(DW_INVALID_ARGUMENT, #param " == nullptr ", DW_FUNCTION_NAME, ":", __LINE__); \
56 static constexpr char8_t LOG_TAG[] =
"Exception";
59 :
dw::core::ExceptionBase(dwGetStatusName(statusCode))
60 , m_statusCode(statusCode)
64 m_messageBegin = m_what.length();
72 template <class... Tothers>
76 (void)std::initializer_list<int32_t>{(
static_cast<void>(m_what << others), 0)...};
86 return what() + m_messageBegin;
101 template <
typename TryBlock>
102 static dwStatus
guardWithReturn(TryBlock tryBlock, dw::core::Logger::Verbosity verbosity = dw::core::Logger::Verbosity::DEBUG);
108 template <
typename TryBlock>
109 static dwStatus
guard(TryBlock tryBlock, dw::core::Logger::Verbosity verbosity = dw::core::Logger::Verbosity::DEBUG);
111 template <
typename TryBlock>
115 dwStatus m_statusCode;
116 size_t m_messageBegin;
121template <
typename TryBlock>
124 using FixedString = dw::core::BaseString<40>;
127 auto const logException = [verbosity](
const dwStatus
status,
const auto& ex, FixedString errorMessage) -> dwStatus {
131 DW_LOG(verbosity) << errorMessage
132 << dwGetStatusName(
status)
135 << Logger::State::endl
137 << Logger::State::endl;
151 DW_LOG(verbosity) <<
"Framework exception thrown: "
152 << dwGetStatusName(ex.status())
155 << Logger::State::endl;
158 catch (
const BufferFullException& ex)
163 return logException(DW_BUFFER_FULL, ex, FixedString(
"Framework exception thrown: "));
165 catch (
const BufferEmptyException& ex)
169 return logException(DW_NOT_AVAILABLE, ex, FixedString(
"Framework exception thrown: "));
171 catch (
const OutOfBoundsException& ex)
175 return logException(DW_OUT_OF_BOUNDS, ex, FixedString(
"Framework exception thrown: "));
177 catch (
const InvalidArgumentException& ex)
181 return logException(DW_INVALID_ARGUMENT, ex, FixedString(
"Framework exception thrown: "));
183 catch (
const BadAlignmentException& ex)
187 return logException(DW_BAD_ALIGNMENT, ex, FixedString(
"Framework exception thrown: "));
189 catch (
const CudaException& ex)
193 return logException(DW_CUDA_ERROR, ex, FixedString(
"Framework exception thrown: "));
195 catch (
const ExceptionWithStackTrace& ex)
199 return logException(DW_FAILURE, ex, FixedString(
"Framework exception thrown: "));
201 catch (
const std::exception& ex)
206 DW_LOG(verbosity) <<
"std::exception thrown: "
207 << dwGetStatusName(DW_FAILURE)
210 << Logger::State::endl;
218 DW_LOG(verbosity) <<
"Unknown exception thrown, "
219 << dwGetStatusName(DW_FAILURE)
220 << Logger::State::endl;
227template <
typename TryBlock>
230 static_assert(std::is_same<void,
typename std::result_of<TryBlock()>::type>::value,
231 "tryBlock must return void");
241template <
typename TryBlock>
263#define FRWK_CHECK_DW_ERROR(x) \
265 dwStatus result = x; \
266 if (result != DW_SUCCESS) \
268 throw dw::framework::Exception(result, __FILE__, ":", __LINE__, " - DriveWorks Error"); \
271#define GET_STRING(s) #s
272#define FRWK_CHECK_DW_ERROR_IGNORE_SOME(x, fallback, ...) \
274 dwStatus result = x; \
275 dwStatus ignoreErros[] = {__VA_ARGS__}; \
276 if (result != DW_SUCCESS) \
278 if (std::find(std::begin(ignoreErros), std::end(ignoreErros), result) != std::end(ignoreErros)) \
280 DW_LOGD << __FILE__ \
281 << "(" << __LINE__ << ") " \
282 << "Ignoring Error: " \
283 << dwGetStatusName(result) << ". Falling back on calling " << GET_STRING(fallback) \
284 << dw::core::Logger::State::endl; \
286 if (result != DW_SUCCESS) \
288 throw dw::framework::Exception(result, "After ignoring errors from ignore list, fallback operation %s encountered DriveWorks error.", GET_STRING(fallback)); \
292 if (result != DW_SUCCESS) \
294 throw dw::framework::Exception(result, "DriveWorks error not in ignore list."); \
298#define FRWK_CHECK_DW_ERROR_NOTHROW(x) \
300 dwStatus result = x; \
301 if (result != DW_SUCCESS) \
303 DW_LOGE << __FILE__ \
304 << "(" << __LINE__ << ") " \
305 << "DriveWorks exception but not thrown: " \
306 << dwGetStatusName(result) \
307 << dw::core::Logger::State::endl; \
311#define FRWK_CHECK_DW_ERROR_MSG(x, description) \
313 dwStatus result = (x); \
314 if (result != DW_SUCCESS) \
316 throw dw::framework::Exception(result, (description)); \
322#define FRWK_CHECK_CUDA_ERROR(x) \
325 auto result = cudaGetLastError(); \
326 if (result != cudaSuccess) \
328 throw dw::framework::Exception(DW_CUDA_ERROR, cudaGetErrorString(result)); \
332#define FRWK_CHECK_CUDA_ERROR_NOTHROW(x) \
335 auto result = cudaGetLastError(); \
336 if (result != cudaSuccess) \
338 DW_LOGE << __FILE__ \
339 << "(" << __LINE__ << ") " \
340 << "CUDA error but not thrown: " \
341 << cudaGetErrorString(result) \
342 << dw::core::Logger::State::endl; \
346#define FRWK_CHECK_NVMEDIA_ERROR(e) \
348 auto FRWK_CHECK_NVMEDIA_ERROR_ret = (e); \
349 if (FRWK_CHECK_NVMEDIA_ERROR_ret != NVMEDIA_STATUS_OK) \
351 throw dw::framework::Exception(DW_NVMEDIA_ERROR, "NvMedia error occured"); \
355#define FRWK_CHECK_NVSCI_ERROR(e) \
357 auto FRWK_CHECK_NVSCI_ERROR_ret = (e); \
358 if (FRWK_CHECK_NVSCI_ERROR_ret != NvSciError_Success) \
360 DW_LOGE << "Failed with " << nvSciGetErrorName(FRWK_CHECK_NVSCI_ERROR_ret) \
361 << "(" << FRWK_CHECK_NVSCI_ERROR_ret << ")" \
362 << " in " << __FILE__ \
363 << ":" << __LINE__ << Logger::State::endl; \
364 if (FRWK_CHECK_NVSCI_ERROR_ret == NvSciError_Timeout) \
365 throw Exception(DW_TIME_OUT, "NvSci API Timeout"); \
367 throw Exception(DW_INTERNAL_ERROR, "NvSci internal error occured"); \
static dwStatus guard(TryBlock tryBlock, dw::core::Logger::Verbosity verbosity=dw::core::Logger::Verbosity::DEBUG)
static dwStatus guardWithReturn(TryBlock tryBlock, dw::core::Logger::Verbosity verbosity=dw::core::Logger::Verbosity::DEBUG)
static constexpr char8_t LOG_TAG[]
char8_t const * messageStr() const noexcept
Exception(dwStatus statusCode, const char8_t *messageStr)
static dwStatus guardWithNoPrint(TryBlock tryBlock)
~Exception() noexcept override=default
const char * nvSciGetEventName(uint32_t event)
const char * nvSciGetErrorName(uint32_t error)