Skip to content

Commit baa65e4

Browse files
committed
almost done
1 parent c6cdaf5 commit baa65e4

File tree

7 files changed

+46
-127
lines changed

7 files changed

+46
-127
lines changed

catalyst-toolbox/src/bin/cli/advisor_reviews/mod.rs

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,14 @@
11
use catalyst_toolbox::utils;
2-
use catalyst_toolbox::vca_reviews::{read_vca_reviews_aggregated_file, Error as ReviewsError};
2+
use catalyst_toolbox::vca_reviews::read_vca_reviews_aggregated_file;
33

4+
use color_eyre::eyre::bail;
5+
use color_eyre::Report;
46
use jcli_lib::utils::io::open_file_write;
57
use std::fmt;
68
use std::path::PathBuf;
79
use std::str::FromStr;
810
use structopt::StructOpt;
911

10-
#[derive(thiserror::Error, Debug)]
11-
pub enum Error {
12-
#[error(transparent)]
13-
Review(#[from] ReviewsError),
14-
15-
#[error("Error while serializing reviews to json")]
16-
SerializeToJson(#[from] serde_json::Error),
17-
18-
#[error("Error while serializing reviews to csv")]
19-
SerializeToCsv(#[from] csv::Error),
20-
21-
#[error("Invalid output format {0}. Only 'csv' and 'json' are supported")]
22-
InvalidFormat(String),
23-
24-
#[error(transparent)]
25-
Io(#[from] std::io::Error),
26-
}
27-
2812
#[derive(Debug)]
2913
pub enum OutputFormat {
3014
Csv,
@@ -50,13 +34,13 @@ pub struct Export {
5034
}
5135

5236
impl FromStr for OutputFormat {
53-
type Err = Error;
37+
type Err = Report;
5438

5539
fn from_str(s: &str) -> Result<Self, Self::Err> {
5640
match s.to_lowercase().as_str() {
5741
"csv" => Ok(Self::Csv),
5842
"json" => Ok(Self::Json),
59-
other => Err(Error::InvalidFormat(other.to_string())),
43+
other => bail!("invalid format: {other}"),
6044
}
6145
}
6246
}
@@ -68,7 +52,7 @@ impl fmt::Display for OutputFormat {
6852
}
6953

7054
impl Reviews {
71-
pub fn exec(self) -> Result<(), Error> {
55+
pub fn exec(self) -> Result<(), Report> {
7256
match self {
7357
Reviews::Export(transform) => transform.exec()?,
7458
};
@@ -77,7 +61,7 @@ impl Reviews {
7761
}
7862

7963
impl Export {
80-
pub fn exec(self) -> Result<(), Error> {
64+
pub fn exec(self) -> Result<(), Report> {
8165
let Self { from, to, format } = self;
8266

8367
let reviews = read_vca_reviews_aggregated_file(&from)?;

catalyst-toolbox/src/bin/cli/logs/compare.rs

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use catalyst_toolbox::logs::compare::{compare_logs, LogCmpStats};
2-
use catalyst_toolbox::logs::sentry;
32
use catalyst_toolbox::logs::sentry::{RawLog, SentryFragmentLog};
43
use chain_core::property::Fragment;
4+
use color_eyre::Report;
55
use jcli_lib::utils::io;
66
use jormungandr_lib::interfaces::{
77
load_persistent_fragments_logs_from_folder_path, PersistentFragmentLog,
@@ -10,24 +10,6 @@ use serde::de::DeserializeOwned;
1010
use std::path::PathBuf;
1111
use structopt::StructOpt;
1212

13-
#[derive(thiserror::Error, Debug)]
14-
pub enum Error {
15-
#[error("api-token parameter must be provided if sentry-url is set")]
16-
MissingTokenParameter,
17-
18-
#[error(transparent)]
19-
SentryLogs(#[from] sentry::Error),
20-
21-
#[error(transparent)]
22-
Io(#[from] std::io::Error),
23-
24-
#[error("error deserializing logs from: {path}")]
25-
Deserialize {
26-
path: PathBuf,
27-
source: serde_json::Error,
28-
},
29-
}
30-
3113
#[derive(StructOpt)]
3214
#[structopt(rename_all = "kebab-case")]
3315
pub struct Compare {
@@ -39,7 +21,7 @@ pub struct Compare {
3921
}
4022

4123
impl Compare {
42-
pub fn exec(self) -> Result<(), Error> {
24+
pub fn exec(self) -> Result<(), Report> {
4325
let Self {
4426
sentry_logs,
4527
permanent_logs,
@@ -95,9 +77,9 @@ impl Compare {
9577
}
9678
}
9779

98-
pub fn load_logs_from_file<L: DeserializeOwned>(path: PathBuf) -> Result<Vec<L>, Error> {
99-
let reader = io::open_file_read(&Some(path.clone()))?;
100-
serde_json::from_reader(reader).map_err(|e| Error::Deserialize { path, source: e })
80+
pub fn load_logs_from_file<L: DeserializeOwned>(path: PathBuf) -> Result<Vec<L>, Report> {
81+
let reader = io::open_file_read(&Some(path))?;
82+
Ok(serde_json::from_reader(reader)?)
10183
}
10284

10385
pub fn print_results(results: &LogCmpStats) {

catalyst-toolbox/src/bin/cli/notifications/send.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use catalyst_toolbox::notifications::{
77
Request, RequestData,
88
},
99
send::send_create_message,
10-
Error,
1110
};
11+
use color_eyre::Report;
1212
use jcli_lib::utils::io;
1313

1414
use reqwest::Url;
@@ -82,7 +82,7 @@ pub enum SendNotification {
8282
}
8383

8484
impl Args {
85-
pub fn exec(self) -> Result<(), Error> {
85+
pub fn exec(self) -> Result<(), Report> {
8686
let url = self.api_params.api_url.join("createMessage").unwrap();
8787
let message = self.build_create_message()?;
8888
let request = Request::new(RequestData::CreateMessageRequest(message));
@@ -92,7 +92,7 @@ impl Args {
9292
Ok(())
9393
}
9494

95-
pub fn build_create_message(&self) -> Result<CreateMessage, Error> {
95+
pub fn build_create_message(&self) -> Result<CreateMessage, Report> {
9696
let content: ContentType = serde_json::from_str(&self.content_path.get_content()?)?;
9797
let mut content_builder = ContentSettingsBuilder::new()
9898
.with_timezone(self.timezone.clone())
@@ -115,7 +115,7 @@ impl Args {
115115
}
116116

117117
impl Json {
118-
pub fn exec(self) -> Result<(), Error> {
118+
pub fn exec(self) -> Result<(), Report> {
119119
let url = self.api_url.join("createMessage").unwrap();
120120
let message_data: RequestData = serde_json::from_str(&self.json_path.get_content()?)?;
121121
let request: Request = Request::new(message_data);
@@ -127,7 +127,7 @@ impl Json {
127127
}
128128

129129
impl SendNotification {
130-
pub fn exec(self) -> Result<(), Error> {
130+
pub fn exec(self) -> Result<(), Report> {
131131
match self {
132132
SendNotification::FromArgs(args) => args.exec(),
133133
SendNotification::FromJson(json) => json.exec(),
@@ -136,8 +136,8 @@ impl SendNotification {
136136
}
137137

138138
impl Content {
139-
pub fn get_content(&self) -> Result<String, Error> {
140-
let mut reader = io::open_file_read(&self.content_path).map_err(Error::FileError)?;
139+
pub fn get_content(&self) -> Result<String, Report> {
140+
let mut reader = io::open_file_read(&self.content_path)?;
141141
let mut result = String::new();
142142
reader.read_to_string(&mut result)?;
143143
Ok(result)

catalyst-toolbox/src/notifications/mod.rs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,3 @@ pub mod requests;
22
pub mod responses;
33
pub mod send;
44

5-
use thiserror::Error;
6-
7-
#[allow(clippy::large_enum_variant)]
8-
#[derive(Debug, Error)]
9-
pub enum Error {
10-
#[error(transparent)]
11-
CreateMessageError(#[from] requests::create_message::Error),
12-
13-
#[error(transparent)]
14-
SerdeError(#[from] serde_json::Error),
15-
16-
#[error(transparent)]
17-
RequestError(#[from] reqwest::Error),
18-
19-
#[error("error reading file, source: {0}")]
20-
FileError(#[from] std::io::Error),
21-
22-
#[error("sent data is invalid:\n {request}")]
23-
BadDataSent { request: String },
24-
25-
#[error("request was unsuccessful, feedback:\n {response}")]
26-
UnsuccessfulRequest { response: String },
27-
}

catalyst-toolbox/src/notifications/requests/create_message.rs

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,14 @@
1+
use color_eyre::{
2+
eyre::{bail, eyre},
3+
Report,
4+
};
15
use serde::{Deserialize, Serialize};
26

37
use std::collections::HashMap;
48
use time::{format_description::FormatItem, macros::format_description, OffsetDateTime};
59

6-
use thiserror::Error;
7-
810
pub const DATETIME_FMT: &[FormatItem] = format_description!("[year]-[month]-[day] [hour]:[minute]");
911

10-
#[allow(clippy::large_enum_variant)]
11-
#[derive(Debug, Error)]
12-
pub enum Error {
13-
#[error("could not build {object_name:?}, missing field {field_name:?}")]
14-
MissingFieldOnBuilderError {
15-
object_name: String,
16-
field_name: String,
17-
},
18-
19-
#[error("CreateMessage should contain at least one ContentSettings entry")]
20-
EmptyContentSettingsError,
21-
}
22-
2312
pub type MultiLanguageContent = HashMap<String, String>;
2413

2514
#[derive(Serialize, Deserialize)]
@@ -123,13 +112,12 @@ impl ContentSettingsBuilder {
123112
self
124113
}
125114

126-
pub fn build(self) -> Result<ContentSettings, Error> {
115+
pub fn build(self) -> Result<ContentSettings, Report> {
116+
let content = self.content.ok_or_else(|| eyre!("missing field content"))?;
117+
127118
Ok(ContentSettings {
128119
send_date: self.send_date,
129-
content: self.content.ok_or(Error::MissingFieldOnBuilderError {
130-
object_name: "ContentSettings".to_string(),
131-
field_name: "content".to_string(),
132-
})?,
120+
content,
133121
ignore_user_timezones: self.ignore_user_timezones,
134122
timezone: self.timezone,
135123
campaign: self.campaign,
@@ -167,19 +155,19 @@ impl CreateMessageBuilder {
167155
self
168156
}
169157

170-
pub fn build(self) -> Result<CreateMessage, Error> {
158+
pub fn build(self) -> Result<CreateMessage, Report> {
171159
if self.notifications.is_empty() {
172-
return Err(Error::EmptyContentSettingsError);
160+
bail!("empty content settings");
173161
}
162+
163+
let auth = self.auth.ok_or_else(|| eyre!("missing field auth"))?;
164+
let application = self
165+
.application
166+
.ok_or_else(|| eyre!("missing field application"))?;
167+
174168
Ok(CreateMessage {
175-
auth: self.auth.ok_or(Error::MissingFieldOnBuilderError {
176-
object_name: "CreateMessage".to_string(),
177-
field_name: "auth".to_string(),
178-
})?,
179-
application: self.application.ok_or(Error::MissingFieldOnBuilderError {
180-
object_name: "CreateMessage".to_string(),
181-
field_name: "application".to_string(),
182-
})?,
169+
auth,
170+
application,
183171
notifications: self.notifications,
184172
})
185173
}

catalyst-toolbox/src/notifications/send.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1+
use color_eyre::{Report, eyre::bail};
12
use reqwest::{blocking::Client, StatusCode, Url};
23

3-
use crate::notifications::{
4-
requests::Request, responses::create_message::CreateMessageResponse, Error,
5-
};
4+
use crate::notifications::{requests::Request, responses::create_message::CreateMessageResponse};
65

76
pub fn send_create_message(
87
url: Url,
98
notification: &Request,
10-
) -> Result<CreateMessageResponse, Error> {
9+
) -> Result<CreateMessageResponse, Report> {
1110
let client = Client::new();
1211
let response = client
1312
.post(url)
@@ -16,14 +15,12 @@ pub fn send_create_message(
1615
match response.status() {
1716
StatusCode::OK => {}
1817
StatusCode::BAD_REQUEST => {
19-
return Err(Error::BadDataSent {
20-
request: serde_json::to_string_pretty(&notification)?,
21-
})
18+
let request = serde_json::to_string_pretty(&notification)?;
19+
bail!("bad request: {request:?}")
2220
}
2321
_ => {
24-
return Err(Error::UnsuccessfulRequest {
25-
response: response.text()?,
26-
})
22+
let response = response.text()?;
23+
bail!("unsuccessful request: {response}");
2724
}
2825
};
2926
let response_message = response.json()?;

catalyst-toolbox/src/vca_reviews/mod.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
11
use crate::community_advisors::models::AdvisorReviewRow;
22
use crate::utils;
3+
use color_eyre::Report;
34
use vit_servicing_station_lib::db::models::community_advisors_reviews::AdvisorReview;
45

56
use std::path::Path;
6-
7-
#[derive(thiserror::Error, Debug)]
8-
pub enum Error {
9-
#[error(transparent)]
10-
CouldNotReadCsv(#[from] csv::Error),
11-
12-
#[error("Couldn't parse advisor review tag for question: {0}")]
13-
CouldntParseTag(String),
14-
}
15-
167
impl AdvisorReviewRow {
178
fn as_advisor_review(&self) -> AdvisorReview {
189
AdvisorReview {
@@ -30,7 +21,7 @@ impl AdvisorReviewRow {
3021
}
3122
}
3223

33-
pub fn read_vca_reviews_aggregated_file(filepath: &Path) -> Result<Vec<AdvisorReview>, Error> {
24+
pub fn read_vca_reviews_aggregated_file(filepath: &Path) -> Result<Vec<AdvisorReview>, Report> {
3425
Ok(
3526
utils::csv::load_data_from_csv::<AdvisorReviewRow, b','>(filepath)?
3627
.into_iter()

0 commit comments

Comments
 (0)