Skip to content

Commit 0b22b82

Browse files
committed
cleanup(lro): simplify error handling
1 parent 75a8b9b commit 0b22b82

File tree

2 files changed

+7
-132
lines changed

2 files changed

+7
-132
lines changed

src/generated/cloud/compute/v1/src/operation.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
// limitations under the License.
1414

1515
use crate::model::Operation;
16-
use gax::error::rpc::Status;
1716

1817
impl lro::internal::DiscoveryOperation for Operation {
1918
fn name(&self) -> Option<&String> {
@@ -22,7 +21,4 @@ impl lro::internal::DiscoveryOperation for Operation {
2221
fn done(&self) -> bool {
2322
self.status == Some(crate::model::operation::Status::Done)
2423
}
25-
fn status(&self) -> Option<Status> {
26-
None
27-
}
2824
}

src/lro/src/internal/discovery.rs

Lines changed: 7 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
//! we can name directly.
2424
//!
2525
26-
use crate::{Error, Poller, PollingBackoffPolicy, PollingErrorPolicy, PollingResult, Result};
27-
use gax::error::rpc::Status;
26+
use crate::{Poller, PollingBackoffPolicy, PollingErrorPolicy, PollingResult, Result};
2827
use gax::polling_state::PollingState;
2928
use gax::retry_result::RetryResult;
3029
use std::sync::Arc;
@@ -49,13 +48,6 @@ pub trait DiscoveryOperation {
4948
///
5049
/// It may be `None` in which case the polling loop stops.
5150
fn name(&self) -> Option<&String>;
52-
53-
/// Determines if the operation completed with an error.
54-
///
55-
/// If the operation completed successfully this returns `None`. On an error
56-
/// the trait must convert the error details to `gax::error::rpc::Status` as
57-
/// it always indicates a service error.
58-
fn status(&self) -> Option<Status>;
5951
}
6052

6153
pub fn new_discovery_poller<S, SF, Q, QF, O>(
@@ -170,7 +162,7 @@ where
170162
{
171163
match result {
172164
Err(ref _e) => (None, PollingResult::Completed(result)),
173-
Ok(o) if o.done() => (None, handle_polling_done(o)),
165+
Ok(o) if o.done() => (None, PollingResult::Completed(Ok(o))),
174166
Ok(o) => handle_polling_success(o),
175167
}
176168
}
@@ -189,7 +181,7 @@ where
189181
let state = error_policy.on_error(state, e);
190182
self::handle_polling_error(state, operation_name)
191183
}
192-
Ok(o) if o.done() => (None, handle_polling_done(o)),
184+
Ok(o) if o.done() => (None, PollingResult::Completed(Ok(o))),
193185
Ok(o) => handle_polling_success(o),
194186
}
195187
}
@@ -209,34 +201,22 @@ where
209201
}
210202
}
211203

212-
fn handle_polling_done<O>(o: O) -> PollingResult<O, O>
213-
where
214-
O: DiscoveryOperation,
215-
{
216-
match o.status() {
217-
None => PollingResult::Completed(Ok(o)),
218-
Some(s) => PollingResult::Completed(Err(Error::service(s))),
219-
}
220-
}
221-
222204
fn handle_polling_success<O>(o: O) -> (Option<String>, PollingResult<O, O>)
223205
where
224206
O: DiscoveryOperation,
225207
{
226-
match o.status() {
227-
Some(s) => (None, PollingResult::Completed(Err(Error::service(s)))),
228-
None => (o.name().cloned(), PollingResult::InProgress(Some(o))),
229-
}
208+
(o.name().cloned(), PollingResult::InProgress(Some(o)))
230209
}
231210

232211
#[cfg(test)]
233212
mod tests {
234-
use std::time::Duration;
235-
236213
use super::*;
214+
use crate::Error;
237215
use gax::error::rpc::Code;
216+
use gax::error::rpc::Status;
238217
use gax::exponential_backoff::{ExponentialBackoff, ExponentialBackoffBuilder};
239218
use gax::polling_error_policy::{Aip194Strict, AlwaysContinue};
219+
use std::time::Duration;
240220

241221
#[tokio::test]
242222
async fn poller_until_done_success() {
@@ -322,40 +302,6 @@ mod tests {
322302
);
323303
}
324304

325-
#[tokio::test]
326-
async fn poller_until_done_error_on_done() {
327-
let start = || async move {
328-
let op = TestOperation {
329-
name: Some("start-name".into()),
330-
..TestOperation::default()
331-
};
332-
Ok(op)
333-
};
334-
let query = |_name| async move {
335-
let op = TestOperation {
336-
done: true,
337-
status: Some(permanent_status()),
338-
..TestOperation::default()
339-
};
340-
Ok(op)
341-
};
342-
let got = new_discovery_poller(
343-
Arc::new(AlwaysContinue),
344-
Arc::new(test_backoff()),
345-
start,
346-
query,
347-
)
348-
.until_done()
349-
.await;
350-
assert!(
351-
matches!(
352-
got,
353-
Err(ref e) if e.status() == Some(&permanent_status())
354-
),
355-
"{got:?}"
356-
);
357-
}
358-
359305
#[tokio::test]
360306
async fn poller_until_done_error_on_start() {
361307
let start = || async move { Err(Error::service(permanent_status())) };
@@ -446,21 +392,6 @@ mod tests {
446392
assert!(matches!(&got.1, PollingResult::Completed(Ok(_))), "{got:?}");
447393
}
448394

449-
#[test]
450-
fn start_done_with_error() {
451-
let input = TestOperation {
452-
done: true,
453-
status: Some(permanent_status()),
454-
..TestOperation::default()
455-
};
456-
let got = handle_start(Ok(input));
457-
assert!(got.0.is_none(), "{got:?}");
458-
assert!(
459-
matches!(&got.1, PollingResult::Completed(Err(_))),
460-
"{got:?}"
461-
);
462-
}
463-
464395
#[test]
465396
fn start_in_progress() {
466397
let input = TestOperation {
@@ -497,37 +428,20 @@ mod tests {
497428
let input = TestOperation {
498429
done: true,
499430
name: Some("in-progress".into()),
500-
status: None,
501431
..TestOperation::default()
502432
};
503433
let got = handle_poll(Arc::new(policy), &state, "started".to_string(), Ok(input));
504434
assert!(got.0.is_none(), "{got:?}");
505435
assert!(matches!(got.1, PollingResult::Completed(Ok(_))), "{got:?}");
506436
}
507437

508-
#[test]
509-
fn poll_done_error() {
510-
let policy = Aip194Strict;
511-
let state = PollingState::default();
512-
let input = TestOperation {
513-
done: true,
514-
name: Some("in-progress".into()),
515-
status: Some(permanent_status()),
516-
..TestOperation::default()
517-
};
518-
let got = handle_poll(Arc::new(policy), &state, "started".to_string(), Ok(input));
519-
assert!(got.0.is_none(), "{got:?}");
520-
assert!(matches!(got.1, PollingResult::Completed(Err(_))), "{got:?}");
521-
}
522-
523438
#[test]
524439
fn poll_in_progress() {
525440
let policy = Aip194Strict;
526441
let state = PollingState::default();
527442
let input = TestOperation {
528443
done: false,
529444
name: Some("in-progress".into()),
530-
status: None,
531445
..TestOperation::default()
532446
};
533447
let got = handle_poll(Arc::new(policy), &state, "started".to_string(), Ok(input));
@@ -568,39 +482,8 @@ mod tests {
568482
);
569483
}
570484

571-
#[test]
572-
fn polling_done() {
573-
let input = TestOperation {
574-
status: Some(transient_status()),
575-
..TestOperation::default()
576-
};
577-
let got = handle_polling_done(input);
578-
assert!(
579-
matches!(&got, PollingResult::Completed(Err(e)) if e.status() == Some(&transient_status())),
580-
"{got:?}"
581-
);
582-
583-
let input = TestOperation {
584-
status: None,
585-
..TestOperation::default()
586-
};
587-
let got = handle_polling_done(input);
588-
assert!(matches!(&got, PollingResult::Completed(Ok(_))), "{got:?}");
589-
}
590-
591485
#[test]
592486
fn polling_success() {
593-
let input = TestOperation {
594-
status: Some(transient_status()),
595-
..TestOperation::default()
596-
};
597-
let got = handle_polling_success(input);
598-
assert!(got.0.is_none(), "{got:?}");
599-
assert!(
600-
matches!(&got.1, PollingResult::Completed(Err(e)) if e.status() == Some(&transient_status())),
601-
"{got:?}"
602-
);
603-
604487
let input = TestOperation {
605488
name: Some("in-progress".to_string()),
606489
..TestOperation::default()
@@ -645,7 +528,6 @@ mod tests {
645528
struct TestOperation {
646529
done: bool,
647530
name: Option<String>,
648-
status: Option<gax::error::rpc::Status>,
649531
value: Option<i32>,
650532
}
651533

@@ -656,8 +538,5 @@ mod tests {
656538
fn name(&self) -> Option<&String> {
657539
self.name.as_ref()
658540
}
659-
fn status(&self) -> Option<Status> {
660-
self.status.clone()
661-
}
662541
}
663542
}

0 commit comments

Comments
 (0)