Skip to content

Commit 648081c

Browse files
xunnanxufacebook-github-bot
authored andcommitted
Add error class
Summary: Credit to original author Pieter Noordhuis (pietern) This diff resolves conflicts. Differential Revision: D45437711 fbshipit-source-id: c651df2b9c9226c502ac552d67193c890d95c61d
1 parent 63b3ab2 commit 648081c

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed

gloo/transport/tcp/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ else()
66
"${CMAKE_CURRENT_SOURCE_DIR}/buffer.cc"
77
"${CMAKE_CURRENT_SOURCE_DIR}/context.cc"
88
"${CMAKE_CURRENT_SOURCE_DIR}/device.cc"
9+
"${CMAKE_CURRENT_SOURCE_DIR}/error.cc"
910
"${CMAKE_CURRENT_SOURCE_DIR}/loop.cc"
1011
"${CMAKE_CURRENT_SOURCE_DIR}/pair.cc"
1112
"${CMAKE_CURRENT_SOURCE_DIR}/socket.cc"
@@ -17,6 +18,7 @@ else()
1718
"${CMAKE_CURRENT_SOURCE_DIR}/buffer.h"
1819
"${CMAKE_CURRENT_SOURCE_DIR}/context.h"
1920
"${CMAKE_CURRENT_SOURCE_DIR}/device.h"
21+
"${CMAKE_CURRENT_SOURCE_DIR}/error.h"
2022
"${CMAKE_CURRENT_SOURCE_DIR}/loop.h"
2123
"${CMAKE_CURRENT_SOURCE_DIR}/pair.h"
2224
"${CMAKE_CURRENT_SOURCE_DIR}/socket.h"

gloo/transport/tcp/error.cc

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Copyright (c) 2017-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
#include <gloo/transport/tcp/error.h>
10+
11+
#include <cstring>
12+
#include <sstream>
13+
14+
namespace gloo {
15+
namespace transport {
16+
namespace tcp {
17+
18+
const Error Error::kSuccess = Error();
19+
20+
std::string Error::what() const {
21+
return "no error";
22+
}
23+
24+
std::string SystemError::what() const {
25+
std::ostringstream ss;
26+
ss << syscall_ << ": " << strerror(error_);
27+
return ss.str();
28+
}
29+
30+
std::string ShortReadError::what() const {
31+
std::ostringstream ss;
32+
ss << "short read: got " << actual_ << " bytes while expecting to read "
33+
<< expected_ << " bytes";
34+
return ss.str();
35+
}
36+
37+
std::string ShortWriteError::what() const {
38+
std::ostringstream ss;
39+
ss << "short write: wrote " << actual_ << " bytes while expecting to write "
40+
<< expected_ << " bytes";
41+
return ss.str();
42+
}
43+
44+
} // namespace tcp
45+
} // namespace transport
46+
} // namespace gloo

gloo/transport/tcp/error.h

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* Copyright (c) 2017-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
#pragma once
10+
11+
#include <string>
12+
13+
namespace gloo {
14+
namespace transport {
15+
namespace tcp {
16+
17+
class Error {
18+
public:
19+
// Constant instance that indicates success.
20+
static const Error kSuccess;
21+
22+
/* implicit */ Error() : valid_(false) {}
23+
24+
virtual ~Error() = default;
25+
26+
// Converting to boolean means checking if there is an error. This
27+
// means we don't need to use an `std::optional` and allows for a
28+
// snippet like the following:
29+
//
30+
// if (error) {
31+
// // Deal with it.
32+
// }
33+
//
34+
operator bool() const {
35+
return valid_;
36+
}
37+
38+
// Returns an explanatory string.
39+
// Like `std::exception` but returns a `std::string`.
40+
virtual std::string what() const;
41+
42+
protected:
43+
explicit Error(bool valid) : valid_(valid) {}
44+
45+
private:
46+
const bool valid_;
47+
};
48+
49+
class SystemError : public Error {
50+
public:
51+
explicit SystemError(const char* syscall, int error)
52+
: Error(true), syscall_(syscall), error_(error) {}
53+
54+
std::string what() const override;
55+
56+
private:
57+
const char* syscall_;
58+
const int error_;
59+
};
60+
61+
class ShortReadError : public Error {
62+
public:
63+
ShortReadError(ssize_t expected, ssize_t actual)
64+
: Error(true), expected_(expected), actual_(actual) {}
65+
66+
std::string what() const override;
67+
68+
private:
69+
const ssize_t expected_;
70+
const ssize_t actual_;
71+
};
72+
73+
class ShortWriteError : public Error {
74+
public:
75+
ShortWriteError(ssize_t expected, ssize_t actual)
76+
: Error(true), expected_(expected), actual_(actual) {}
77+
78+
std::string what() const override;
79+
80+
private:
81+
const ssize_t expected_;
82+
const ssize_t actual_;
83+
};
84+
85+
} // namespace tcp
86+
} // namespace transport
87+
} // namespace gloo

0 commit comments

Comments
 (0)