microsoft/onnxruntime-extensions
Publicmirrored fromhttps://github.com/microsoft/onnxruntime-extensionsAvailable
includes/exceptions.h
99lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | #pragma once |
| 5 | |
| 6 | #if defined(OCOS_NO_EXCEPTIONS) || defined(OCOS_PREVENT_EXCEPTION_PROPAGATION) |
| 7 | #if defined(__ANDROID__) |
| 8 | #include <android/log.h> |
| 9 | #else |
| 10 | #include <iostream> |
| 11 | #endif |
| 12 | #endif |
| 13 | |
| 14 | #include <stdexcept> |
| 15 | |
| 16 | #include "onnxruntime_c_api.h" |
| 17 | |
| 18 | namespace OrtW { |
| 19 | |
| 20 | // All C++ methods that can fail will throw an exception of this type |
| 21 | struct Exception : std::exception { |
| 22 | Exception(std::string message, OrtErrorCode code) : message_{std::move(message)}, code_{code} {} |
| 23 | |
| 24 | OrtErrorCode GetOrtErrorCode() const { return code_; } |
| 25 | const char* what() const noexcept override { return message_.c_str(); } |
| 26 | |
| 27 | private: |
| 28 | std::string message_; |
| 29 | OrtErrorCode code_; |
| 30 | }; |
| 31 | |
| 32 | #if defined(OCOS_NO_EXCEPTIONS) || defined(OCOS_PREVENT_EXCEPTION_PROPAGATION) |
| 33 | inline void PrintFinalMessage(const char* file, int line, const char* msg) { |
| 34 | #if defined(__ANDROID__) |
| 35 | __android_log_print(ANDROID_LOG_ERROR, "onnxruntime-extensions", "Exception in %s line %d: %s", file, line, msg); |
| 36 | #else |
| 37 | std::cerr << "Exception in " << file << " line " << line << ": " << msg << std::endl; |
| 38 | #endif |
| 39 | } |
| 40 | #endif |
| 41 | |
| 42 | #ifdef OCOS_NO_EXCEPTIONS |
| 43 | #define ORTX_CXX_API_THROW(string, code) \ |
| 44 | do { \ |
| 45 | OrtW::PrintFinalMessage(__FILE__, __LINE__, OrtW::Exception(string, code).what()); \ |
| 46 | abort(); \ |
| 47 | } while (false) |
| 48 | |
| 49 | #define OCOS_TRY if (true) |
| 50 | #define OCOS_CATCH(x) else if (false) |
| 51 | #define OCOS_RETHROW |
| 52 | // In order to ignore the catch statement when a specific exception (not ... ) is caught and referred |
| 53 | // in the body of the catch statements, it is necessary to wrap the body of the catch statement into |
| 54 | // a lambda function. otherwise the exception referred will be undefined and cause build break |
| 55 | #define OCOS_HANDLE_EXCEPTION(func) |
| 56 | #else |
| 57 | #define ORTX_CXX_API_THROW(string, code) \ |
| 58 | throw OrtW::Exception(string, code) |
| 59 | |
| 60 | #define OCOS_TRY try |
| 61 | #define OCOS_CATCH(x) catch (x) |
| 62 | #define OCOS_RETHROW throw; |
| 63 | #define OCOS_HANDLE_EXCEPTION(func) func() |
| 64 | #endif |
| 65 | |
| 66 | inline void ThrowOnError(const OrtApi& ort, OrtStatus* status) { |
| 67 | if (status) { |
| 68 | std::string error_message = ort.GetErrorMessage(status); |
| 69 | OrtErrorCode error_code = ort.GetErrorCode(status); |
| 70 | ort.ReleaseStatus(status); |
| 71 | ORTX_CXX_API_THROW(std::move(error_message), error_code); |
| 72 | } |
| 73 | } |
| 74 | } // namespace OrtW |
| 75 | |
| 76 | // macros to wrap entry points that ORT calls where we may need to prevent exceptions propagating upwards to ORT |
| 77 | #define OCOS_API_IMPL_BEGIN \ |
| 78 | OCOS_TRY { |
| 79 | // if exceptions are disabled (a 3rd party library could throw so we need to handle that) |
| 80 | // or we're preventing exception propagation, log and abort(). |
| 81 | #if defined(OCOS_NO_EXCEPTIONS) || defined(OCOS_PREVENT_EXCEPTION_PROPAGATION) |
| 82 | #define OCOS_API_IMPL_END \ |
| 83 | } \ |
| 84 | OCOS_CATCH(const std::exception& ex) { \ |
| 85 | OCOS_HANDLE_EXCEPTION([&]() { \ |
| 86 | OrtW::PrintFinalMessage(__FILE__, __LINE__, ex.what()); \ |
| 87 | abort(); \ |
| 88 | }); \ |
| 89 | } |
| 90 | #else |
| 91 | // rethrow. |
| 92 | #define OCOS_API_IMPL_END \ |
| 93 | } \ |
| 94 | OCOS_CATCH(const std::exception&) { \ |
| 95 | OCOS_HANDLE_EXCEPTION([&]() { \ |
| 96 | OCOS_RETHROW; \ |
| 97 | }); \ |
| 98 | } |
| 99 | #endif |
| 100 | |