Skip to content

Commit a8cb8c3

Browse files
authored
Merge pull request containerd#67 from Tim-Zhang/update-error
Update methods and macros related to error
2 parents ceccaa5 + 5ce0548 commit a8cb8c3

File tree

9 files changed

+35
-32
lines changed

9 files changed

+35
-32
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ttrpc"
3-
version = "0.4.8"
3+
version = "0.4.9"
44
authors = ["The AntFin Kata Team <kata@list.alibaba-inc.com>"]
55
edition = "2018"
66
license = "Apache-2.0"

src/asynchronous/client.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ impl Client {
148148
let mut buf = Vec::with_capacity(req.compute_size() as usize);
149149
{
150150
let mut s = CodedOutputStream::vec(&mut buf);
151-
req.write_to(&mut s).map_err(err_to_Others!(e, ""))?;
152-
s.flush().map_err(err_to_Others!(e, ""))?;
151+
req.write_to(&mut s).map_err(err_to_others_err!(e, ""))?;
152+
s.flush().map_err(err_to_others_err!(e, ""))?;
153153
}
154154

155155
let (tx, mut rx): (ResponseSender, ResponseReceiver) = channel(100);
@@ -179,7 +179,7 @@ impl Client {
179179
let mut s = CodedInputStream::from_bytes(&buf);
180180
let mut res = Response::new();
181181
res.merge_from(&mut s)
182-
.map_err(err_to_Others!(e, "Unpack response error "))?;
182+
.map_err(err_to_others_err!(e, "Unpack response error "))?;
183183

184184
let status = res.get_status();
185185
if status.get_code() != Code::OK {

src/asynchronous/stream.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ where
4141
let mut mh = MessageHeader::default();
4242
let mut covbuf: &[u8] = &buf[..4];
4343
mh.length = byteorder::ReadBytesExt::read_u32::<BigEndian>(&mut covbuf)
44-
.map_err(err_to_RpcStatus!(Code::INVALID_ARGUMENT, e, ""))?;
44+
.map_err(err_to_rpc_err!(Code::INVALID_ARGUMENT, e, ""))?;
4545
let mut covbuf: &[u8] = &buf[4..8];
4646
mh.stream_id = byteorder::ReadBytesExt::read_u32::<BigEndian>(&mut covbuf)
47-
.map_err(err_to_RpcStatus!(Code::INVALID_ARGUMENT, e, ""))?;
47+
.map_err(err_to_rpc_err!(Code::INVALID_ARGUMENT, e, ""))?;
4848
mh.type_ = buf[8];
4949
mh.flags = buf[9];
5050

@@ -113,8 +113,8 @@ pub fn to_res_buf(stream_id: u32, mut body: Vec<u8>) -> Vec<u8> {
113113
fn get_response_body(res: &Response) -> Result<Vec<u8>> {
114114
let mut buf = Vec::with_capacity(res.compute_size() as usize);
115115
let mut s = protobuf::CodedOutputStream::vec(&mut buf);
116-
res.write_to(&mut s).map_err(err_to_Others!(e, ""))?;
117-
s.flush().map_err(err_to_Others!(e, ""))?;
116+
res.write_to(&mut s).map_err(err_to_others_err!(e, ""))?;
117+
s.flush().map_err(err_to_others_err!(e, ""))?;
118118

119119
Ok(buf)
120120
}
@@ -128,7 +128,7 @@ pub async fn respond(
128128

129129
tx.send(buf)
130130
.await
131-
.map_err(err_to_Others!(e, "Send packet to sender error "))
131+
.map_err(err_to_others_err!(e, "Send packet to sender error "))
132132
}
133133

134134
pub async fn respond_with_status(
@@ -152,5 +152,5 @@ pub async fn respond_with_status(
152152

153153
tx.send(buf)
154154
.await
155-
.map_err(err_to_Others!(e, "Send packet to sender error "))
155+
.map_err(err_to_others_err!(e, "Send packet to sender error "))
156156
}

src/asynchronous/utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ pub struct TtrpcContext {
9292
pub fn convert_response_to_buf(res: Response) -> Result<Vec<u8>> {
9393
let mut buf = Vec::with_capacity(res.compute_size() as usize);
9494
let mut s = protobuf::CodedOutputStream::vec(&mut buf);
95-
res.write_to(&mut s).map_err(err_to_Others!(e, ""))?;
96-
s.flush().map_err(err_to_Others!(e, ""))?;
95+
res.write_to(&mut s).map_err(err_to_others_err!(e, ""))?;
96+
s.flush().map_err(err_to_others_err!(e, ""))?;
9797

9898
Ok(buf)
9999
}

src/common.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub fn do_bind(host: &str) -> Result<(RawFd, Domain)> {
8181
.map_err(|e| Error::Socket(e.to_string()))?;
8282
let sockaddr_h = hostv[1].to_owned() + &"\x00".to_string();
8383
let sockaddr_u =
84-
UnixAddr::new_abstract(sockaddr_h.as_bytes()).map_err(err_to_Others!(e, ""))?;
84+
UnixAddr::new_abstract(sockaddr_h.as_bytes()).map_err(err_to_others_err!(e, ""))?;
8585
sockaddr = SockAddr::Unix(sockaddr_u);
8686
}
8787
Domain::Vsock => {
@@ -107,7 +107,7 @@ pub fn do_bind(host: &str) -> Result<(RawFd, Domain)> {
107107
};
108108

109109
setsockopt(fd, sockopt::ReusePort, &true).ok();
110-
bind(fd, &sockaddr).map_err(err_to_Others!(e, ""))?;
110+
bind(fd, &sockaddr).map_err(err_to_others_err!(e, ""))?;
111111

112112
Ok((fd, domain))
113113
}

src/error.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ pub enum Error {
3535
pub type Result<T> = result::Result<T, Error>;
3636

3737
/// Get ttrpc::Status from ttrpc::Code and a message.
38-
pub fn get_status(c: Code, msg: String) -> Status {
38+
pub fn get_status(c: Code, msg: impl ToString) -> Status {
3939
let mut status = Status::new();
4040
status.set_code(c);
41-
status.set_message(msg);
41+
status.set_message(msg.to_string());
4242

4343
status
4444
}
4545

46-
pub fn get_rpc_status(c: Code, msg: String) -> Error {
46+
pub fn get_rpc_status(c: Code, msg: impl ToString) -> Error {
4747
Error::RpcStatus(get_status(c, msg))
4848
}
4949

@@ -56,13 +56,13 @@ pub fn sock_error_msg(size: usize, msg: String) -> Error {
5656
get_rpc_status(Code::INVALID_ARGUMENT, msg)
5757
}
5858

59-
macro_rules! err_to_RpcStatus {
59+
macro_rules! err_to_rpc_err {
6060
($c: expr, $e: ident, $s: expr) => {
6161
|$e| get_rpc_status($c, $s.to_string() + &$e.to_string())
6262
};
6363
}
6464

65-
macro_rules! err_to_Others {
65+
macro_rules! err_to_others_err {
6666
($e: ident, $s: expr) => {
6767
|$e| Error::Others($s.to_string() + &$e.to_string())
6868
};

src/sync/channel.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn read_count(fd: RawFd, count: usize) -> Result<Vec<u8>> {
2626
let mut len = 0;
2727

2828
if count == 0 {
29-
return Ok(v.to_vec())
29+
return Ok(v.to_vec());
3030
}
3131

3232
loop {
@@ -54,7 +54,7 @@ fn write_count(fd: RawFd, buf: &[u8], count: usize) -> Result<usize> {
5454
let mut len = 0;
5555

5656
if count == 0 {
57-
return Ok(0)
57+
return Ok(0);
5858
}
5959

6060
loop {
@@ -92,12 +92,12 @@ fn read_message_header(fd: RawFd) -> Result<MessageHeader> {
9292
mh.length =
9393
covbuf
9494
.read_u32::<BigEndian>()
95-
.map_err(err_to_RpcStatus!(Code::INVALID_ARGUMENT, e, ""))?;
95+
.map_err(err_to_rpc_err!(Code::INVALID_ARGUMENT, e, ""))?;
9696
let mut covbuf: &[u8] = &buf[4..8];
9797
mh.stream_id =
9898
covbuf
9999
.read_u32::<BigEndian>()
100-
.map_err(err_to_RpcStatus!(Code::INVALID_ARGUMENT, e, ""))?;
100+
.map_err(err_to_rpc_err!(Code::INVALID_ARGUMENT, e, ""))?;
101101
mh.type_ = buf[8];
102102
mh.flags = buf[9];
103103

src/sync/client.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,31 +173,34 @@ impl Client {
173173
pub fn request(&self, req: Request) -> Result<Response> {
174174
let mut buf = Vec::with_capacity(req.compute_size() as usize);
175175
let mut s = CodedOutputStream::vec(&mut buf);
176-
req.write_to(&mut s).map_err(err_to_Others!(e, ""))?;
177-
s.flush().map_err(err_to_Others!(e, ""))?;
176+
req.write_to(&mut s).map_err(err_to_others_err!(e, ""))?;
177+
s.flush().map_err(err_to_others_err!(e, ""))?;
178178

179179
let (tx, rx) = mpsc::sync_channel(0);
180180

181181
self.sender_tx
182182
.send((buf, tx))
183-
.map_err(err_to_Others!(e, "Send packet to sender error "))?;
183+
.map_err(err_to_others_err!(e, "Send packet to sender error "))?;
184184

185185
let result: Result<Vec<u8>>;
186186
if req.timeout_nano == 0 {
187187
result = rx
188188
.recv()
189-
.map_err(err_to_Others!(e, "Receive packet from recver error: "))?;
189+
.map_err(err_to_others_err!(e, "Receive packet from recver error: "))?;
190190
} else {
191191
result = rx
192192
.recv_timeout(Duration::from_nanos(req.timeout_nano as u64))
193-
.map_err(err_to_Others!(e, "Receive packet from recver timeout: "))?;
193+
.map_err(err_to_others_err!(
194+
e,
195+
"Receive packet from recver timeout: "
196+
))?;
194197
}
195198

196199
let buf = result?;
197200
let mut s = CodedInputStream::from_bytes(&buf);
198201
let mut res = Response::new();
199202
res.merge_from(&mut s)
200-
.map_err(err_to_Others!(e, "Unpack response error "))?;
203+
.map_err(err_to_others_err!(e, "Unpack response error "))?;
201204

202205
let status = res.get_status();
203206
if status.get_code() != Code::OK {

src/sync/utils.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ pub fn response_to_channel(
1717
) -> Result<()> {
1818
let mut buf = Vec::with_capacity(res.compute_size() as usize);
1919
let mut s = protobuf::CodedOutputStream::vec(&mut buf);
20-
res.write_to(&mut s).map_err(err_to_Others!(e, ""))?;
21-
s.flush().map_err(err_to_Others!(e, ""))?;
20+
res.write_to(&mut s).map_err(err_to_others_err!(e, ""))?;
21+
s.flush().map_err(err_to_others_err!(e, ""))?;
2222

2323
let mh = MessageHeader {
2424
length: buf.len() as u32,
2525
stream_id,
2626
type_: MESSAGE_TYPE_RESPONSE,
2727
flags: 0,
2828
};
29-
tx.send((mh, buf)).map_err(err_to_Others!(e, ""))?;
29+
tx.send((mh, buf)).map_err(err_to_others_err!(e, ""))?;
3030

3131
Ok(())
3232
}

0 commit comments

Comments
 (0)