Skip to content

Commit 98a9dcb

Browse files
author
Michael Ingley
committed
errors: restore GrpcAPI payload compatibility without cfg(clippy) split
Signed-off-by: Michael Ingley <mingley@linkedin.com>
1 parent cfad559 commit 98a9dcb

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

examples/raw.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright 2018 TiKV Project Authors. Licensed under Apache-2.0.
22

33
#![type_length_limit = "8165158"]
4+
#![allow(clippy::result_large_err)]
45

56
mod common;
67

src/common/errors.rs

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub enum Error {
6868
/// Wraps a `reqwest::Error`.
6969
/// Wraps a `grpcio::Error`.
7070
#[error("gRPC api error: {0}")]
71-
GrpcAPI(Box<tonic::Status>),
71+
GrpcAPI(tonic::Status),
7272
/// Wraps a `grpcio::Error`.
7373
#[error("url error: {0}")]
7474
Url(#[from] tonic::codegen::http::uri::InvalidUri),
@@ -144,7 +144,7 @@ impl From<ProtoKeyError> for Error {
144144

145145
impl From<tonic::Status> for Error {
146146
fn from(status: tonic::Status) -> Error {
147-
Error::GrpcAPI(Box::new(status))
147+
Error::GrpcAPI(status)
148148
}
149149
}
150150

@@ -163,3 +163,62 @@ macro_rules! internal_err {
163163
internal_err!(format!($f, $($arg),+))
164164
});
165165
}
166+
167+
#[cfg(test)]
168+
mod test {
169+
use tonic::Code;
170+
171+
use super::Error;
172+
use super::ProtoKeyError;
173+
use super::ProtoRegionError;
174+
175+
#[test]
176+
fn from_tonic_status_produces_grpc_api_error() {
177+
let status = tonic::Status::new(Code::Unavailable, "network unavailable");
178+
let error: Error = status.clone().into();
179+
180+
let Error::GrpcAPI(actual_status) = error else {
181+
panic!("expected Error::GrpcAPI");
182+
};
183+
assert_eq!(actual_status.code(), status.code());
184+
assert_eq!(actual_status.message(), status.message());
185+
}
186+
187+
#[test]
188+
fn grpc_api_variant_accepts_tonic_status_payload() {
189+
let error = Error::GrpcAPI(tonic::Status::new(Code::DeadlineExceeded, "timeout"));
190+
let Error::GrpcAPI(status) = error else {
191+
panic!("expected Error::GrpcAPI");
192+
};
193+
assert_eq!(status.code(), Code::DeadlineExceeded);
194+
assert_eq!(status.message(), "timeout");
195+
}
196+
197+
#[test]
198+
fn from_proto_region_error_wraps_region_error() {
199+
let region_error = ProtoRegionError {
200+
message: "region stale".to_owned(),
201+
..Default::default()
202+
};
203+
let error: Error = region_error.clone().into();
204+
205+
let Error::RegionError(actual_region_error) = error else {
206+
panic!("expected Error::RegionError");
207+
};
208+
assert_eq!(actual_region_error.message, region_error.message);
209+
}
210+
211+
#[test]
212+
fn from_proto_key_error_wraps_key_error() {
213+
let key_error = ProtoKeyError {
214+
retryable: "retry later".to_owned(),
215+
..Default::default()
216+
};
217+
let error: Error = key_error.clone().into();
218+
219+
let Error::KeyError(actual_key_error) = error else {
220+
panic!("expected Error::KeyError");
221+
};
222+
assert_eq!(actual_key_error.retryable, key_error.retryable);
223+
}
224+
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
//! ```
9292
9393
#![allow(clippy::field_reassign_with_default)]
94+
#![allow(clippy::result_large_err)]
9495

9596
pub mod backoff;
9697
#[doc(hidden)]

0 commit comments

Comments
 (0)