Enrich the type of error and declare the error type interfaces (#21024)
* Enrich the type of error and declare the error type interfaces, test=develop * adjust tests to adapt new form, test=develop * add inference deps with error_codes.pb.h, test=develop * restore stack iter start pos, test=develop * polish code based review comments, test=developcustom_op_abi
parent
3fda695bb0
commit
7ee25189c3
@ -0,0 +1,80 @@
|
||||
/* Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License. */
|
||||
|
||||
syntax = "proto2";
|
||||
option optimize_for = LITE_RUNTIME;
|
||||
package paddle.platform.error;
|
||||
|
||||
enum Code {
|
||||
// Legacy error.
|
||||
// Error type string: "Error"
|
||||
LEGACY = 0;
|
||||
|
||||
// Client specified an invalid argument.
|
||||
// Error type string: "InvalidArgumentError"
|
||||
INVALID_ARGUMENT = 1;
|
||||
|
||||
// Some requested entity (e.g., file or directory) was not found.
|
||||
// Error type string: "NotFoundError"
|
||||
NOT_FOUND = 2;
|
||||
|
||||
// Operation tried to iterate past the valid input range. E.g., seeking or
|
||||
// reading past end of file.
|
||||
// Error type string: "OutOfRangeError"
|
||||
OUT_OF_RANGE = 3;
|
||||
|
||||
// Some entity that we attempted to create (e.g., file or directory)
|
||||
// already exists.
|
||||
// Error type string: "AlreadyExistsError"
|
||||
ALREADY_EXISTS = 4;
|
||||
|
||||
// Some resource has been exhausted, perhaps a per-user quota, or
|
||||
// perhaps the entire file system is out of space.
|
||||
// Error type string: "ResourceExhaustedError"
|
||||
RESOURCE_EXHAUSTED = 5;
|
||||
|
||||
// Operation was rejected because the system is not in a state
|
||||
// required for the operation's execution.
|
||||
// Error type string: "PreconditionNotMetError"
|
||||
PRECONDITION_NOT_MET = 6;
|
||||
|
||||
// The caller does not have permission to execute the specified
|
||||
// operation.
|
||||
// Error type string: "PermissionDeniedError"
|
||||
PERMISSION_DENIED = 7;
|
||||
|
||||
// Deadline expired before operation could complete.
|
||||
// Error type string: "ExecutionTimeout"
|
||||
EXECUTION_TIMEOUT = 8;
|
||||
|
||||
// Operation is not implemented or not supported/enabled in this service.
|
||||
// Error type string: "UnimpelmentedError"
|
||||
UNIMPLEMENTED = 9;
|
||||
|
||||
// The service is currently unavailable. This is a most likely a
|
||||
// transient condition and may be corrected by retrying with
|
||||
// a backoff.
|
||||
// Error type string: "UnavailableError"
|
||||
UNAVAILABLE = 10;
|
||||
|
||||
// Fatal errors. Means some invariant expected by the underlying
|
||||
// system has been broken. If you see one of these errors,
|
||||
// something is very broken.
|
||||
// Error type string: "FatalError"
|
||||
FATAL = 11;
|
||||
|
||||
// Third-party library error.
|
||||
// Error type string: "ExternalError"
|
||||
EXTERNAL = 12;
|
||||
}
|
@ -0,0 +1,79 @@
|
||||
/* Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
|
||||
|
||||
licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License. */
|
||||
|
||||
#include "paddle/fluid/platform/errors.h"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
namespace paddle {
|
||||
namespace platform {
|
||||
|
||||
typedef ::paddle::platform::error::Code Code;
|
||||
|
||||
std::string error_name(Code code) {
|
||||
switch (code) {
|
||||
case paddle::platform::error::LEGACY:
|
||||
return "Error";
|
||||
break;
|
||||
case paddle::platform::error::INVALID_ARGUMENT:
|
||||
return "InvalidArgumentError";
|
||||
break;
|
||||
case paddle::platform::error::NOT_FOUND:
|
||||
return "NotFoundError";
|
||||
break;
|
||||
case paddle::platform::error::OUT_OF_RANGE:
|
||||
return "OutOfRangeError";
|
||||
break;
|
||||
case paddle::platform::error::ALREADY_EXISTS:
|
||||
return "AlreadyExistsError";
|
||||
break;
|
||||
case paddle::platform::error::RESOURCE_EXHAUSTED:
|
||||
return "ResourceExhaustedError";
|
||||
break;
|
||||
case paddle::platform::error::PRECONDITION_NOT_MET:
|
||||
return "PreconditionNotMetError";
|
||||
break;
|
||||
case paddle::platform::error::PERMISSION_DENIED:
|
||||
return "PermissionDeniedError";
|
||||
break;
|
||||
case paddle::platform::error::EXECUTION_TIMEOUT:
|
||||
return "ExecutionTimeoutError";
|
||||
break;
|
||||
case paddle::platform::error::UNIMPLEMENTED:
|
||||
return "UnimplementedError";
|
||||
break;
|
||||
case paddle::platform::error::UNAVAILABLE:
|
||||
return "UnavailableError";
|
||||
break;
|
||||
case paddle::platform::error::FATAL:
|
||||
return "FatalError";
|
||||
break;
|
||||
case paddle::platform::error::EXTERNAL:
|
||||
return "ExternalError";
|
||||
break;
|
||||
default:
|
||||
throw std::invalid_argument("The error type is undefined.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
std::string ErrorSummary::ToString() const {
|
||||
std::string result(error_name(code()));
|
||||
result += ": ";
|
||||
result += error_message();
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace platform
|
||||
} // namespace paddle
|
@ -0,0 +1,94 @@
|
||||
/* Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
|
||||
|
||||
licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
|
||||
#include "paddle/fluid/platform/error_codes.pb.h"
|
||||
#include "paddle/fluid/string/printf.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace platform {
|
||||
|
||||
typedef ::paddle::platform::error::Code Code;
|
||||
|
||||
class ErrorSummary {
|
||||
public:
|
||||
// Note(chenweihang): Final deprecated constructor
|
||||
// This constructor is only used to be compatible with
|
||||
// current existing no error message PADDLE_ENFORCE_*
|
||||
ErrorSummary() {
|
||||
code_ = paddle::platform::error::LEGACY;
|
||||
msg_ =
|
||||
"Paddle internal Check failed. (Please help us create a new issue, "
|
||||
"here we need to find the developer to add a user friendly error "
|
||||
"message)";
|
||||
}
|
||||
|
||||
// Note(chenweihang): Final deprecated constructor
|
||||
// This constructor is used to be compatible with
|
||||
// current existing untyped PADDLE_ENFORCE_*
|
||||
// PADDLE_ENFORCE
|
||||
template <typename... Args>
|
||||
explicit ErrorSummary(Args... args) {
|
||||
code_ = paddle::platform::error::LEGACY;
|
||||
msg_ = paddle::string::Sprintf(args...);
|
||||
}
|
||||
|
||||
// Note(chenweihang): Recommended constructor
|
||||
explicit ErrorSummary(Code code, std::string msg) : code_(code), msg_(msg) {}
|
||||
|
||||
Code code() const { return code_; }
|
||||
|
||||
const std::string& error_message() const { return msg_; }
|
||||
|
||||
std::string ToString() const;
|
||||
|
||||
private:
|
||||
Code code_;
|
||||
std::string msg_;
|
||||
};
|
||||
|
||||
namespace errors {
|
||||
|
||||
#define REGISTER_ERROR(FUNC, CONST, ...) \
|
||||
template <typename... Args> \
|
||||
::paddle::platform::ErrorSummary FUNC(Args... args) { \
|
||||
return ::paddle::platform::ErrorSummary( \
|
||||
::paddle::platform::error::CONST, ::paddle::string::Sprintf(args...)); \
|
||||
}
|
||||
|
||||
REGISTER_ERROR(InvalidArgument, INVALID_ARGUMENT)
|
||||
REGISTER_ERROR(NotFound, NOT_FOUND)
|
||||
REGISTER_ERROR(OutOfRange, OUT_OF_RANGE)
|
||||
REGISTER_ERROR(AlreadyExists, ALREADY_EXISTS)
|
||||
REGISTER_ERROR(ResourceExhausted, RESOURCE_EXHAUSTED)
|
||||
REGISTER_ERROR(PreconditionNotMet, PRECONDITION_NOT_MET)
|
||||
REGISTER_ERROR(PermissionDenied, PERMISSION_DENIED)
|
||||
REGISTER_ERROR(ExecutionTimeout, EXECUTION_TIMEOUT)
|
||||
REGISTER_ERROR(Unimplemented, UNIMPLEMENTED)
|
||||
REGISTER_ERROR(Unavailable, UNAVAILABLE)
|
||||
REGISTER_ERROR(Fatal, FATAL)
|
||||
REGISTER_ERROR(External, EXTERNAL)
|
||||
|
||||
#undef REGISTER_ERROR
|
||||
|
||||
} // namespace errors
|
||||
} // namespace platform
|
||||
} // namespace paddle
|
@ -0,0 +1,122 @@
|
||||
/* Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
|
||||
|
||||
licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License. */
|
||||
|
||||
#include <functional>
|
||||
#include <string>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "paddle/fluid/platform/enforce.h"
|
||||
#include "paddle/fluid/platform/errors.h"
|
||||
|
||||
using namespace paddle::platform::errors; // NOLINT
|
||||
|
||||
#define CHECK_PADDLE_THROW(EFUNC) \
|
||||
do { \
|
||||
bool caught_exception = false; \
|
||||
try { \
|
||||
PADDLE_THROW((EFUNC)("paddle throw test.")); \
|
||||
} catch (paddle::platform::EnforceNotMet & error) { \
|
||||
caught_exception = true; \
|
||||
std::string ex_msg = error.what(); \
|
||||
EXPECT_TRUE(ex_msg.find(#EFUNC "Error: paddle throw test.") != \
|
||||
std::string::npos); \
|
||||
} \
|
||||
EXPECT_TRUE(caught_exception); \
|
||||
} while (0)
|
||||
|
||||
#define CHECK_PADDLE_ENFORCE(EFUNC) \
|
||||
do { \
|
||||
bool caught_exception = false; \
|
||||
try { \
|
||||
PADDLE_ENFORCE(false, (EFUNC)("paddle enforce test.")); \
|
||||
} catch (paddle::platform::EnforceNotMet & error) { \
|
||||
caught_exception = true; \
|
||||
std::string ex_msg = error.what(); \
|
||||
EXPECT_TRUE(ex_msg.find(#EFUNC "Error: paddle enforce test.") != \
|
||||
std::string::npos); \
|
||||
} \
|
||||
EXPECT_TRUE(caught_exception); \
|
||||
} while (0)
|
||||
|
||||
#define CHECK_PADDLE_ENFORCE_NOT_NULL(EFUNC) \
|
||||
do { \
|
||||
bool caught_exception = false; \
|
||||
try { \
|
||||
PADDLE_ENFORCE_NOT_NULL(nullptr, \
|
||||
(EFUNC)("paddle enforce not null test.")); \
|
||||
} catch (paddle::platform::EnforceNotMet & error) { \
|
||||
caught_exception = true; \
|
||||
std::string ex_msg = error.what(); \
|
||||
EXPECT_TRUE( \
|
||||
ex_msg.find(#EFUNC "Error: paddle enforce not null test.") != \
|
||||
std::string::npos); \
|
||||
} \
|
||||
EXPECT_TRUE(caught_exception); \
|
||||
} while (0)
|
||||
|
||||
#define CHECK_PADDLE_ENFORCE_EQ(EFUNC) \
|
||||
do { \
|
||||
bool caught_exception = false; \
|
||||
try { \
|
||||
PADDLE_ENFORCE_EQ(1, 2, (EFUNC)("paddle enforce equal test.")); \
|
||||
} catch (paddle::platform::EnforceNotMet & error) { \
|
||||
caught_exception = true; \
|
||||
std::string ex_msg = error.what(); \
|
||||
EXPECT_TRUE(ex_msg.find(#EFUNC "Error: paddle enforce equal test.") != \
|
||||
std::string::npos); \
|
||||
} \
|
||||
EXPECT_TRUE(caught_exception); \
|
||||
} while (0)
|
||||
|
||||
#define CHECK_ALL_PADDLE_EXCEPTION_MACRO(EFUNC) \
|
||||
do { \
|
||||
CHECK_PADDLE_THROW(EFUNC); \
|
||||
CHECK_PADDLE_ENFORCE(EFUNC); \
|
||||
CHECK_PADDLE_ENFORCE_NOT_NULL(EFUNC); \
|
||||
CHECK_PADDLE_ENFORCE_EQ(EFUNC); \
|
||||
} while (0)
|
||||
|
||||
TEST(Errors, InvalidArgument) {
|
||||
CHECK_ALL_PADDLE_EXCEPTION_MACRO(InvalidArgument);
|
||||
}
|
||||
|
||||
TEST(Errors, NotFound) { CHECK_ALL_PADDLE_EXCEPTION_MACRO(NotFound); }
|
||||
|
||||
TEST(Errors, OutOfRange) { CHECK_ALL_PADDLE_EXCEPTION_MACRO(OutOfRange); }
|
||||
|
||||
TEST(Errors, AlreadExists) { CHECK_ALL_PADDLE_EXCEPTION_MACRO(AlreadyExists); }
|
||||
|
||||
TEST(Errors, ResourceExhausted) {
|
||||
CHECK_ALL_PADDLE_EXCEPTION_MACRO(ResourceExhausted);
|
||||
}
|
||||
|
||||
TEST(Errors, PreconditionNotMet) {
|
||||
CHECK_ALL_PADDLE_EXCEPTION_MACRO(PreconditionNotMet);
|
||||
}
|
||||
|
||||
TEST(Errors, PermissionDenied) {
|
||||
CHECK_ALL_PADDLE_EXCEPTION_MACRO(PermissionDenied);
|
||||
}
|
||||
|
||||
TEST(Errors, ExecutionTimeout) {
|
||||
CHECK_ALL_PADDLE_EXCEPTION_MACRO(ExecutionTimeout);
|
||||
}
|
||||
|
||||
TEST(Errors, Unimplemented) { CHECK_ALL_PADDLE_EXCEPTION_MACRO(Unimplemented); }
|
||||
|
||||
TEST(Errors, Unavailable) { CHECK_ALL_PADDLE_EXCEPTION_MACRO(Unavailable); }
|
||||
|
||||
TEST(Errors, Fatal) { CHECK_ALL_PADDLE_EXCEPTION_MACRO(Fatal); }
|
||||
|
||||
TEST(Errors, External) { CHECK_ALL_PADDLE_EXCEPTION_MACRO(External); }
|
Loading…
Reference in new issue