Skip to content

Commit 7b0da51

Browse files
committed
RUST-742 Reduce the size of Error
1 parent bb945e7 commit 7b0da51

File tree

26 files changed

+56
-70
lines changed

26 files changed

+56
-70
lines changed

src/client/executor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl Client {
110110

111111
// Retryable writes are only supported by storage engines with document-level
112112
// locking, so users need to disable retryable writes if using mmapv1.
113-
if let ErrorKind::CommandError(ref mut command_error) = err.kind {
113+
if let ErrorKind::CommandError(ref mut command_error) = *err.kind {
114114
if command_error.code == 20
115115
&& command_error.message.starts_with("Transaction numbers")
116116
{

src/client/options/test.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,7 @@ async fn run_test(test_file: TestFile) {
316316

317317
match ClientOptions::parse(&test_case.uri)
318318
.await
319-
.as_ref()
320-
.map_err(|e| &e.kind)
319+
.map_err(|e| *e.kind)
321320
{
322321
Ok(_) => panic!("expected {}", expected_type),
323322
Err(ErrorKind::ArgumentError { .. }) => {}
@@ -341,8 +340,7 @@ async fn run_connection_string_spec_tests() {
341340

342341
async fn parse_uri(option: &str, suggestion: Option<&str>) {
343342
match ClientOptionsParser::parse(&format!("mongodb://host:27017/?{}=test", option))
344-
.as_ref()
345-
.map_err(|e| &e.kind)
343+
.map_err(|e| *e.kind)
346344
{
347345
Ok(_) => panic!("expected error for option {}", option),
348346
Err(ErrorKind::ArgumentError { message, .. }) => {

src/cmap/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ impl ConnectionPool {
156156
});
157157
}
158158
Err(ref e) => {
159-
let failure_reason = if let ErrorKind::WaitQueueTimeoutError { .. } = e.kind {
159+
let failure_reason = if let ErrorKind::WaitQueueTimeoutError { .. } = *e.kind {
160160
ConnectionCheckoutFailedReason::Timeout
161161
} else {
162162
ConnectionCheckoutFailedReason::ConnectionError

src/cmap/test/file.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ pub struct Error {
9797

9898
impl Error {
9999
pub fn assert_matches(&self, error: &crate::error::Error, description: &str) {
100-
match error.kind {
100+
match error.kind.as_ref() {
101101
ErrorKind::WaitQueueTimeoutError { .. } => {
102102
assert_eq!(self.type_, "WaitQueueTimeoutError", "{}", description);
103103
}

src/coll/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ where
696696
}
697697
}
698698
}
699-
Err(e) => match e.kind {
699+
Err(e) => match *e.kind {
700700
ErrorKind::BulkWriteError(failure) => {
701701
let failure_ref =
702702
cumulative_failure.get_or_insert_with(BulkWriteFailure::new);

src/concern/test.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ async fn inconsistent_write_concern_rejected() {
107107
)
108108
.await
109109
.expect_err("insert should fail");
110-
assert!(matches!(error.kind, ErrorKind::ArgumentError { .. }));
110+
assert!(matches!(*error.kind, ErrorKind::ArgumentError { .. }));
111111

112112
let coll = db.collection(function_name!());
113113
let wc = WriteConcern {
@@ -120,7 +120,7 @@ async fn inconsistent_write_concern_rejected() {
120120
.insert_one(doc! {}, options)
121121
.await
122122
.expect_err("insert should fail");
123-
assert!(matches!(error.kind, ErrorKind::ArgumentError { .. }));
123+
assert!(matches!(*error.kind, ErrorKind::ArgumentError { .. }));
124124
}
125125

126126
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
@@ -142,5 +142,5 @@ async fn unacknowledged_write_concern_rejected() {
142142
)
143143
.await
144144
.expect_err("insert should fail");
145-
assert!(matches!(error.kind, ErrorKind::ArgumentError { .. }));
145+
assert!(matches!(*error.kind, ErrorKind::ArgumentError { .. }));
146146
}

src/cursor/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ pub(super) trait GetMoreProviderResult {
137137
match self.as_ref() {
138138
Ok(res) => res.exhausted,
139139
Err(e) => {
140-
matches!(e.kind, ErrorKind::CommandError(ref e) if e.code == 43 || e.code == 237)
140+
matches!(*e.kind, ErrorKind::CommandError(ref e) if e.code == 43 || e.code == 237)
141141
}
142142
}
143143
}

src/error.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Contains the `Error` and `Result` types that `mongodb` uses.
22
3-
use std::{fmt, sync::Arc};
3+
use std::{fmt::{self, Debug}, sync::Arc};
44

55
use serde::Deserialize;
66
use thiserror::Error;
@@ -27,7 +27,7 @@ pub type Result<T> = std::result::Result<T, Error>;
2727
#[non_exhaustive]
2828
pub struct Error {
2929
/// The type of error that occurred.
30-
pub kind: ErrorKind,
30+
pub kind: Box<ErrorKind>,
3131
labels: Vec<String>,
3232
}
3333

@@ -66,20 +66,20 @@ impl Error {
6666
}
6767

6868
pub(crate) fn is_auth_error(&self) -> bool {
69-
matches!(self.kind, ErrorKind::AuthenticationError { .. })
69+
matches!(self.kind.as_ref(), ErrorKind::AuthenticationError { .. })
7070
}
7171

7272
pub(crate) fn is_command_error(&self) -> bool {
73-
matches!(self.kind, ErrorKind::CommandError(_))
73+
matches!(self.kind.as_ref(), ErrorKind::CommandError(_))
7474
}
7575

7676
pub(crate) fn is_network_timeout(&self) -> bool {
77-
matches!(self.kind, ErrorKind::Io(ref io_err) if io_err.kind() == std::io::ErrorKind::TimedOut)
77+
matches!(self.kind.as_ref(), ErrorKind::Io(ref io_err) if io_err.kind() == std::io::ErrorKind::TimedOut)
7878
}
7979

8080
/// Whether this error is an "ns not found" error or not.
8181
pub(crate) fn is_ns_not_found(&self) -> bool {
82-
matches!(self.kind, ErrorKind::CommandError(ref err) if err.code == 26)
82+
matches!(self.kind.as_ref(), ErrorKind::CommandError(ref err) if err.code == 26)
8383
}
8484

8585
/// Whether a read operation should be retried if this error occurs.
@@ -119,7 +119,7 @@ impl Error {
119119
/// Whether an error originated from the server.
120120
pub(crate) fn is_server_error(&self) -> bool {
121121
matches!(
122-
self.kind,
122+
self.kind.as_ref(),
123123
ErrorKind::AuthenticationError { .. }
124124
| ErrorKind::BulkWriteError(_)
125125
| ErrorKind::CommandError(_)
@@ -129,7 +129,7 @@ impl Error {
129129

130130
/// Returns the labels for this error.
131131
pub fn labels(&self) -> &[String] {
132-
match self.kind {
132+
match self.kind.as_ref() {
133133
ErrorKind::CommandError(ref err) => &err.labels,
134134
ErrorKind::WriteError(ref err) => match err {
135135
WriteFailure::WriteError(_) => &self.labels,
@@ -153,7 +153,7 @@ impl Error {
153153
/// Returns a copy of this Error with the specified label added.
154154
pub(crate) fn with_label<T: AsRef<str>>(mut self, label: T) -> Self {
155155
let label = label.as_ref().to_string();
156-
match self.kind {
156+
match self.kind.as_ref() {
157157
ErrorKind::CommandError(ref err) => {
158158
let mut err = err.clone();
159159
err.labels.push(label);
@@ -197,7 +197,7 @@ where
197197
{
198198
fn from(err: E) -> Self {
199199
Self {
200-
kind: err.into(),
200+
kind: Box::new(err.into()),
201201
labels: Vec::new(),
202202
}
203203
}
@@ -603,7 +603,7 @@ impl WriteFailure {
603603
/// Translates ErrorKind::BulkWriteError cases to ErrorKind::WriteErrors, leaving all other errors
604604
/// untouched.
605605
pub(crate) fn convert_bulk_errors(error: Error) -> Error {
606-
match error.kind {
606+
match *error.kind {
607607
ErrorKind::BulkWriteError(ref bulk_failure) => {
608608
match WriteFailure::from_bulk_failure(bulk_failure.clone()) {
609609
Ok(failure) => ErrorKind::WriteError(failure).into(),

src/operation/aggregate/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ async fn handle_write_concern_error() {
310310
let error = aggregate
311311
.handle_response(response, &Default::default())
312312
.expect_err("should get wc error");
313-
match error.kind {
313+
match *error.kind {
314314
ErrorKind::WriteError(WriteFailure::WriteConcernError(_)) => {}
315315
ref e => panic!("should have gotten WriteConcernError, got {:?} instead", e),
316316
}

src/operation/count/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ async fn handle_response_no_n() {
9494
let response = CommandResponse::with_document(doc! { "ok" : 1 });
9595

9696
let result = count_op.handle_response(response, &Default::default());
97-
match result.as_ref().map_err(|e| &e.kind) {
97+
match result.map_err(|e| *e.kind) {
9898
Err(ErrorKind::ResponseError { .. }) => {}
9999
other => panic!("expected response error, but got {:?}", other),
100100
}

0 commit comments

Comments
 (0)