Skip to content

Commit ddefb46

Browse files
committed
refactor: Move to useing anyhow::Result errors (cleaner)
Signed-off-by: Rachel Powers <[email protected]>
1 parent d96e940 commit ddefb46

File tree

12 files changed

+54
-58
lines changed

12 files changed

+54
-58
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Cargo.lock
33

44
.env
55
/config
6+
/logs
67

78
# downloaded metadata during development
89
meta

mcmeta/.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
RUST_LOG=INFO
2+
13
MCMETA__BIND_ADDRESS=127.0.0.1:9988
24

35
MCMETA__STORAGE_FORMAT__TYPE=json

mcmeta/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ edition = "2021"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9+
anyhow = "1.0.70"
910
argparse = "0.2.2"
1011
axum = "0.6.15"
1112
clap = "4.2.1"
1213
config = "0.13.3"
13-
custom_error = "1.9.2"
1414
dotenv = "0.15.0"
1515
futures = "0.3.28"
1616
git2 = "0.17.0"
@@ -21,6 +21,7 @@ serde = { version = "1.0.160", features = ["derive"] }
2121
serde_json = "1.0.95"
2222
serde_valid = "0.15.0"
2323
serde_with = "2.3.2"
24+
thiserror = "1.0.40"
2425
tokio = { version = "1.27.0", features = ["full"] }
2526
tracing = "0.1.37"
2627
tracing-appender = "0.2.2"

mcmeta/src/app_config.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1+
use anyhow::Result;
12
use serde::Deserialize;
23

3-
use crate::errors::MetaMCError;
4-
54
#[derive(Deserialize, Debug)]
65
#[serde(rename_all = "snake_case", tag = "type")]
76
pub enum StorageFormat {
@@ -31,7 +30,7 @@ pub struct ServerConfig {
3130
}
3231

3332
impl ServerConfig {
34-
pub fn from_config(path: &str) -> Result<Self, MetaMCError> {
33+
pub fn from_config(path: &str) -> Result<Self> {
3534
let config = config::Config::builder()
3635
.set_default("bind_address", "127.0.0.1:8080")?
3736
.set_default("storage_format.type", "json")?

mcmeta/src/download/errors.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,24 @@
11
use crate::utils::get_json_context_back;
2-
use custom_error::custom_error;
2+
use thiserror::Error;
33

4-
custom_error! {
5-
pub MetadataError
6-
Config { source: config::ConfigError } = "Error while reading config from environment",
7-
Request { source: reqwest::Error } = "Request error: {source}",
8-
Deserialization { source: serde_json::Error } = "Deserialization error: {source}",
4+
#[derive(Error, Debug)]
5+
pub enum MetadataError {
6+
#[error("Unable to deserialise object at {line}:{column}. Context `{ctx}` \n\nCaused by:\n\t{source}")]
97
BadData {
108
ctx: String,
11-
source: serde_json::Error
12-
} = @{
13-
format!("{}. Context at {}:{} (may be truncated) \" {} \"", source, source.line(), source.column(), ctx)
9+
line: usize,
10+
column: usize,
11+
source: serde_json::Error,
1412
},
15-
Validation { source: serde_valid::validation::Errors } = "Validation Error: {source}",
1613
}
1714

1815
impl MetadataError {
1916
pub fn from_json_err(err: serde_json::Error, body: &str) -> Self {
20-
match err.classify() {
21-
serde_json::error::Category::Data => Self::BadData {
22-
ctx: get_json_context_back(&err, body, 200),
23-
source: err,
24-
},
25-
_ => err.into(),
17+
Self::BadData {
18+
ctx: get_json_context_back(&err, body, 200),
19+
line: err.line(),
20+
column: err.column(),
21+
source: err,
2622
}
2723
}
2824
}

mcmeta/src/download/forge.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use tracing::debug;
55

66
use crate::download::errors::MetadataError;
77

8+
use anyhow::Result;
9+
810
fn default_maven_url() -> String {
911
"https://files.minecraftforge.net/net/minecraftforge/forge/maven-metadata.json".to_string()
1012
}
@@ -22,7 +24,7 @@ struct DownloadConfig {
2224
}
2325

2426
impl DownloadConfig {
25-
fn from_config() -> Result<Self, MetadataError> {
27+
fn from_config() -> Result<Self> {
2628
let config = config::Config::builder()
2729
.add_source(config::Environment::with_prefix("MCMETA_FORGE"))
2830
.build()?;
@@ -31,7 +33,7 @@ impl DownloadConfig {
3133
}
3234
}
3335

34-
pub async fn load_maven_metadata() -> Result<ForgeMavenMetadata, MetadataError> {
36+
pub async fn load_maven_metadata() -> Result<ForgeMavenMetadata> {
3537
let client = reqwest::Client::new();
3638
let config = DownloadConfig::from_config()?;
3739

@@ -49,7 +51,7 @@ pub async fn load_maven_metadata() -> Result<ForgeMavenMetadata, MetadataError>
4951
Ok(metadata)
5052
}
5153

52-
pub async fn load_maven_promotions() -> Result<ForgeMavenPromotions, MetadataError> {
54+
pub async fn load_maven_promotions() -> Result<ForgeMavenPromotions> {
5355
let client = reqwest::Client::new();
5456
let config = DownloadConfig::from_config()?;
5557

mcmeta/src/download/mojang.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use serde::Deserialize;
33
use serde_valid::Validate;
44
use tracing::debug;
55

6+
use anyhow::Result;
7+
68
use crate::download::errors::MetadataError;
79

810
fn default_download_url() -> String {
@@ -16,7 +18,7 @@ struct DownloadConfig {
1618
}
1719

1820
impl DownloadConfig {
19-
fn from_config() -> Result<Self, MetadataError> {
21+
fn from_config() -> Result<Self> {
2022
let config = config::Config::builder()
2123
.add_source(config::Environment::with_prefix("MCMETA_MOJANG"))
2224
.build()?;
@@ -25,7 +27,7 @@ impl DownloadConfig {
2527
}
2628
}
2729

28-
pub async fn load_manifest() -> Result<MojangVersionManifest, MetadataError> {
30+
pub async fn load_manifest() -> Result<MojangVersionManifest> {
2931
let client = reqwest::Client::new();
3032
let config = DownloadConfig::from_config()?;
3133

@@ -43,7 +45,7 @@ pub async fn load_manifest() -> Result<MojangVersionManifest, MetadataError> {
4345
Ok(manifest)
4446
}
4547

46-
pub async fn load_version_manifest(version_url: &str) -> Result<MinecraftVersion, MetadataError> {
48+
pub async fn load_version_manifest(version_url: &str) -> Result<MinecraftVersion> {
4749
let client = reqwest::Client::new();
4850

4951
debug!("Fetching version manifest from {:#?}", version_url);

mcmeta/src/errors.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1 @@
1-
use crate::download::errors::MetadataError;
2-
use custom_error::custom_error;
31

4-
custom_error! {
5-
pub MetaMCError
6-
MojangMetadata { source: MetadataError } = "Error while downloading metadata: {source}",
7-
Config { source: config::ConfigError } = "Error while reading config from environment",
8-
Parse { source: std::net::AddrParseError } = "Error while parsing address: {source}",
9-
Hyper { source: hyper::Error } = "Error while running Hyper: {source}",
10-
IO { source: std::io::Error } = "Error while reading or writing: {source}",
11-
Json { source: serde_json::Error } = "Error while serializing or deserializing JSON: {source}",
12-
Join { source: tokio::task::JoinError } = "Thread join error: {source}",
13-
}

mcmeta/src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use axum::{routing::get, Extension, Router};
55

66
use tracing::{debug, info};
77

8+
use anyhow::Result;
89
use argparse::{ArgumentParser, Store};
910
use dotenv::dotenv;
1011
use tracing_subscriber::{filter, prelude::*};
@@ -17,7 +18,7 @@ mod storage;
1718
mod utils;
1819

1920
#[tokio::main]
20-
async fn main() -> Result<(), errors::MetaMCError> {
21+
async fn main() -> Result<()> {
2122
dotenv().ok(); // This line loads the environment variables from the ".env" file.
2223

2324
let mut config_path = "".to_string();

mcmeta/src/storage/mod.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1+
use crate::{app_config::MetadataConfig, app_config::StorageFormat};
2+
use anyhow::Result;
13
use tracing::info;
24

3-
use crate::{app_config::MetadataConfig, app_config::StorageFormat, errors::MetaMCError};
4-
55
mod mojang;
66

77
impl StorageFormat {
8-
pub async fn initialize_metadata(
9-
&self,
10-
metadata_cfg: &MetadataConfig,
11-
) -> Result<(), MetaMCError> {
8+
pub async fn initialize_metadata(&self, metadata_cfg: &MetadataConfig) -> Result<()> {
129
match self {
1310
StorageFormat::Json { meta_directory } => {
1411
let metadata_dir = std::path::Path::new(meta_directory);

0 commit comments

Comments
 (0)