• <xmp id="om0om">
  • <table id="om0om"><noscript id="om0om"></noscript></table>
  • Compute Graph Framework SDK Reference  5.6
    All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
    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_MSG(x, description) \
    312 { \
    313 dwStatus result = (x); \
    314 if (result != DW_SUCCESS) \
    315 { \
    316 throw dw::framework::Exception(result, (description)); \
    317 } \
    318 };
    319
    320//------------------------------------------------------------------------------
    321// macro to easily check for cuda errors
    322#define FRWK_CHECK_CUDA_ERROR(x) \
    323 { \
    324 x; \
    325 auto result = cudaGetLastError(); \
    326 if (result != cudaSuccess) \
    327 { \
    328 throw dw::framework::Exception(DW_CUDA_ERROR, cudaGetErrorString(result)); \
    329 } \
    330 };
    331
    332#define FRWK_CHECK_CUDA_ERROR_NOTHROW(x) \
    333 { \
    334 x; \
    335 auto result = cudaGetLastError(); \
    336 if (result != cudaSuccess) \
    337 { \
    338 DW_LOGE << __FILE__ \
    339 << "(" << __LINE__ << ") " \
    340 << "CUDA error but not thrown: " \
    341 << cudaGetErrorString(result) \
    342 << dw::core::Logger::State::endl; \
    343 } \
    344 };
    345
    346#define FRWK_CHECK_NVMEDIA_ERROR(e) \
    347 { \
    348 auto FRWK_CHECK_NVMEDIA_ERROR_ret = (e); \
    349 if (FRWK_CHECK_NVMEDIA_ERROR_ret != NVMEDIA_STATUS_OK) \
    350 { \
    351 throw dw::framework::Exception(DW_NVMEDIA_ERROR, "NvMedia error occured"); \
    352 } \
    353 }
    354
    355#define FRWK_CHECK_NVSCI_ERROR(e) \
    356 { \
    357 auto FRWK_CHECK_NVSCI_ERROR_ret = (e); \
    358 if (FRWK_CHECK_NVSCI_ERROR_ret != NvSciError_Success) \
    359 { \
    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"); \
    366 else \
    367 throw Exception(DW_INTERNAL_ERROR, "NvSci internal error occured"); \
    368 } \
    369 }
    370
    371#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
    人人超碰97caoporen国产