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

Improve error handling in client #37

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
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
12 changes: 9 additions & 3 deletions healthpi-api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::str::FromStr;
use actix_cors::Cors;
use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder};
use healthpi_model::measurement::{Record, ValueType};
use log::info;
use log::{error, info};
use serde::{de, Deserialize};

use crate::db::{
Expand Down Expand Up @@ -49,8 +49,14 @@ async fn post_measurements(
measurements: web::Json<Vec<Record>>,
) -> impl Responder {
match measurement_repository.store_records(measurements.0).await {
Ok(_) => HttpResponse::Created().json(()),
Err(_) => HttpResponse::InternalServerError().json(()),
Ok(_) => {
info!("Successfully stored records");
HttpResponse::Created().json(())
}
Err(e) => {
error!("Failed to store records: {e}");
HttpResponse::InternalServerError().json(())
}
}
}

Expand Down
49 changes: 41 additions & 8 deletions healthpi-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ use itertools::Itertools;

#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("failed to communicate with the server")]
ServerError,
#[error("server unreachable")]
CommunicationError,
#[error("internal server error")]
InternalServerError,
#[error("invalid request")]
RequestError,
#[error("incorrect server response")]
ResponseError,
}

type Result<T> = std::result::Result<T, Error>;
Expand Down Expand Up @@ -43,10 +49,19 @@ impl Client for ClientImpl {
.get(&self.url)
.send()
.await
.map_err(|_| Error::ServerError)?
.map_err(|_| Error::CommunicationError)
.and_then(|resp| {
if resp.status().is_client_error() {
Err(Error::RequestError)
} else if resp.status().is_server_error() {
Err(Error::InternalServerError)
} else {
Ok(resp)
}
})?
.json()
.await
.map_err(|_| Error::ServerError)
.map_err(|_| Error::ResponseError)
}

async fn get_records_with_value_types(&self, types: &[ValueType]) -> Result<Vec<Record>> {
Expand All @@ -58,10 +73,19 @@ impl Client for ClientImpl {
])
.send()
.await
.map_err(|_| Error::ServerError)?
.map_err(|_| Error::CommunicationError)
.and_then(|resp| {
if resp.status().is_client_error() {
Err(Error::RequestError)
} else if resp.status().is_server_error() {
Err(Error::InternalServerError)
} else {
Ok(resp)
}
})?
.json()
.await
.map_err(|_| Error::ServerError)
.map_err(|_| Error::ResponseError)
}

async fn post_records(&self, records: &[Record]) -> Result<()> {
Expand All @@ -70,9 +94,18 @@ impl Client for ClientImpl {
.json(&records)
.send()
.await
.map_err(|_| Error::ServerError)?
.map_err(|_| Error::CommunicationError)
.and_then(|resp| {
if resp.status().is_client_error() {
Err(Error::RequestError)
} else if resp.status().is_server_error() {
Err(Error::InternalServerError)
} else {
Ok(resp)
}
})?
.json()
.await
.map_err(|_| Error::ServerError)
.map_err(|_| Error::ResponseError)
}
}
2 changes: 1 addition & 1 deletion healthpi-loader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl Loader {

info!("Storing records in database");
if let Err(e) = self.api_client.post_records(&records).await {
error!("Failed to store records in database, skipping. {}", e);
error!("Failed to store records in database, skipping: {}", e);
continue;
}

Expand Down
Loading