Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(client): better error reporting on login/registration #2076

Merged
merged 1 commit into from
Jun 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 6 additions & 23 deletions crates/atuin-client/src/api_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,12 @@ pub async fn register(
.json(&map)
.send()
.await?;
let resp = handle_resp_error(resp).await?;

if !ensure_version(&resp)? {
bail!("could not register user due to version mismatch");
}

if !resp.status().is_success() {
let error = resp.json::<ErrorResponse>().await?;
bail!("failed to register user: {}", error.reason);
}

let session = resp.json::<RegisterResponse>().await?;
Ok(session)
}
Expand All @@ -85,18 +81,10 @@ pub async fn login(address: &str, req: LoginRequest) -> Result<LoginResponse> {
.json(&req)
.send()
.await?;

if resp.status() == StatusCode::TOO_MANY_REQUESTS {
bail!("Rate limited. Too many login attempts.");
}
let resp = handle_resp_error(resp).await?;

if !ensure_version(&resp)? {
bail!("could not login due to version mismatch");
}

if resp.status() != reqwest::StatusCode::OK {
let error = resp.json::<ErrorResponse>().await?;
bail!("invalid login details: {}", error.reason);
bail!("Could not login due to version mismatch");
}

let session = resp.json::<LoginResponse>().await?;
Expand All @@ -115,11 +103,7 @@ pub async fn latest_version() -> Result<Version> {
.header(USER_AGENT, APP_USER_AGENT)
.send()
.await?;

if resp.status() != reqwest::StatusCode::OK {
let error = resp.json::<ErrorResponse>().await?;
bail!("failed to check latest version: {}", error.reason);
}
let resp = handle_resp_error(resp).await?;

let index = resp.json::<IndexResponse>().await?;
let version = Version::parse(index.version.as_str())?;
Expand All @@ -136,8 +120,7 @@ pub fn ensure_version(response: &Response) -> Result<bool> {
Err(e) => bail!("failed to parse server version: {:?}", e),
}
} else {
// if there is no version header, then the newest this server can possibly be is 17.1.0
Version::parse("17.1.0")
bail!("Server not reporting its version: it is either too old or unhealthy");
}?;

// If the client is newer than the server
Expand Down Expand Up @@ -170,7 +153,7 @@ async fn handle_resp_error(resp: Response) -> Result<Response> {
let reason = error.reason;

if status.is_client_error() {
bail!("Could not fetch history, client error {status}: {reason}.")
bail!("Invalid request to the service: {status} - {reason}.")
}

bail!("There was an error with the atuin sync service, server error {status}: {reason}.\nIf the problem persists, contact the host")
Expand Down
Loading