From 1fd1f06f115d1570c513856a3264ec519692d2bb Mon Sep 17 00:00:00 2001 From: Chen Weihang Date: Sun, 15 Dec 2019 20:57:35 +0800 Subject: [PATCH] Rename paddle throw error macro (#21657) * rename paddle throw error macro, test=develop * fix new error use case, test=develop --- paddle/fluid/inference/capi/pd_predictor.cc | 3 ++- .../operators/distributed_ops/recv_save_op.cc | 5 ++--- paddle/fluid/platform/enforce.h | 18 ++++++++++-------- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/paddle/fluid/inference/capi/pd_predictor.cc b/paddle/fluid/inference/capi/pd_predictor.cc index d4d5e9c35f9..114c6c03103 100644 --- a/paddle/fluid/inference/capi/pd_predictor.cc +++ b/paddle/fluid/inference/capi/pd_predictor.cc @@ -46,7 +46,8 @@ inline void VisitDataType(PD_DataType type, Visitor visitor) { _DataType_(VisitDataTypeCallback); #undef VisitDataTypeCallback - PADDLE_THROW_ERROR("Unsupported data type. "); + PADDLE_THROW( + paddle::platform::errors::InvalidArgument("Unsupported data type.")); } struct PD_ZeroCopyFunctor { diff --git a/paddle/fluid/operators/distributed_ops/recv_save_op.cc b/paddle/fluid/operators/distributed_ops/recv_save_op.cc index 766e378e106..565e9f9886e 100644 --- a/paddle/fluid/operators/distributed_ops/recv_save_op.cc +++ b/paddle/fluid/operators/distributed_ops/recv_save_op.cc @@ -165,9 +165,8 @@ class RecvSaveOpKernel : public framework::OpKernel { auto overwrite = ctx.Attr("overwrite"); if (FileExists(filename) && !overwrite) { - PADDLE_THROW_ERROR( - "%s is existed, cannot save to it when overwrite=false", filename, - overwrite); + PADDLE_THROW(platform::errors::AlreadyExists( + "%s is existed, cannot save to it when overwrite=false", filename)); } MkDirRecursively(DirName(filename).c_str()); diff --git a/paddle/fluid/platform/enforce.h b/paddle/fluid/platform/enforce.h index c558c5f91d3..d422b93cead 100644 --- a/paddle/fluid/platform/enforce.h +++ b/paddle/fluid/platform/enforce.h @@ -223,6 +223,13 @@ inline void throw_on_error(const platform::ErrorSummary& error) { #endif } +// Note: This Macro can only be used within enforce.h +#define __THROW_ERROR_INTERNAL__(...) \ + do { \ + throw ::paddle::platform::EnforceNotMet( \ + ::paddle::string::Sprintf(__VA_ARGS__), __FILE__, __LINE__); \ + } while (0) + /** ENFORCE EXCEPTION AND MACROS **/ struct EnforceNotMet : public std::exception { @@ -251,12 +258,6 @@ struct EnforceNotMet : public std::exception { ::paddle::platform::ErrorSummary(__VA_ARGS__), __FILE__, __LINE__); \ } while (0) -#define PADDLE_THROW_ERROR(...) \ - do { \ - throw ::paddle::platform::EnforceNotMet( \ - ::paddle::string::Sprintf(__VA_ARGS__), __FILE__, __LINE__); \ - } while (0) - #if defined(__CUDA_ARCH__) // For cuda, the assertions can affect performance and it is therefore // recommended to disable them in production code @@ -298,10 +299,11 @@ struct EnforceNotMet : public std::exception { * extra messages is also supported, for example: * PADDLE_ENFORCE(a, b, "some simple enforce failed between %d numbers", 2) */ + #define PADDLE_ENFORCE_NOT_NULL(__VAL, ...) \ do { \ if (UNLIKELY(nullptr == (__VAL))) { \ - PADDLE_THROW_ERROR( \ + __THROW_ERROR_INTERNAL__( \ "%s\n [Hint: " #__VAL " should not be null.]", \ ::paddle::platform::ErrorSummary(__VA_ARGS__).ToString()); \ } \ @@ -323,7 +325,7 @@ struct EnforceNotMet : public std::exception { constexpr bool __kCanToString__ = \ ::paddle::platform::details::CanToString<__TYPE1__>::kValue && \ ::paddle::platform::details::CanToString<__TYPE2__>::kValue; \ - PADDLE_THROW_ERROR( \ + __THROW_ERROR_INTERNAL__( \ "%s\n [Hint: Expected %s " #__CMP \ " %s, but received %s " #__INV_CMP " %s.]", \ ::paddle::platform::ErrorSummary(__VA_ARGS__).ToString(), #__VAL1, \ -- GitLab