Skip to content

Commit

Permalink
refactor: use anyhow less for better error handling (#404)
Browse files Browse the repository at this point in the history
  • Loading branch information
dyc3 committed Jul 21, 2024
1 parent 5a484b2 commit aa0a13c
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 22 deletions.
8 changes: 5 additions & 3 deletions steamguard/src/accountlinker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ where
&self.tokens
}

pub fn link(&mut self) -> anyhow::Result<AccountLinkSuccess, AccountLinkError> {
pub fn link(&mut self) -> Result<AccountLinkSuccess, AccountLinkError> {
let access_token = self.tokens.access_token();
let steam_id = access_token
.decode()
Expand Down Expand Up @@ -99,7 +99,7 @@ where
time: u64,
account: &mut SteamGuardAccount,
confirm_code: String,
) -> anyhow::Result<(), FinalizeLinkError> {
) -> Result<(), FinalizeLinkError> {
let code = account.generate_code(time);

let token = self.tokens.access_token();
Expand Down Expand Up @@ -133,7 +133,7 @@ where
pub fn query_status(
&self,
account: &SteamGuardAccount,
) -> anyhow::Result<CTwoFactor_Status_Response> {
) -> Result<CTwoFactor_Status_Response, TransportError> {
let mut req = CTwoFactor_Status_Request::new();
req.set_steamid(account.steam_id);

Expand Down Expand Up @@ -327,6 +327,8 @@ pub enum FinalizeLinkError {
WantMore { server_time: u64 },
#[error("Steam returned an unexpected error code: {0:?}")]
UnknownEResult(EResult),
#[error("Transport error: {0}")]
TransportError(#[from] TransportError),
#[error(transparent)]
Unknown(#[from] anyhow::Error),
}
Expand Down
22 changes: 13 additions & 9 deletions steamguard/src/steamapi/authentication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ where
pub fn begin_auth_session_via_credentials(
&mut self,
req: CAuthentication_BeginAuthSessionViaCredentials_Request_BinaryGuardData,
) -> anyhow::Result<ApiResponse<CAuthentication_BeginAuthSessionViaCredentials_Response>> {
) -> Result<ApiResponse<CAuthentication_BeginAuthSessionViaCredentials_Response>, TransportError>
{
let req = ApiRequest::new(SERVICE_NAME, "BeginAuthSessionViaCredentials", 1u32, req);
let resp = self.transport.send_request::<
CAuthentication_BeginAuthSessionViaCredentials_Request_BinaryGuardData,
Expand All @@ -42,7 +43,7 @@ where
pub fn begin_auth_session_via_qr(
&mut self,
req: CAuthentication_BeginAuthSessionViaQR_Request,
) -> anyhow::Result<ApiResponse<CAuthentication_BeginAuthSessionViaQR_Response>> {
) -> Result<ApiResponse<CAuthentication_BeginAuthSessionViaQR_Response>, TransportError> {
let req = ApiRequest::new(SERVICE_NAME, "BeginAuthSessionViaQR", 1u32, req);
let resp = self
.transport
Expand All @@ -56,7 +57,7 @@ where
&mut self,
req: CAuthentication_AccessToken_GenerateForApp_Request,
access_token: &Jwt,
) -> anyhow::Result<ApiResponse<CAuthentication_AccessToken_GenerateForApp_Response>> {
) -> Result<ApiResponse<CAuthentication_AccessToken_GenerateForApp_Response>, TransportError> {
let req = ApiRequest::new(SERVICE_NAME, "GenerateAccessTokenForApp", 1u32, req)
.with_access_token(access_token);
let resp = self
Expand All @@ -70,7 +71,7 @@ where
pub fn fetch_rsa_key(
&mut self,
account_name: String,
) -> anyhow::Result<ApiResponse<CAuthentication_GetPasswordRSAPublicKey_Response>> {
) -> Result<ApiResponse<CAuthentication_GetPasswordRSAPublicKey_Response>, TransportError> {
let mut inner = CAuthentication_GetPasswordRSAPublicKey_Request::new();
inner.set_account_name(account_name);
let req = ApiRequest::new(SERVICE_NAME, "GetPasswordRSAPublicKey", 1u32, inner);
Expand All @@ -86,7 +87,7 @@ where
pub fn migrate_mobile_session(
&mut self,
req: CAuthentication_MigrateMobileSession_Request,
) -> anyhow::Result<ApiResponse<CAuthentication_MigrateMobileSession_Response>> {
) -> Result<ApiResponse<CAuthentication_MigrateMobileSession_Response>, TransportError> {
let req = ApiRequest::new(SERVICE_NAME, "MigrateMobileSession", 1u32, req);
let resp = self
.transport
Expand All @@ -99,7 +100,7 @@ where
pub fn poll_auth_session(
&mut self,
req: CAuthentication_PollAuthSessionStatus_Request,
) -> anyhow::Result<ApiResponse<CAuthentication_PollAuthSessionStatus_Response>> {
) -> Result<ApiResponse<CAuthentication_PollAuthSessionStatus_Response>, TransportError> {
let req = ApiRequest::new(SERVICE_NAME, "PollAuthSessionStatus", 1u32, req);
let resp = self
.transport
Expand All @@ -112,7 +113,7 @@ where
pub fn revoke_refresh_token(
&mut self,
req: CAuthentication_RefreshToken_Revoke_Request,
) -> anyhow::Result<ApiResponse<CAuthentication_RefreshToken_Revoke_Response>> {
) -> Result<ApiResponse<CAuthentication_RefreshToken_Revoke_Response>, TransportError> {
let req = ApiRequest::new(SERVICE_NAME, "RevokeRefreshToken", 1u32, req);
let resp = self
.transport
Expand All @@ -125,7 +126,7 @@ where
pub fn revoke_access_token(
&mut self,
req: CAuthenticationSupport_RevokeToken_Request,
) -> anyhow::Result<ApiResponse<CAuthenticationSupport_RevokeToken_Response>> {
) -> Result<ApiResponse<CAuthenticationSupport_RevokeToken_Response>, TransportError> {
let req = ApiRequest::new(SERVICE_NAME, "RevokeToken", 1u32, req);
let resp = self
.transport
Expand Down Expand Up @@ -161,7 +162,10 @@ where
pub fn update_session_with_steam_guard_code(
&mut self,
req: CAuthentication_UpdateAuthSessionWithSteamGuardCode_Request,
) -> anyhow::Result<ApiResponse<CAuthentication_UpdateAuthSessionWithSteamGuardCode_Response>> {
) -> Result<
ApiResponse<CAuthentication_UpdateAuthSessionWithSteamGuardCode_Response>,
TransportError,
> {
let req = ApiRequest::new(
SERVICE_NAME,
"UpdateAuthSessionWithSteamGuardCode",
Expand Down
4 changes: 2 additions & 2 deletions steamguard/src/steamapi/twofactor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ where
&self,
req: CTwoFactor_AddAuthenticator_Request,
access_token: &Jwt,
) -> anyhow::Result<ApiResponse<CTwoFactor_AddAuthenticator_Response>> {
) -> Result<ApiResponse<CTwoFactor_AddAuthenticator_Response>, TransportError> {
let req = ApiRequest::new(SERVICE_NAME, "AddAuthenticator", 1, req)
.with_access_token(access_token);
let resp = self
Expand All @@ -43,7 +43,7 @@ where
&self,
req: CTwoFactor_FinalizeAddAuthenticator_Request,
access_token: &Jwt,
) -> anyhow::Result<ApiResponse<CTwoFactor_FinalizeAddAuthenticator_Response>> {
) -> Result<ApiResponse<CTwoFactor_FinalizeAddAuthenticator_Response>, TransportError> {
let req = ApiRequest::new(SERVICE_NAME, "FinalizeAddAuthenticator", 1, req)
.with_access_token(access_token);
let resp = self
Expand Down
2 changes: 1 addition & 1 deletion steamguard/src/transport/webapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Transport for WebApiTransport {
fn send_request<Req: BuildableRequest + MessageFull, Res: MessageFull>(
&self,
apireq: ApiRequest<Req>,
) -> anyhow::Result<ApiResponse<Res>, TransportError> {
) -> Result<ApiResponse<Res>, TransportError> {
// All the API endpoints accept 2 data formats: json and protobuf.
// Depending on the http method for the request, the data can go in 2 places:
// - GET: query string, with the key `input_protobuf_encoded` or `input_json`
Expand Down
26 changes: 19 additions & 7 deletions steamguard/src/userlogin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::refresher::TokenRefresher;
use crate::steamapi::authentication::AuthenticationClient;
use crate::steamapi::EResult;
use crate::token::Tokens;
use crate::transport::Transport;
use crate::transport::{Transport, TransportError};
use anyhow::Context;
use base64::Engine;
use log::*;
Expand All @@ -30,6 +30,7 @@ pub enum LoginError {
TooManyAttempts,
UnknownEResult(EResult),
AuthAlreadyStarted,
TransportError(TransportError),
NetworkFailure(reqwest::Error),
OtherFailure(anyhow::Error),
}
Expand All @@ -42,6 +43,12 @@ impl std::fmt::Display for LoginError {

impl std::error::Error for LoginError {}

impl From<TransportError> for LoginError {
fn from(err: TransportError) -> Self {
LoginError::TransportError(err)
}
}

impl From<reqwest::Error> for LoginError {
fn from(err: reqwest::Error) -> Self {
LoginError::NetworkFailure(err)
Expand Down Expand Up @@ -108,7 +115,7 @@ where
&mut self,
account_name: &str,
password: &str,
) -> anyhow::Result<Vec<AllowedConfirmation>, LoginError> {
) -> Result<Vec<AllowedConfirmation>, LoginError> {
if self.started_auth.is_some() {
return Err(LoginError::AuthAlreadyStarted);
}
Expand Down Expand Up @@ -146,7 +153,7 @@ where
.collect())
}

pub fn begin_auth_via_qr(&mut self) -> anyhow::Result<BeginQrLoginResponse, LoginError> {
pub fn begin_auth_via_qr(&mut self) -> Result<BeginQrLoginResponse, LoginError> {
if self.started_auth.is_some() {
return Err(LoginError::AuthAlreadyStarted);
}
Expand Down Expand Up @@ -248,10 +255,8 @@ where
&mut self,
guard_type: EAuthSessionGuardType,
code: String,
) -> anyhow::Result<
CAuthentication_UpdateAuthSessionWithSteamGuardCode_Response,
UpdateAuthSessionError,
> {
) -> Result<CAuthentication_UpdateAuthSessionWithSteamGuardCode_Response, UpdateAuthSessionError>
{
let Some(started_auth) = self.started_auth.as_ref() else {
return Err(UpdateAuthSessionError::SessionNotStarted);
};
Expand Down Expand Up @@ -387,6 +392,7 @@ pub enum UpdateAuthSessionError {
SessionExpired,
IncorrectSteamGuardCode,
UnknownEResult(EResult),
TransportError(TransportError),
NetworkFailure(reqwest::Error),
OtherFailure(anyhow::Error),
}
Expand All @@ -410,6 +416,12 @@ impl From<EResult> for UpdateAuthSessionError {
}
}

impl From<TransportError> for UpdateAuthSessionError {
fn from(err: TransportError) -> Self {
UpdateAuthSessionError::TransportError(err)
}
}

impl From<reqwest::Error> for UpdateAuthSessionError {
fn from(err: reqwest::Error) -> Self {
UpdateAuthSessionError::NetworkFailure(err)
Expand Down

0 comments on commit aa0a13c

Please sign in to comment.