@@ -890,6 +890,76 @@ index 90cd4c9678..b01ded641b 100644
890890 #ifdef __cplusplus
891891 } // extern "C"
892892 #endif
893+ diff --git a/paddle/fluid/inference/capi_exp/pd_error_internal.h b/paddle/fluid/inference/capi_exp/pd_error_internal.h
894+ new file mode 100644
895+ index 0000000000..75cf63257c
896+ --- /dev/null
897+ +++ b/paddle/fluid/inference/capi_exp/pd_error_internal.h
898+ @@ -0,0 +1,63 @@
899+ + // Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
900+ + //
901+ + // Licensed under the Apache License, Version 2.0 (the "License");
902+ + // you may not use this file except in compliance with the License.
903+ + // You may obtain a copy of the License at
904+ + //
905+ + // http://www.apache.org/licenses/LICENSE-2.0
906+ + //
907+ + // Unless required by applicable law or agreed to in writing, software
908+ + // distributed under the License is distributed on an "AS IS" BASIS,
909+ + // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
910+ + // See the License for the specific language governing permissions and
911+ + // limitations under the License.
912+ +
913+ + #pragma once
914+ +
915+ + #include <exception>
916+ + #include <stdexcept>
917+ + #include <string>
918+ + #include <utility>
919+ +
920+ + #include "paddle/fluid/inference/capi_exp/pd_common.h"
921+ + #include "paddle/fluid/platform/enforce.h"
922+ +
923+ + namespace paddle_infer {
924+ + namespace capi_exp {
925+ +
926+ + void ClearLastError();
927+ + int GetLastErrorCode();
928+ + const char* GetLastErrorMessage();
929+ + int SetLastError(PD_StatusCode code, const std::string& message);
930+ +
931+ + template <typename Func>
932+ + int SafeCall(Func&& func) noexcept {
933+ + try {
934+ + ClearLastError();
935+ + std::forward<Func>(func)();
936+ + return PD_STATUS_OK;
937+ + } catch (const common::enforce::EnforceNotMet& exception) {
938+ + return SetLastError(PD_STATUS_PADDLE_EXCEPTION, exception.what());
939+ + } catch (const std::invalid_argument& exception) {
940+ + return SetLastError(PD_STATUS_INVALID_ARGUMENT, exception.what());
941+ + } catch (const std::exception& exception) {
942+ + return SetLastError(PD_STATUS_STD_EXCEPTION, exception.what());
943+ + } catch (...) {
944+ + return SetLastError(PD_STATUS_UNKNOWN, "Unknown C++ exception");
945+ + }
946+ + }
947+ +
948+ + template <typename T, typename Func>
949+ + int SafeCallWithOutput(T* out, T failure_value, Func&& func) noexcept {
950+ + if (out != nullptr) {
951+ + *out = failure_value;
952+ + }
953+ + if (out == nullptr) {
954+ + return SetLastError(PD_STATUS_INVALID_ARGUMENT,
955+ + "The pointer of out shouldn't be nullptr");
956+ + }
957+ + return SafeCall([&]() { *out = std::forward<Func>(func)(); });
958+ + }
959+ +
960+ + } // namespace capi_exp
961+ + } // namespace paddle_infer
962+ \ No newline at end of file
893963diff --git a/paddle/fluid/inference/capi_exp/pd_predictor.cc b/paddle/fluid/inference/capi_exp/pd_predictor.cc
894964index 8e193411bf..e4a26beb49 100644
895965--- a/paddle/fluid/inference/capi_exp/pd_predictor.cc
0 commit comments