Skip to content

Commit 5a15c70

Browse files
committed
Make Error interface cleaner
1 parent 699d18f commit 5a15c70

File tree

3 files changed

+45
-73
lines changed

3 files changed

+45
-73
lines changed

paddle/gserver/activations/ActivationFunction.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ Argument argument_;
169169
public:
170170
Error __must_check forward(Argument& act) {
171171
if (act.value->getWidth() != 1UL) {
172-
return ErrorF(
172+
return Error(
173173
"Input width for each timestep of sequence softmax should be 1");
174174
}
175175

@@ -193,7 +193,7 @@ Error __must_check forward(Argument& act) {
193193

194194
Error __must_check backward(Argument& act) {
195195
if (act.value->getWidth() != 1UL) {
196-
return ErrorF(
196+
return Error(
197197
"Input width for each timestep of sequence softmax should be 1");
198198
}
199199

@@ -208,7 +208,7 @@ Error __must_check backward(Argument& act) {
208208
argument_.grad->setData(act.grad->getData() + offset, 1UL, size);
209209

210210
Error status = softmax_.backward(argument_);
211-
if (!status.isOK()) return status;
211+
if (!status) return status;
212212
}
213213
return Error();
214214
}

paddle/utils/Error.h

Lines changed: 32 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,12 @@ limitations under the License. */
2323
namespace paddle {
2424

2525
/**
26-
* Status is Paddle error code. It only contain a std::string as error message.
27-
* Although Status inherits the std::exception, but do not throw it except you
28-
* know what you are doing.
26+
* Error is Paddle error code. It only contain a std::string as error message.
2927
*
3028
*
31-
* There are two styles to return status in Paddle.
29+
* There are two styles to return error in Paddle.
3230
*
33-
* 1. Return Status
31+
* 1. Return Error
3432
* When method return a status, the return must use `__must_check` attribute.
3533
* Example as below.
3634
* @code{cpp}
@@ -39,29 +37,29 @@ namespace paddle {
3937
* Error __must_check bar() {
4038
* // do something.
4139
* Status s = foo(); // invoke other method return status.
42-
* if (!s.isOK()) return s;
40+
* if (!s) return s;
4341
* // do something else.
4442
* return Status();
4543
* }
4644
* @endcode{cpp}
4745
*
4846
* 2. Return by parameter.
49-
* It is another way to return a status, by using a pointer parameter.
47+
* It is another way to return an error, by using a pointer parameter.
5048
* Example as below.
5149
*
5250
* @code{cpp}
5351
* Error bar();
5452
*
55-
* int foo(Error* status) {
53+
* int foo(Error* error) {
5654
* // Do something.
57-
* Status s = bar();
58-
* if (!s.isOK()) {
59-
* *status = s;
55+
* Error s = bar();
56+
* if (!s) {
57+
* *error = s;
6058
* return 0;
6159
* }
6260
* // Do something else.
6361
* if (someInternalErrorHappend) {
64-
* *status = ErrorF("Some dimension is too large, %d", dimension);
62+
* *error = Error("Some dimension is too large, %d", dimension);
6563
* return 0;
6664
* }
6765
* // End of method.
@@ -72,7 +70,7 @@ namespace paddle {
7270
* Error s;
7371
* // do something.
7472
* foo(&s);
75-
* if (!s.isOK()) return s;
73+
* if (!s) return s;
7674
* }
7775
* @endcode{cpp}
7876
*
@@ -81,17 +79,31 @@ namespace paddle {
8179
* use log(FATAL) or CHECK to make program exit before. When we clean all
8280
* log(FATAL) and CHECK in Paddle, 'check' method will be removed.
8381
*/
84-
class Error final : public std::exception {
82+
class Error final {
8583
public:
8684
/**
8785
* Default Status. OK
8886
*/
89-
Error() noexcept {}
87+
inline Error() {}
9088

9189
/**
92-
* @brief what will return the error message. If status is OK, return nullptr.
90+
* @brief Create an Error use printf syntax.
9391
*/
94-
const char* what() const noexcept override {
92+
inline explicit Error(const char* fmt, ...) {
93+
va_list ap;
94+
va_start(ap, fmt);
95+
constexpr size_t kBufferSize = 1024;
96+
this->errMsg_.reset(new std::string(kBufferSize, 0));
97+
auto sz = vsnprintf(&(*errMsg_)[0], kBufferSize, fmt, ap);
98+
this->errMsg_->resize(sz);
99+
this->errMsg_->shrink_to_fit();
100+
va_end(ap);
101+
}
102+
103+
/**
104+
* @brief what will return the error message. If no error, return nullptr.
105+
*/
106+
inline const char* msg() const {
95107
if (errMsg_) {
96108
return errMsg_->data();
97109
} else {
@@ -100,58 +112,18 @@ class Error final : public std::exception {
100112
}
101113

102114
/**
103-
* @brief isOK
104-
* @return true if OK.
115+
* @brief operator bool, return True if there is no error.
105116
*/
106-
inline bool isOK() const noexcept { return errMsg_ == nullptr; }
107-
117+
inline operator bool() const { return !errMsg_; }
108118
/**
109119
* @brief check this status by glog.
110120
* @note It is a temp method used during cleaning Paddle code. It will be
111121
* removed later.
112122
*/
113-
inline void check() const { CHECK(isOK()) << what(); }
114-
115-
/**
116-
* friend method to create Error.
117-
*/
118-
template <typename... ARGS>
119-
friend Error __must_check ErrorF(const char* fmt, ARGS... args);
123+
inline void check() const { CHECK(*this) << msg(); }
120124

121125
private:
122126
std::shared_ptr<std::string> errMsg_;
123127
};
124128

125-
/**
126-
* ErrorF will create an Error by printf syntax.
127-
*
128-
* Specialize this method because clang will give a warning when use printf(fmt)
129-
* without arguments.
130-
*/
131-
template <>
132-
inline Error __must_check ErrorF(const char* msg) {
133-
Error e;
134-
e.errMsg_.reset(new std::string(msg));
135-
return e;
136-
}
137-
138-
/**
139-
* ErrorF will create an Error by printf syntax.
140-
*
141-
* Examples:
142-
* @code{cpp}
143-
* auto err = ErrorF("SomeError");
144-
* auto err2 = ErrorF("SomeErrorWithParameter %f %d", real_val, int_val);
145-
* @endcode{cpp}
146-
*/
147-
template <typename... ARGS>
148-
inline Error __must_check ErrorF(const char* fmt, ARGS... args) {
149-
constexpr size_t kBufferSize = 1024;
150-
char buffer[kBufferSize];
151-
snprintf(buffer, kBufferSize, fmt, args...);
152-
Error e;
153-
e.errMsg_.reset(new std::string(buffer));
154-
return e;
155-
}
156-
157129
} // namespace paddle

paddle/utils/tests/test_Error.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@ limitations under the License. */
1818

1919
TEST(Error, testAll) {
2020
paddle::Error error;
21-
ASSERT_TRUE(error.isOK());
22-
error = paddle::ErrorF("I'm the error");
23-
ASSERT_FALSE(error.isOK());
24-
ASSERT_STREQ("I'm the error", error.what());
21+
ASSERT_TRUE(error);
22+
error = paddle::Error("I'm the error");
23+
ASSERT_FALSE(error);
24+
ASSERT_STREQ("I'm the error", error.msg());
2525

26-
error = paddle::ErrorF("error2");
27-
ASSERT_FALSE(error.isOK());
28-
ASSERT_STREQ("error2", error.what());
26+
error = paddle::Error("error2");
27+
ASSERT_FALSE(error);
28+
ASSERT_STREQ("error2", error.msg());
2929

3030
int i = 3;
31-
auto error3 = paddle::ErrorF("error%d", i);
32-
ASSERT_FALSE(error3.isOK());
33-
ASSERT_STREQ("error3", error3.what());
31+
auto error3 = paddle::Error("error%d", i);
32+
ASSERT_FALSE(error3);
33+
ASSERT_STREQ("error3", error3.msg());
3434
}

0 commit comments

Comments
 (0)