Skip to content

Commit

Permalink
migrated to using crate::BoxedError instead of `Box<dyn std::error:…
Browse files Browse the repository at this point in the history
…:Error + Send + Sync>`
  • Loading branch information
CommanderStorm committed Jan 24, 2024
1 parent ebc065f commit 9c1eb7c
Show file tree
Hide file tree
Showing 10 changed files with 18 additions and 16 deletions.
2 changes: 2 additions & 0 deletions server/feedback/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use actix_cors::Cors;
use actix_governor::{GlobalKeyExtractor, Governor, GovernorConfigBuilder};
use std::collections::HashMap;
use std::error::Error;

use crate::tokens::RecordedTokens;
use actix_web::{get, middleware, web, App, HttpResponse, HttpServer};
Expand All @@ -12,6 +13,7 @@ mod github;
mod post_feedback;
mod proposed_edits;
mod tokens;
type BoxedError=Box<dyn Error + Send + Sync>;

const MAX_JSON_PAYLOAD: usize = 1024 * 1024; // 1 MB

Expand Down
5 changes: 2 additions & 3 deletions server/feedback/src/proposed_edits/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use serde::Deserialize;
use serde::Serialize;
use std::cmp::max;
use std::collections::BTreeMap;
use std::error;
use std::fs::File;
use std::path::{Path, PathBuf};

Expand Down Expand Up @@ -57,7 +56,7 @@ impl Image {
}
}
}
fn save_metadata(&self, key: &str, image_dir: &Path) -> Result<(), Box<dyn error::Error>> {
fn save_metadata(&self, key: &str, image_dir: &Path) -> Result<(), crate::BoxedError> {
let file = File::open(image_dir.join("img-sources.yaml"))?;
let mut image_sources =
serde_yaml::from_reader::<_, BTreeMap<String, BTreeMap<u32, Source>>>(file)?;
Expand All @@ -80,7 +79,7 @@ impl Image {
+ 1;
image_dir.join(format!("{key}_{next_free_slot}.webp"))
}
fn save_content(&self, target: &Path) -> Result<(), Box<dyn error::Error>> {
fn save_content(&self, target: &Path) -> Result<(), crate::BoxedError> {
let bytes = BASE64_STANDARD.decode(&self.content)?;
let image = image::load_from_memory(&bytes)?;

Expand Down
2 changes: 1 addition & 1 deletion server/feedback/src/proposed_edits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl EditRequest {
async fn apply_changes_and_generate_description(
&self,
branch_name: &str,
) -> Result<String, Box<dyn std::error::Error>> {
) -> Result<String, crate::BoxedError> {
let repo = TempRepo::clone_and_checkout(GIT_URL, branch_name).await?;
let desc = repo.apply_and_gen_description(self);
repo.commit(&desc.title).await?;
Expand Down
7 changes: 3 additions & 4 deletions server/feedback/src/proposed_edits/tmp_repo.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::proposed_edits::EditRequest;
use log::{debug, info};
use std::error;
use tokio::process::Command;

use crate::proposed_edits::discription::Description;
Expand All @@ -13,7 +12,7 @@ impl TempRepo {
pub async fn clone_and_checkout(
url: &'static str,
branch_name: &str,
) -> Result<Self, Box<dyn error::Error>> {
) -> Result<Self, crate::BoxedError> {
let dir = tempfile::tempdir()?;

info!("Cloning {url} into {dir:?}");
Expand Down Expand Up @@ -60,7 +59,7 @@ impl TempRepo {
description
}

pub async fn commit(&self, title: &str) -> Result<(), Box<dyn error::Error>> {
pub async fn commit(&self, title: &str) -> Result<(), crate::BoxedError> {
let out = Command::new("git")
.current_dir(&self.dir)
.arg("add")
Expand All @@ -82,7 +81,7 @@ impl TempRepo {
_ => Err(format!("git commit failed with output: {out:?}").into()),
}
}
pub async fn push(&self) -> Result<(), Box<dyn error::Error>> {
pub async fn push(&self) -> Result<(), crate::BoxedError> {
let out = Command::new("git")
.current_dir(&self.dir)
.arg("status")
Expand Down
3 changes: 2 additions & 1 deletion server/main-api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use sqlx::postgres::PgPoolOptions;
use sqlx::prelude::*;
use sqlx::PgPool;
use std::collections::HashMap;
use std::error::Error;
use structured_logger::async_json::new_writer;
use structured_logger::Builder;

Expand Down Expand Up @@ -52,7 +53,7 @@ fn connection_string() -> String {
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
async fn main() -> Result<(), crate::BoxedError> {
Builder::with_level("info")
.with_target_writer("*", new_writer(tokio::io::stdout()))
.init();
Expand Down
2 changes: 1 addition & 1 deletion server/main-api/src/maps/fetch_tile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl FetchTileTask {
async fn download_map_image(
&self,
file: &std::path::PathBuf,
) -> Result<web::Bytes, Box<dyn std::error::Error>> {
) -> Result<web::Bytes, crate::BoxedError> {
let url = self.get_tileserver_url();
let res = reqwest::get(&url).await?.bytes().await?;

Expand Down
2 changes: 1 addition & 1 deletion server/main-api/src/setup/database/alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl Alias {

pub(crate) async fn load_all_to_db(
tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
) -> Result<(), Box<dyn std::error::Error>> {
) -> Result<(), crate::BoxedError> {
let cdn_url = std::env::var("CDN_URL").unwrap_or_else(|_| "https://nav.tum.de/cdn".to_string());
let raw_aliase = reqwest::get(format!("{cdn_url}/api_data.json"))
.await?
Expand Down
2 changes: 1 addition & 1 deletion server/main-api/src/setup/database/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl DelocalisedValues {
}
pub(crate) async fn load_all_to_db(
tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
) -> Result<(), Box<dyn std::error::Error>> {
) -> Result<(), crate::BoxedError> {
let start = Instant::now();
let cdn_url = std::env::var("CDN_URL").unwrap_or_else(|_| "https://nav.tum.de/cdn".to_string());
let tasks = reqwest::get(format!("{cdn_url}/api_data.json"))
Expand Down
5 changes: 3 additions & 2 deletions server/main-api/src/setup/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ mod data;

use log::info;

pub(crate) async fn setup_database(pool: &sqlx::PgPool) -> Result<(), Box<dyn std::error::Error>> {
pub(crate) async fn setup_database(pool: &sqlx::PgPool) -> Result<(), crate::BoxedError> {
info!("setting up the database");
sqlx::migrate!("./migrations").run(pool).await?;
info!("migrations complete");

Expand All @@ -14,7 +15,7 @@ pub(crate) async fn setup_database(pool: &sqlx::PgPool) -> Result<(), Box<dyn st
}
async fn load_data(
tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
) -> Result<(), Box<dyn std::error::Error>> {
) -> Result<(), crate::BoxedError> {
info!("deleting old data");
sqlx::query!("DELETE FROM aliases")
.execute(&mut **tx)
Expand Down
4 changes: 2 additions & 2 deletions server/main-api/src/setup/meilisearch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use meilisearch_sdk::tasks::Task;
use meilisearch_sdk::Client;
use serde_json::Value;
use std::collections::HashMap;
use std::error::Error;
use std::time::Duration;

const TIMEOUT: Option<Duration> = Some(Duration::from_secs(20));
Expand Down Expand Up @@ -44,7 +43,8 @@ async fn wait_for_healthy(client: &Client) {
}
}

pub(crate) async fn setup_meilisearch() -> Result<(), Box<dyn Error>> {
pub(crate) async fn setup_meilisearch() -> Result<(), crate::BoxedError> {
info!("setting up meilisearch");
let start = std::time::Instant::now();
let ms_url = std::env::var("MIELI_URL").unwrap_or_else(|_| "http://localhost:7700".to_string());
info!("connecting to Meilisearch at {ms_url}", ms_url = ms_url);
Expand Down

0 comments on commit 9c1eb7c

Please sign in to comment.