Compute Graph Framework SDK Reference  5.8
Exception.hpp
Go to the documentation of this file.
1
2//
3// Notice
4// ALL NVIDIA DESIGN SPECIFICATIONS AND CODE ("MATERIALS") ARE PROVIDED "AS IS" NVIDIA MAKES
5// NO REPRESENTATIONS, WARRANTIES, EXPRESSED, IMPLIED, STATUTORY, OR OTHERWISE WITH RESPECT TO
6// THE MATERIALS, AND EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTIES OF NONINFRINGEMENT,
7// MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8//
9// NVIDIA CORPORATION & AFFILIATES assumes no responsibility for the consequences of use of such
10// information or for any infringement of patents or other rights of third parties that may
11// result from its use. No license is granted by implication or otherwise under any patent
12// or patent rights of NVIDIA CORPORATION & AFFILIATES. No third party distribution is allowed unless
13// expressly authorized by NVIDIA. Details are subject to change without notice.
14// This code supersedes and replaces all information previously supplied.
15// NVIDIA CORPORATION & AFFILIATES products are not authorized for use as critical
16// components in life support devices or systems without express written approval of
17// NVIDIA CORPORATION & AFFILIATES.
18//
19// SPDX-FileCopyrightText: Copyright (c) 2019-2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
20// SPDX-License-Identifier: LicenseRef-NvidiaProprietary
21//
22// NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
23// property and proprietary rights in and to this material, related
24// documentation and any modifications thereto. Any use, reproduction,
25// disclosure or distribution of this material and related documentation
26// without an express license agreement from NVIDIA CORPORATION or
27// its affiliates is strictly prohibited.
28//
30
31#ifndef DW_FRAMEWORK_EXCEPTION_HPP_
32#define DW_FRAMEWORK_EXCEPTION_HPP_
33
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>
39
40#define THROW_ON_PARAM_NULL(param) \
41 if (param == nullptr) \
42 { \
43 throw dw::framework::Exception(DW_INVALID_ARGUMENT, #param " == nullptr ", DW_FUNCTION_NAME, ":", __LINE__); \
44 }
45
46namespace dw
47{
48namespace framework
49{
50
52
53class Exception : public dw::core::ExceptionBase
54{
55public:
56 static constexpr char8_t LOG_TAG[] = "Exception";
57
58 Exception(dwStatus statusCode, const char8_t* messageStr)
59 : dw::core::ExceptionBase(dwGetStatusName(statusCode))
60 , m_statusCode(statusCode)
61 , m_messageBegin(0)
62 {
63 m_what += ": ";
64 m_messageBegin = m_what.length();
65 m_what += messageStr;
66 }
67
68 ~Exception() noexcept override = default;
69
71 // These templated constructors allow direct construction of the message
72 template <class... Tothers>
73 explicit Exception(dwStatus statusCode, const char8_t* messageStr, Tothers... others)
74 : Exception(statusCode, messageStr)
75 {
76 (void)std::initializer_list<int32_t>{(static_cast<void>(m_what << others), 0)...};
77 }
78
79 dwStatus status() const
80 {
81 return m_statusCode;
82 }
83
84 char8_t const* messageStr() const noexcept
85 {
86 return what() + m_messageBegin;
87 }
88
101 template <typename TryBlock>
102 static dwStatus guardWithReturn(TryBlock tryBlock, dw::core::Logger::Verbosity verbosity = dw::core::Logger::Verbosity::DEBUG);
103
108 template <typename TryBlock>
109 static dwStatus guard(TryBlock tryBlock, dw::core::Logger::Verbosity verbosity = dw::core::Logger::Verbosity::DEBUG);
110
111 template <typename TryBlock>
112 static dwStatus guardWithNoPrint(TryBlock tryBlock);
113
114private:
115 dwStatus m_statusCode;
116 size_t m_messageBegin;
117};
118
120// Cannot compile with NVCC because of the generic lambda
121template <typename TryBlock>
122dwStatus Exception::guardWithReturn(TryBlock tryBlock, dw::core::Logger::Verbosity verbosity)
123{
124 using FixedString = dw::core::BaseString<40>;
125
126 // logging exception
127 auto const logException = [verbosity](const dwStatus status, const auto& ex, FixedString errorMessage) -> dwStatus {
128
129 // Disabling rule A5-1-1 here as literals are allowed for logging
130 // coverity[autosar_cpp14_a5_1_1_violation]
131 DW_LOG(verbosity) << errorMessage
132 << dwGetStatusName(status)
133 << ", "
134 << ex.what()
135 << Logger::State::endl
136 << ex.stackTrace()
137 << Logger::State::endl;
138
139 return status;
140 };
141
142 try
143 {
144 return tryBlock();
145 }
146 catch (const Exception& ex)
147 {
148 // TODO(lindax): Make this message a warning after RR1.0 fix all their
149 // channel problem
150
151 DW_LOG(verbosity) << "Framework exception thrown: "
152 << dwGetStatusName(ex.status())
153 << ", "
154 << ex.what()
155 << Logger::State::endl;
156 return ex.status();
157 }
158 catch (const BufferFullException& ex)
159 {
160 // Disabling rule A4-5-1 here. Coverity confuses 'status' used as parameter in lambda function with using enum as operand in overloaded "operator ()"
161 // coverity[autosar_cpp14_a4_5_1_violation]
162
163 return logException(DW_BUFFER_FULL, ex, FixedString("Framework exception thrown: "));
164 }
165 catch (const BufferEmptyException& ex)
166 {
167 // Disabling rule A4-5-1 here. Coverity confuses 'status' used as parameter in lambda function with using enum as operand in overloaded "operator ()"
168 // coverity[autosar_cpp14_a4_5_1_violation]
169 return logException(DW_NOT_AVAILABLE, ex, FixedString("Framework exception thrown: "));
170 }
171 catch (const OutOfBoundsException& ex)
172 {
173 // Disabling rule A4-5-1 here. Coverity confuses 'status' used as parameter in lambda function with using enum as operand in overloaded "operator ()"
174 // coverity[autosar_cpp14_a4_5_1_violation]
175 return logException(DW_OUT_OF_BOUNDS, ex, FixedString("Framework exception thrown: "));
176 }
177 catch (const InvalidArgumentException& ex)
178 {
179 // Disabling rule A4-5-1 here. Coverity confuses 'status' used as parameter in lambda function with using enum as operand in overloaded "operator ()"
180 // coverity[autosar_cpp14_a4_5_1_violation]
181 return logException(DW_INVALID_ARGUMENT, ex, FixedString("Framework exception thrown: "));
182 }
183 catch (const BadAlignmentException& ex)
184 {
185 // Disabling rule A4-5-1 here. Coverity confuses 'status' used as parameter in lambda function with using enum as operand in overloaded "operator ()"
186 // coverity[autosar_cpp14_a4_5_1_violation]
187 return logException(DW_BAD_ALIGNMENT, ex, FixedString("Framework exception thrown: "));
188 }
189 catch (const CudaException& ex)
190 {
191 // Disabling rule A4-5-1 here. Coverity confuses 'status' used as parameter in lambda function with using enum as operand in overloaded "operator ()"
192 // coverity[autosar_cpp14_a4_5_1_violation]
193 return logException(DW_CUDA_ERROR, ex, FixedString("Framework exception thrown: "));
194 }
195 catch (const ExceptionWithStackTrace& ex)
196 {
197 // Disabling rule A4-5-1 here. Coverity confuses 'status' used as parameter in lambda function with using enum as operand in overloaded "operator ()"
198 // coverity[autosar_cpp14_a4_5_1_violation]
199 return logException(DW_FAILURE, ex, FixedString("Framework exception thrown: "));
200 }
201 catch (const std::exception& ex)
202 {
203 // Disabling rule A4-5-1 here. Coverity confuses 'status' used as parameter in lambda function with using enum as operand in overloaded "operator ()"
204 // coverity[autosar_cpp14_a4_5_1_violation]
205
206 DW_LOG(verbosity) << "std::exception thrown: "
207 << dwGetStatusName(DW_FAILURE)
208 << ", "
209 << ex.what()
210 << Logger::State::endl;
211 return DW_FAILURE;
212 }
213 catch (...)
214 {
215 // Disabling rule A4-5-1 here. Coverity confuses 'status' used as parameter in lambda function with using enum as operand in overloaded "operator ()"
216 // coverity[autosar_cpp14_a4_5_1_violation]
217
218 DW_LOG(verbosity) << "Unknown exception thrown, "
219 << dwGetStatusName(DW_FAILURE)
220 << Logger::State::endl;
221
222 return DW_FAILURE;
223 }
224}
225
227template <typename TryBlock>
228dwStatus Exception::guard(TryBlock tryBlock, dw::core::Logger::Verbosity verbosity)
229{
230 static_assert(std::is_same<void, typename std::result_of<TryBlock()>::type>::value,
231 "tryBlock must return void");
232
233 return guardWithReturn([&] {
234 tryBlock();
235 return DW_SUCCESS;
236 },
237 verbosity);
238}
239
241template <typename TryBlock>
242dwStatus Exception::guardWithNoPrint(TryBlock tryBlock)
243{
244 try
245 {
246 tryBlock();
247 return DW_SUCCESS;
248 }
249 catch (const Exception& ex)
250 {
251 return ex.status();
252 }
253}
254
255const char* nvSciGetEventName(uint32_t event);
256const char* nvSciGetErrorName(uint32_t error);
257
258} // namespace framework
259} // namespace dw
260
261//------------------------------------------------------------------------------
262// macro to easily check for dw errors
263#define FRWK_CHECK_DW_ERROR(x) \
264 { \
265 dwStatus result = x; \
266 if (result != DW_SUCCESS) \
267 { \
268 throw dw::framework::Exception(result, __FILE__, ":", __LINE__, " - DriveWorks Error"); \
269 } \
270 };
271#define GET_STRING(s) #s
272#define FRWK_CHECK_DW_ERROR_IGNORE_SOME(x, fallback, ...) \
273 { \
274 dwStatus result = x; \
275 dwStatus ignoreErros[] = {__VA_ARGS__}; \
276 if (result != DW_SUCCESS) \
277 { \
278 if (std::find(std::begin(ignoreErros), std::end(ignoreErros), result) != std::end(ignoreErros)) \
279 { \
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; \
285 result = fallback; \
286 if (result != DW_SUCCESS) \
287 { \
288 throw dw::framework::Exception(result, "After ignoring errors from ignore list, fallback operation %s encountered DriveWorks error.", GET_STRING(fallback)); \
289 } \
290 } \
291 } \
292 if (result != DW_SUCCESS) \
293 { \
294 throw dw::framework::Exception(result, "DriveWorks error not in ignore list."); \
295 } \
296 };
297
298#define FRWK_CHECK_DW_ERROR_NOTHROW(x) \
299 { \
300 dwStatus result = x; \
301 if (result != DW_SUCCESS) \
302 { \
303 DW_LOGE << __FILE__ \
304 << "(" << __LINE__ << ") " \
305 << "DriveWorks exception but not thrown: " \
306 << dwGetStatusName(result) \
307 << dw::core::Logger::State::endl; \
308 } \
309 };
310
311#define FRWK_CHECK_DW_ERROR_NOTHROW_IGNORE_SOME(x, fallback, ...) \
312 { \
313 dwStatus result = x; \
314 dwStatus ignoreErros[] = {__VA_ARGS__}; \
315 if (std::find(std::begin(ignoreErros), std::end(ignoreErros), result) != std::end(ignoreErros)) \
316 { \
317 result = fallback; \
318 } \
319 if (result != DW_SUCCESS) \
320 { \
321 DW_LOGE << __FILE__ \
322 << "(" << __LINE__ << ") " \
323 << "DriveWorks exception but not thrown: " \
324 << dwGetStatusName(result) \
325 << dw::core::Logger::State::endl; \
326 } \
327 };
328
329#define FRWK_CHECK_DW_ERROR_MSG(x, description) \
330 { \
331 dwStatus result = (x); \
332 if (result != DW_SUCCESS) \
333 { \
334 throw dw::framework::Exception(result, (description)); \
335 } \
336 };
337
338//------------------------------------------------------------------------------
339// macro to easily check for cuda errors
340#define FRWK_CHECK_CUDA_ERROR(x) \
341 { \
342 x; \
343 auto result = cudaGetLastError(); \
344 if (result != cudaSuccess) \
345 { \
346 throw dw::framework::Exception(DW_CUDA_ERROR, cudaGetErrorString(result)); \
347 } \
348 };
349
350#define FRWK_CHECK_CUDA_ERROR_NOTHROW(x) \
351 { \
352 x; \
353 auto result = cudaGetLastError(); \
354 if (result != cudaSuccess) \
355 { \
356 DW_LOGE << __FILE__ \
357 << "(" << __LINE__ << ") " \
358 << "CUDA error but not thrown: " \
359 << cudaGetErrorString(result) \
360 << dw::core::Logger::State::endl; \
361 } \
362 };
363
364#define FRWK_CHECK_NVMEDIA_ERROR(e) \
365 { \
366 auto FRWK_CHECK_NVMEDIA_ERROR_ret = (e); \
367 if (FRWK_CHECK_NVMEDIA_ERROR_ret != NVMEDIA_STATUS_OK) \
368 { \
369 throw dw::framework::Exception(DW_NVMEDIA_ERROR, "NvMedia error occured"); \
370 } \
371 }
372
373#define FRWK_CHECK_NVSCI_ERROR(e) \
374 { \
375 auto FRWK_CHECK_NVSCI_ERROR_ret = (e); \
376 if (FRWK_CHECK_NVSCI_ERROR_ret != NvSciError_Success) \
377 { \
378 DW_LOGE << "Failed with " << nvSciGetErrorName(FRWK_CHECK_NVSCI_ERROR_ret) \
379 << "(" << FRWK_CHECK_NVSCI_ERROR_ret << ")" \
380 << " in " << __FILE__ \
381 << ":" << __LINE__ << Logger::State::endl; \
382 if (FRWK_CHECK_NVSCI_ERROR_ret == NvSciError_Timeout) \
383 throw Exception(DW_TIME_OUT, "NvSci API Timeout"); \
384 else \
385 throw Exception(DW_INTERNAL_ERROR, "NvSci internal error occured"); \
386 } \
387 }
388
389#endif // DW_FRAMEWORK_TYPES_HPP_
static dwStatus guard(TryBlock tryBlock, dw::core::Logger::Verbosity verbosity=dw::core::Logger::Verbosity::DEBUG)
Definition: Exception.hpp:228
static dwStatus guardWithReturn(TryBlock tryBlock, dw::core::Logger::Verbosity verbosity=dw::core::Logger::Verbosity::DEBUG)
Definition: Exception.hpp:122
static constexpr char8_t LOG_TAG[]
Definition: Exception.hpp:56
char8_t const * messageStr() const noexcept
Definition: Exception.hpp:84
Exception(dwStatus statusCode, const char8_t *messageStr)
Definition: Exception.hpp:58
static dwStatus guardWithNoPrint(TryBlock tryBlock)
Definition: Exception.hpp:242
~Exception() noexcept override=default
dwStatus status() const
Definition: Exception.hpp:79
const char * nvSciGetEventName(uint32_t event)
const char * nvSciGetErrorName(uint32_t error)
Definition: Exception.hpp:47