Compute Graph Framework SDK Reference  5.12
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-2023 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
34#include <dwshared/dwfoundation/dw/core/base/ExceptionWithStatus.hpp>
35#include <dw/core/base/Status.h>
36#include <dwshared/dwfoundation/dw/core/container/BaseString.hpp>
37#include <dwshared/dwfoundation/dw/core/language/Function.hpp>
38#include <dwshared/dwfoundation/dw/core/logger/Logger.hpp>
39
40#define THROW_ON_PARAM_NULL(param) \
41 if (param == nullptr) \
42 { \
43 throw dw::core::ExceptionWithStatus(DW_INVALID_ARGUMENT, #param " == nullptr ", DW_FUNCTION_NAME, ":", __LINE__); \
44 }
45
46//------------------------------------------------------------------------------
47// macro to easily check for dw errors
48#define FRWK_CHECK_DW_ERROR(x) \
49 { \
50 dwStatus result{}; \
51 result = (x); \
52 if (result != DW_SUCCESS) \
53 { \
54 throw dw::core::ExceptionWithStatus(result, __FILE__, ":", __LINE__, " - DriveWorks Error"); \
55 } \
56 };
57#define GET_STRING(s) #s
58#define FRWK_CHECK_DW_ERROR_IGNORE_SOME(x, fallback, ...) \
59 { \
60 dwStatus result = x; \
61 dwStatus ignoreErros[] = {__VA_ARGS__}; \
62 if (result != DW_SUCCESS) \
63 { \
64 if (std::find(std::begin(ignoreErros), std::end(ignoreErros), result) != std::end(ignoreErros)) \
65 { \
66 DW_LOGD << __FILE__ \
67 << "(" << __LINE__ << ") " \
68 << "Ignoring Error: " \
69 << dwGetStatusName(result) << ". Falling back on calling " << GET_STRING(fallback) \
70 << dw::core::Logger::State::endl; \
71 result = fallback; \
72 if (result != DW_SUCCESS) \
73 { \
74 throw dw::core::ExceptionWithStatus(result, "After ignoring errors from ignore list, fallback operation %s encountered DriveWorks error.", GET_STRING(fallback)); \
75 } \
76 } \
77 } \
78 if (result != DW_SUCCESS) \
79 { \
80 throw dw::core::ExceptionWithStatus(result, "DriveWorks error not in ignore list."); \
81 } \
82 };
83
84#define FRWK_CHECK_DW_ERROR_NOTHROW(x) \
85 { \
86 dwStatus result{x}; \
87 if (result != DW_SUCCESS) \
88 { \
89 DW_LOGE << __FILE__ \
90 << "(" << __LINE__ << ") " \
91 << "DriveWorks exception but not thrown: " \
92 << dwGetStatusName(result) \
93 << dw::core::Logger::State::endl; \
94 } \
95 };
96
97#define FRWK_CHECK_DW_ERROR_NOTHROW_IGNORE_SOME(x, fallback, ...) \
98 { \
99 dwStatus result = x; \
100 dwStatus ignoreErros[] = {__VA_ARGS__}; \
101 if (std::find(std::begin(ignoreErros), std::end(ignoreErros), result) != std::end(ignoreErros)) \
102 { \
103 result = fallback; \
104 } \
105 if (result != DW_SUCCESS) \
106 { \
107 DW_LOGE << __FILE__ \
108 << "(" << __LINE__ << ") " \
109 << "DriveWorks exception but not thrown: " \
110 << dwGetStatusName(result) \
111 << dw::core::Logger::State::endl; \
112 } \
113 };
114
115#define FRWK_CHECK_DW_ERROR_MSG(x, description) \
116 { \
117 dwStatus result{x}; \
118 if (result != DW_SUCCESS) \
119 { \
120 throw dw::core::ExceptionWithStatus(result, (description)); \
121 } \
122 };
123
124//------------------------------------------------------------------------------
125// macro to easily check for cuda errors
126#define FRWK_CHECK_CUDA_ERROR(x) \
127 { \
128 x; \
129 cudaError_t result{cudaGetLastError()}; \
130 if (result != cudaSuccess) \
131 { \
132 throw dw::core::ExceptionWithStatus(DW_CUDA_ERROR, cudaGetErrorString(result)); \
133 } \
134 };
135
136#define FRWK_CHECK_CUDA_ERROR_NOTHROW(x) \
137 { \
138 x; \
139 cudaError_t result{cudaGetLastError()}; \
140 if (result != cudaSuccess) \
141 { \
142 DW_LOGE << __FILE__ \
143 << "(" << __LINE__ << ") " \
144 << "CUDA error but not thrown: " \
145 << cudaGetErrorString(result) \
146 << dw::core::Logger::State::endl; \
147 } \
148 };
149
150#define FRWK_CHECK_NVMEDIA_ERROR(e) \
151 { \
152 NvMediaStatus FRWK_CHECK_NVMEDIA_ERROR_ret{e}; \
153 if (FRWK_CHECK_NVMEDIA_ERROR_ret != NVMEDIA_STATUS_OK) \
154 { \
155 throw dw::core::ExceptionWithStatus(DW_NVMEDIA_ERROR, "NvMedia error occured"); \
156 } \
157 }
158
159namespace dw
160{
161namespace framework
162{
163
164// coverity[autosar_cpp14_a0_1_6_violation]
166{
167 ExceptionGuard() = delete;
168
169public:
170 template <typename TryBlock>
171 static dwStatus guardWithReturn(TryBlock const& tryBlock, ::dw::core::Logger::Verbosity verbosity = ::dw::core::Logger::Verbosity::ERROR)
172 {
173 return guardWithReturnFunction(tryBlock, verbosity);
174 }
175
176 template <typename TryBlock>
177 static dwStatus guard(TryBlock const& tryBlock, ::dw::core::Logger::Verbosity verbosity = ::dw::core::Logger::Verbosity::ERROR)
178 {
179 static_assert(std::is_same<void, typename std::result_of<TryBlock()>::type>::value,
180 "tryBlock must return void");
181 dw::core::Function<dwStatus()> tryBlockFunc{[&]() -> dwStatus {
182 tryBlock();
183 return DW_SUCCESS;
184 }};
185 return guardWithReturnFunction(tryBlockFunc, verbosity);
186 }
187
188private:
189 static dwStatus guardWithReturnFunction(dw::core::Function<dwStatus()> const& tryBlock, dw::core::Logger::Verbosity verbosity);
190};
191
192} // namespace framework
193} // namespace dw
194
195#endif // DW_FRAMEWORK_EXCEPTION_HPP_
static dwStatus guardWithReturn(TryBlock const &tryBlock, ::dw::core::Logger::Verbosity verbosity=::dw::core::Logger::Verbosity::ERROR)
Definition: Exception.hpp:171
static dwStatus guard(TryBlock const &tryBlock, ::dw::core::Logger::Verbosity verbosity=::dw::core::Logger::Verbosity::ERROR)
Definition: Exception.hpp:177
Definition: Buffer.hpp:40