Skip to content

Commit

Permalink
Implement response.json locally to print response text for error
Browse files Browse the repository at this point in the history
  • Loading branch information
madmikeross committed Dec 21, 2023
1 parent b7ce814 commit 44dec71
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 13 deletions.
47 changes: 41 additions & 6 deletions src/esi.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use chrono::{DateTime, Utc};
use reqwest::{Client, Error, Response};
use serde::{Deserialize, Serialize};
use thiserror::Error;
use warp::http;

use crate::esi::RequestError::HttpError;
use crate::esi::RequestError::ParseError;

#[derive(Debug, Deserialize)]
pub struct SystemEsiResponse {
Expand Down Expand Up @@ -48,25 +53,55 @@ pub struct Destination {
pub async fn get_system_details(
client: &Client,
system_id: i64,
) -> Result<SystemEsiResponse, Error> {
) -> Result<SystemEsiResponse, RequestError> {
let system_detail_url = format!(
"https://esi.evetech.net/latest/universe/systems/{}",
system_id
);
let response = client.get(&system_detail_url).send().await?;
response.json().await
response.json().await.map_err(HttpError)
}

#[derive(Error, Debug)]
pub enum RequestError {
#[error("failed to retrieve data from the source")]
HttpError(#[from] Error),
#[error("failed to parse data")]
ParseError(#[from] serde_json::Error),
}

pub async fn get_stargate_details(
client: &Client,
stargate_id: i64,
) -> Result<StargateEsiResponse, Error> {
) -> Result<StargateEsiResponse, RequestError> {
let stargate_url = format!(
"https://esi.evetech.net/latest/universe/stargates/{}",
stargate_id
);
let response = client.get(&stargate_url).send().await?;
response.json().await
let status_code = response.status();

// Manually implement response.json so we can preserve bytes if we need to understand the error
let response_bytes = response.bytes().await?;
match serde_json::from_slice::<StargateEsiResponse>(&response_bytes).map_err(ParseError) {
Ok(parsed_stargate) => Ok(parsed_stargate),
Err(err) => {
// Rebuild a response so we can print the text
let response = Response::from(
http::Response::builder()
.status(status_code)
.body(response_bytes)
.expect("Failed to rebuild response"),
);
println!(
"{} {}: {}",
status_code,
stargate_url,
response.text().await.unwrap()
);
Err(err)
}
}
}

pub async fn get_system_ids(client: &Client) -> Result<Vec<i64>, Error> {
Expand All @@ -89,7 +124,7 @@ pub struct SystemKills {
pub system_id: i64,
}

pub async fn get_system_kills(client: &Client) -> Result<SystemKillsResponse, Error> {
pub async fn get_system_kills(client: &Client) -> Result<SystemKillsResponse, RequestError> {
let system_kills_url = "https://esi.evetech.net/latest/universe/system_kills/";
let response = client.get(system_kills_url).send().await?;
let last_modified = get_last_modified_date(&response);
Expand Down Expand Up @@ -128,7 +163,7 @@ pub struct SystemJumps {
pub system_id: i64,
}

pub async fn get_system_jumps(client: &Client) -> Result<SystemJumpsResponse, Error> {
pub async fn get_system_jumps(client: &Client) -> Result<SystemJumpsResponse, RequestError> {
let system_jumps_url = "https://esi.evetech.net/latest/universe/system_jumps/";
let response = client.get(system_jumps_url).send().await?;
let last_modified = get_last_modified_date(&response);
Expand Down
9 changes: 5 additions & 4 deletions src/eve_scout.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use reqwest::Client;
use serde::{Deserialize, Serialize};

use crate::esi::RequestError;
use crate::esi::RequestError::HttpError;

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct EveScoutSignature {
id: String,
Expand Down Expand Up @@ -32,10 +35,8 @@ pub struct EveScoutSignature {
comment: Option<String>,
}

pub async fn get_public_signatures(
client: Client,
) -> Result<Vec<EveScoutSignature>, reqwest::Error> {
pub async fn get_public_signatures(client: Client) -> Result<Vec<EveScoutSignature>, RequestError> {
let get_public_signatures = format!("https://api.eve-scout.com/v2/public/signatures");
let response = client.get(&get_public_signatures).send().await?;
response.json().await
response.json().await.map_err(HttpError)
}
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use warp::{reply, Filter, Rejection, Reply};
use crate::database::*;
use crate::esi::{
get_stargate_details, get_system_details, get_system_ids, get_system_jumps, get_system_kills,
StargateEsiResponse, SystemEsiResponse,
RequestError, StargateEsiResponse, SystemEsiResponse,
};
use crate::eve_scout::get_public_signatures;
use crate::ReplicationError::TargetError;
Expand Down Expand Up @@ -263,8 +263,8 @@ impl From<SystemEsiResponse> for System {

#[derive(Error, Debug)]
enum ReplicationError {
#[error("failed to retrieve data from the source")]
SourceError(#[from] reqwest::Error),
#[error("failed to retrieve the data")]
SourceError(#[from] RequestError),
#[error("failed to process the data")]
ProcessError(#[from] JoinError),
#[error("failed to persist data to the target")]
Expand Down

0 comments on commit 44dec71

Please sign in to comment.