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

refactor/improve error handling #48

Merged
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
3 changes: 2 additions & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export RUST_BACKTRACE := "1"
install_tools:
cargo install sqlx-cli
cargo install cargo-pretty-test
cargo install cargo-watch
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like the name suggests, it allows to autoreload the app in dev when the source code changes


# Install all tools
install_all_tools: install_tools
Expand All @@ -26,7 +27,7 @@ _setup_client_node:

# Dev
dev: _setup_db
cd scheduler && cargo run
cd scheduler && cargo watch -- cargo run

# Prepare offline SQLx
prepare_sqlx:
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions scheduler/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions scheduler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ nettu_scheduler_api = { path = "./crates/api" }
nettu_scheduler_domain = { path = "./crates/domain" }
nettu_scheduler_infra = { path = "./crates/infra" }

anyhow = "1.0"

actix-web = "4.8"

tokio = { version = "1", features = ["full"] }
Expand Down Expand Up @@ -57,9 +59,13 @@ nettu_scheduler_sdk = { path = "./clients/rust" }
unsafe_code = "forbid"

[lints.clippy]
print_stdout = "forbid"
print_err = "forbid"
print_stdout = "deny"
print_err = "deny"
unwrap_used = "deny"
expect_used = "deny"

[workspace.lints.clippy]
print_stdout = "forbid"
print_err = "forbid"
print_stdout = "deny"
print_err = "deny"
unwrap_used = "deny"
expect_used = "deny"
2 changes: 2 additions & 0 deletions scheduler/clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
allow-unwrap-in-tests = true
allow-expect-in-tests = true
Comment on lines +1 to +2
Copy link
Collaborator Author

@GuillaumeDecMeetsMore GuillaumeDecMeetsMore Sep 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still allow unwrap and expect in tests, as we don't need to be "as safe" in them (just like we sometimes use any in TS tests)

2 changes: 1 addition & 1 deletion scheduler/crates/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async-trait = "0.1.42"
rrule = "0.12.0"
chrono = { version = "0.4.19", features = ["serde"] }
chrono-tz = "0.8.1"
anyhow = "1.0.0"
anyhow = "1.0"
jsonwebtoken = "7"
thiserror = "1.0"
tracing = "0.1.25"
Expand Down
4 changes: 2 additions & 2 deletions scheduler/crates/api/src/account/set_account_webhook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ mod tests {
#[actix_web::main]
#[test]
async fn it_rejects_invalid_webhook_url() {
let ctx = setup_context().await;
let ctx = setup_context().await.unwrap();
let bad_uris = vec!["1", "", "test.zzcom", "test.com", "google.com"];
for bad_uri in bad_uris {
let mut use_case = SetAccountWebhookUseCase {
Expand All @@ -114,7 +114,7 @@ mod tests {
#[actix_web::main]
#[test]
async fn it_accepts_valid_webhook_url() {
let ctx = setup_context().await;
let ctx = setup_context().await.unwrap();

let valid_uris = vec!["https://google.com", "https://google.com/v1/webhook"];
for valid_uri in valid_uris {
Expand Down
11 changes: 10 additions & 1 deletion scheduler/crates/api/src/calendar/create_calendar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,15 @@ struct CreateCalendarUseCase {

#[derive(Debug)]
enum UseCaseError {
InternalError,
UserNotFound,
StorageError,
}

impl From<UseCaseError> for NettuError {
fn from(e: UseCaseError) -> Self {
match e {
UseCaseError::InternalError => Self::InternalError,
UseCaseError::StorageError => Self::InternalError,
UseCaseError::UserNotFound => {
Self::NotFound("The requested user was not found.".to_string())
Expand All @@ -92,7 +94,14 @@ impl UseCase for CreateCalendarUseCase {
const NAME: &'static str = "CreateCalendar";

async fn execute(&mut self, ctx: &NettuContext) -> Result<Self::Response, Self::Error> {
let user = match ctx.repos.users.find(&self.user_id).await {
let user = ctx
.repos
.users
.find(&self.user_id)
.await
.map_err(|_| UseCaseError::InternalError)?;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note this ? at the end

It allows to return directly the error. This is equivalent to the following

let user = match ctx
            .repos
            .users
            .find(&self.user_id)
            .await
            .map_err(|_| UseCaseError::InternalError) {
    Ok(user) => user,
    Err(e) => return Err(e), // Early return in the function     
 };


let user = match user {
Some(user) if user.account_id == self.account_id => user,
_ => return Err(UseCaseError::UserNotFound),
};
Expand Down
9 changes: 8 additions & 1 deletion scheduler/crates/api/src/calendar/delete_calendar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,15 @@ pub async fn delete_calendar_controller(

#[derive(Debug)]
pub enum UseCaseError {
InternalError,
NotFound(ID),
UnableToDelete,
}

impl From<UseCaseError> for NettuError {
fn from(e: UseCaseError) -> Self {
match e {
UseCaseError::InternalError => Self::InternalError,
UseCaseError::NotFound(calendar_id) => Self::NotFound(format!(
"The calendar with id: {}, was not found.",
calendar_id
Expand All @@ -81,7 +83,12 @@ impl UseCase for DeleteCalendarUseCase {
const NAME: &'static str = "DeleteCalendar";

async fn execute(&mut self, ctx: &NettuContext) -> Result<Self::Response, Self::Error> {
let calendar = ctx.repos.calendars.find(&self.calendar_id).await;
let calendar = ctx
.repos
.calendars
.find(&self.calendar_id)
.await
.map_err(|_| UseCaseError::InternalError)?;
match calendar {
Some(calendar) if calendar.user_id == self.user_id => ctx
.repos
Expand Down
Loading