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

Add derive for EditoastError 🚀 #3148

Merged
merged 2 commits into from
Feb 10, 2023
Merged

Add derive for EditoastError 🚀 #3148

merged 2 commits into from
Feb 10, 2023

Conversation

flomonster
Copy link
Contributor

@flomonster flomonster commented Feb 10, 2023

Description

Add a fancy macro to avoid implementing the trait EditoastError by hand.

Before

use crate::error::EditoastError;

impl EditoastError for ElectricalProfilesError {
    fn get_status(&self) -> StatusCode {
        match self {
            Self::NotFound(_) => StatusCode::NOT_FOUND,
            Self::InternalError(_) => StatusCode::INTERNAL_SERVER_ERROR,
        }
    }

    fn get_type(&self) -> &'static str {
        match self {
            Self::NotFound(_) => "editoast:electrical_profiles:NotFound",
            Self::InternalError(_) => "editoast:electrical_profiles:InternalError",
        }
    }

    fn context(&self) -> Map<String, JsonValue> {
        match self {
            ElectricalProfilesError::NotFound(electrical_profile_set_id) => json!({
                "electrical_profile_set_id": electrical_profile_set_id,
            })
            .as_object()
            .cloned()
            .unwrap(),
            _ => Default::default(),
        }
    }
}

Now

use editoast_derive::EditoastError;

#[derive(Debug, Error, EditoastError)]
#[editoast_error(base_id = "electrical_profiles", context = "Self::context")]
pub enum ElectricalProfilesError {
    /// Couldn't found the infra with the given id
    #[error("Electrical Profile Set '{0}', could not be found")]
    #[editoast_error(status = 404)]
    NotFound(i64),
    #[error("An internal error occurred: '{}'", .0.to_string())]
    #[editoast_error(status = 500)]
    InternalError(String),
}

impl ElectricalProfilesError {
    fn context(&self) -> Map<String, JsonValue> {
        match self {
            ElectricalProfilesError::NotFound(electrical_profile_set_id) => json!({
                "electrical_profile_set_id": electrical_profile_set_id,
            })
            .as_object()
            .cloned()
            .unwrap(),
            _ => Default::default(),
        }
    }
}

@flomonster flomonster requested a review from a team as a code owner February 10, 2023 03:17
@codecov
Copy link

codecov bot commented Feb 10, 2023

Codecov Report

Merging #3148 (c9d4087) into dev (e5a4e52) will decrease coverage by 0.05%.
The diff coverage is 50.00%.

@@             Coverage Diff              @@
##                dev    #3148      +/-   ##
============================================
- Coverage     40.82%   40.77%   -0.05%     
  Complexity     1709     1709              
============================================
  Files           624      624              
  Lines         18608    18549      -59     
  Branches       2314     2314              
============================================
- Hits           7597     7564      -33     
+ Misses        10608    10582      -26     
  Partials        403      403              
Flag Coverage Δ
editoast 71.30% <50.00%> (+0.31%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
editoast/src/infra.rs 67.39% <ø> (-0.70%) ⬇️
editoast/src/schema/operation/mod.rs 45.45% <ø> (-6.93%) ⬇️
editoast/src/views/electrical_profiles.rs 88.09% <ø> (+2.09%) ⬆️
editoast/src/views/infra/edition.rs 90.32% <0.00%> (+10.32%) ⬆️
editoast/src/views/infra/errors.rs 77.55% <ø> (+3.04%) ⬆️
editoast/src/views/infra/objects.rs 100.00% <ø> (ø)
editoast/src/views/infra/pathfinding.rs 75.96% <ø> (+4.14%) ⬆️
editoast/src/views/infra/railjson.rs 84.61% <ø> (+6.04%) ⬆️
editoast/src/views/layers/mod.rs 35.59% <ø> (-3.47%) ⬇️
editoast/src/views/pagination.rs 100.00% <ø> (+28.57%) ⬆️
... and 6 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@flomonster flomonster force-pushed the fam/editoast-error-derive branch 2 times, most recently from bda5b29 to 8328997 Compare February 10, 2023 03:56
bloussou
bloussou previously approved these changes Feb 10, 2023
Copy link
Contributor

@bloussou bloussou left a comment

Choose a reason for hiding this comment

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

Clean !! It is great when lines are removed 🧹

younesschrifi
younesschrifi previously approved these changes Feb 10, 2023
@flomonster flomonster dismissed stale reviews from younesschrifi and bloussou via c9d4087 February 10, 2023 09:45
@flomonster flomonster force-pushed the fam/editoast-error-derive branch from 8328997 to c9d4087 Compare February 10, 2023 09:45
Copy link
Contributor

@leovalais leovalais left a comment

Choose a reason for hiding this comment

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

So nice! More macros! 🥳🥳🥳

@flomonster flomonster merged commit dbf7917 into dev Feb 10, 2023
@flomonster flomonster deleted the fam/editoast-error-derive branch February 10, 2023 15:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants