Skip to content

Commit 57785fa

Browse files
authored
enhance(query-planner): use less Into and From traits, to have a cleaner error handling (#731)
1 parent 778e57d commit 57785fa

6 files changed

Lines changed: 15 additions & 75 deletions

File tree

lib/query-planner/src/graph/error.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,5 @@ pub enum GraphError {
1414
#[error("Field named '{0}' was not found in definition name '{1}'")]
1515
FieldDefinitionNotFound(String, String),
1616
#[error("Supergraph state error: {0}")]
17-
SupergraphStateError(Box<SupergraphStateError>),
18-
}
19-
20-
impl From<SupergraphStateError> for GraphError {
21-
fn from(err: SupergraphStateError) -> Self {
22-
GraphError::SupergraphStateError(Box::new(err))
23-
}
17+
SupergraphStateError(#[from] SupergraphStateError),
2418
}

lib/query-planner/src/planner/best.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ impl Candidate {
9595

9696
#[inline]
9797
fn get_tree(&self, graph: &Graph) -> Result<QueryTree, QueryPlanError> {
98-
self.tree
98+
Ok(self
99+
.tree
99100
.get_or_create(|(p, mp)| QueryTree::from_path(graph, &p, mp))
100-
.clone()
101-
.map_err(Into::into)
101+
.clone()?)
102102
}
103103

104104
#[inline]

lib/query-planner/src/planner/error.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ use super::fetch::error::FetchGraphError;
88
#[derive(Debug, Clone, thiserror::Error)]
99
pub enum QueryPlanError {
1010
#[error("FetchGraph error: {0}")]
11-
FetchGraphFailure(Box<FetchGraphError>),
11+
FetchGraphFailure(#[from] FetchGraphError),
1212
#[error("Graph error: {0}")]
13-
GraphFailure(Box<GraphError>),
13+
GraphFailure(#[from] GraphError),
1414
#[error("Root fetch is missing")]
1515
NoRoot,
1616
#[error("Failed to build a plan")]
@@ -24,15 +24,3 @@ pub enum QueryPlanError {
2424
#[error(transparent)]
2525
CancellationError(#[from] CancellationError),
2626
}
27-
28-
impl From<FetchGraphError> for QueryPlanError {
29-
fn from(error: FetchGraphError) -> Self {
30-
QueryPlanError::FetchGraphFailure(Box::new(error))
31-
}
32-
}
33-
34-
impl From<GraphError> for QueryPlanError {
35-
fn from(error: GraphError) -> Self {
36-
QueryPlanError::GraphFailure(Box::new(error))
37-
}
38-
}

lib/query-planner/src/planner/fetch/error.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub enum FetchGraphError {
1010
#[error("Internal Error: {0}")]
1111
Internal(String),
1212
#[error("Graph error: {0}")]
13-
GraphFailure(Box<GraphError>),
13+
GraphFailure(#[from] GraphError),
1414
#[error("Missing FetchStep: {0} {1}")]
1515
MissingStep(usize, String),
1616
#[error("Missing parent of FetchStep: {0}")]
@@ -30,7 +30,7 @@ pub enum FetchGraphError {
3030
#[error("Expected different indexes: {0}")]
3131
SameNodeIndex(usize),
3232
#[error("Failed ot find satisfiable key for @requires: {0}")]
33-
SatisfiableKeyFailure(Box<WalkOperationError>),
33+
SatisfiableKeyFailure(#[from] WalkOperationError),
3434
#[error("Expected a FetchStep with Mutation to have its order defined")]
3535
MutationStepWithNoOrder,
3636
#[error("Index mapping got lost")]
@@ -52,9 +52,3 @@ pub enum FetchGraphError {
5252
#[error(transparent)]
5353
CancellationError(#[from] CancellationError),
5454
}
55-
56-
impl From<GraphError> for FetchGraphError {
57-
fn from(error: GraphError) -> Self {
58-
FetchGraphError::GraphFailure(Box::new(error))
59-
}
60-
}

lib/query-planner/src/planner/fetch/fetch_graph.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1630,8 +1630,7 @@ fn find_satisfiable_key<'a>(
16301630
// as the result of this function is guaranteed to be successful,
16311631
// and fast.
16321632
&CancellationToken::new(),
1633-
)
1634-
.map_err(|err| FetchGraphError::SatisfiableKeyFailure(Box::new(err)))?
1633+
)?
16351634
.is_some()
16361635
{
16371636
return edge_ref

lib/query-planner/src/planner/mod.rs

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -37,52 +37,17 @@ pub struct Planner {
3737
#[derive(Debug, Clone, thiserror::Error)]
3838
pub enum PlannerError {
3939
#[error("failed to initalize relations graph: {0}")]
40-
GraphInitError(Box<GraphError>),
40+
GraphInitError(#[from] GraphError),
4141
#[error("failed to locate operation to execute")]
4242
MissingOperationToExecute,
4343
#[error("walker failed to locate path: {0}")]
44-
PathLocatorError(Box<WalkOperationError>),
44+
PathLocatorError(#[from] WalkOperationError),
4545
#[error("failed to build fetch graph: {0}")]
46-
FailedToConstructFetchGraph(Box<FetchGraphError>),
46+
FailedToConstructFetchGraph(#[from] FetchGraphError),
4747
#[error("failed to build plan: {0}")]
48-
QueryPlanBuildFailed(Box<QueryPlanError>),
49-
#[error("cancelled")]
50-
Cancelled,
51-
#[error("timedout")]
52-
Timedout,
53-
}
54-
55-
impl From<GraphError> for PlannerError {
56-
fn from(value: GraphError) -> Self {
57-
PlannerError::GraphInitError(Box::new(value))
58-
}
59-
}
60-
61-
impl From<WalkOperationError> for PlannerError {
62-
fn from(value: WalkOperationError) -> Self {
63-
PlannerError::PathLocatorError(Box::new(value))
64-
}
65-
}
66-
67-
impl From<FetchGraphError> for PlannerError {
68-
fn from(value: FetchGraphError) -> Self {
69-
PlannerError::FailedToConstructFetchGraph(Box::new(value))
70-
}
71-
}
72-
73-
impl From<QueryPlanError> for PlannerError {
74-
fn from(value: QueryPlanError) -> Self {
75-
PlannerError::QueryPlanBuildFailed(Box::new(value))
76-
}
77-
}
78-
79-
impl From<CancellationError> for PlannerError {
80-
fn from(value: CancellationError) -> Self {
81-
match value {
82-
CancellationError::Cancelled => PlannerError::Cancelled,
83-
CancellationError::TimedOut => PlannerError::Timedout,
84-
}
85-
}
48+
QueryPlanBuildFailed(#[from] QueryPlanError),
49+
#[error(transparent)]
50+
CancellationError(#[from] CancellationError),
8651
}
8752

8853
impl Planner {

0 commit comments

Comments
 (0)