From 72615139ee0666e1ddcbbc268b6076c2c967850b Mon Sep 17 00:00:00 2001 From: Lorenzo Leonardo <97872577+LorenzoLeonardo@users.noreply.github.com> Date: Wed, 9 Nov 2022 04:55:35 +0800 Subject: [PATCH] Use DeviceCodeErrorResponseType::ExpiredToken for expired Device (#195) It is good to use RequestTokenError::ServerResponse for expired Device token instead of having a "Device code expired" string is set to Other(). We see to it that they are have the same Error with the error came from the endpoint. There was a timing issue found that expired_token is returned from the endpoint and sometimes the RequestTokenError::Other() was returned after the expiration time given by the endpoint. We make it uniform for easy error handling upon use. --- src/lib.rs | 16 ++++++++++++++-- src/tests.rs | 9 ++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 5129cc3..fe55542 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2333,7 +2333,13 @@ where loop { let now = (*self.time_fn)(); if now > timeout_dt { - break Err(RequestTokenError::Other("Device code expired".to_string())); + break Err(RequestTokenError::ServerResponse( + DeviceCodeErrorResponse::new( + DeviceCodeErrorResponseType::ExpiredToken, + Some(String::from("This device code has expired.")), + None, + ), + )); } match self.process_response(http_client(self.prepare_request()?), interval) { @@ -2372,7 +2378,13 @@ where loop { let now = (*self.time_fn)(); if now > timeout_dt { - break Err(RequestTokenError::Other("Device code expired".to_string())); + break Err(RequestTokenError::ServerResponse( + DeviceCodeErrorResponse::new( + DeviceCodeErrorResponseType::ExpiredToken, + Some(String::from("This device code has expired.")), + None, + ), + )); } match self.process_response(http_client(self.prepare_request()?).await, interval) { diff --git a/src/tests.rs b/src/tests.rs index 84d61c2..fb115eb 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -2203,7 +2203,14 @@ fn test_device_token_authorization_timeout() { .err() .unwrap(); match token { - RequestTokenError::Other(msg) => assert_eq!(msg, "Device code expired"), + RequestTokenError::ServerResponse(msg) => assert_eq!( + msg, + DeviceCodeErrorResponse::new( + DeviceCodeErrorResponseType::ExpiredToken, + Some(String::from("This device code has expired.")), + None, + ) + ), _ => unreachable!("Error should be an expiry"), } }