Skip to content

Commit

Permalink
Use Convenience Methods for Creation of Tonic Status
Browse files Browse the repository at this point in the history
  • Loading branch information
wba2hi committed Oct 1, 2024
1 parent 1a98715 commit 48b9f97
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 53 deletions.
2 changes: 1 addition & 1 deletion databroker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ lazy_static = "1.4.0"
thiserror = "1.0.47"

futures = { version = "0.3.28" }
async-trait = "0.1.82"

# VISS
axum = { version = "0.6.20", optional = true, features = ["ws"] }
chrono = { version = "0.4.31", optional = true, features = ["std"] }
uuid = { version = "1.4.1", optional = true, features = ["v4"] }
async-trait = "0.1.82"

# systemd related dependency, only relevant on linux systems
[target.'cfg(target_os = "linux")'.dependencies]
Expand Down
70 changes: 18 additions & 52 deletions databroker/src/grpc/kuksa_val_v2/val.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,30 +99,21 @@ impl proto::val_server::Val for broker::DataBroker {
&self,
_request: tonic::Request<proto::GetValueRequest>,
) -> Result<tonic::Response<proto::GetValueResponse>, tonic::Status> {
Err(tonic::Status::new(
tonic::Code::Unimplemented,
"Unimplemented",
))
Err(tonic::Status::unimplemented("Unimplemented"))
}

async fn get_values(
&self,
_request: tonic::Request<proto::GetValuesRequest>,
) -> Result<tonic::Response<proto::GetValuesResponse>, tonic::Status> {
Err(tonic::Status::new(
tonic::Code::Unimplemented,
"Unimplemented",
))
Err(tonic::Status::unimplemented("Unimplemented"))
}

async fn list_values(
&self,
_request: tonic::Request<proto::ListValuesRequest>,
) -> Result<tonic::Response<proto::ListValuesResponse>, tonic::Status> {
Err(tonic::Status::new(
tonic::Code::Unimplemented,
"Unimplemented",
))
Err(tonic::Status::unimplemented("Unimplemented"))
}

type SubscribeStream = Pin<
Expand Down Expand Up @@ -178,16 +169,11 @@ impl proto::val_server::Val for broker::DataBroker {
let stream = convert_to_proto_stream(stream, size);
Ok(tonic::Response::new(Box::pin(stream)))
}
Err(SubscriptionError::NotFound) => {
Err(tonic::Status::new(tonic::Code::NotFound, "Path not found"))
}
Err(SubscriptionError::InvalidInput) => Err(tonic::Status::new(
tonic::Code::InvalidArgument,
"Invalid Argument",
)),
Err(SubscriptionError::InternalError) => {
Err(tonic::Status::new(tonic::Code::Internal, "Internal Error"))
Err(SubscriptionError::NotFound) => Err(tonic::Status::not_found("Path not found")),
Err(SubscriptionError::InvalidInput) => {
Err(tonic::Status::invalid_argument("Invalid Argument"))
}
Err(SubscriptionError::InternalError) => Err(tonic::Status::internal("Internal Error")),
}
}

Expand All @@ -204,10 +190,7 @@ impl proto::val_server::Val for broker::DataBroker {
&self,
_request: tonic::Request<proto::SubscribeByIdRequest>,
) -> Result<tonic::Response<Self::SubscribeByIdStream>, tonic::Status> {
Err(tonic::Status::new(
tonic::Code::Unimplemented,
"Unimplemented",
))
Err(tonic::Status::unimplemented("Unimplemented"))
}

// Actuate a single actuator
Expand Down Expand Up @@ -268,19 +251,13 @@ impl proto::val_server::Val for broker::DataBroker {
};
}
None => {
return Err(tonic::Status::new(
tonic::Code::InvalidArgument,
return Err(tonic::Status::invalid_argument(
"Signal needs to provide Path or Id",
))
}
};
}
None => {
return Err(tonic::Status::new(
tonic::Code::InvalidArgument,
"No Signal_Id provided",
))
}
None => return Err(tonic::Status::invalid_argument("No Signal_Id provided")),
};
};
return Err(tonic::Status::invalid_argument(
Expand Down Expand Up @@ -421,8 +398,7 @@ impl proto::val_server::Val for broker::DataBroker {
})
.await;
if metadata_response.is_empty() {
Err(tonic::Status::new(
tonic::Code::NotFound,
Err(tonic::Status::not_found(
"Specified root branch does not exist",
))
} else {
Expand All @@ -431,10 +407,7 @@ impl proto::val_server::Val for broker::DataBroker {
}))
}
}
Err(_) => Err(tonic::Status::new(
tonic::Code::InvalidArgument,
"Invalid Pattern Argument",
)),
Err(_) => Err(tonic::Status::invalid_argument("Invalid Pattern Argument")),
}
}

Expand Down Expand Up @@ -714,10 +687,7 @@ impl proto::val_server::Val for broker::DataBroker {
&self,
_request: tonic::Request<proto::GetServerInfoRequest>,
) -> Result<tonic::Response<proto::GetServerInfoResponse>, tonic::Status> {
Err(tonic::Status::new(
tonic::Code::Unimplemented,
"Unimplemented",
))
Err(tonic::Status::unimplemented("Unimplemented"))
}
}

Expand Down Expand Up @@ -849,26 +819,22 @@ async fn get_signal(
match signal {
proto::signal_id::Signal::Path(path) => {
if path.len() > MAX_REQUEST_PATH_LENGTH {
return Err(tonic::Status::new(
tonic::Code::InvalidArgument,
return Err(tonic::Status::invalid_argument(
"The provided path is too long",
));
}
match broker.get_id_by_path(&path).await {
Some(id) => Ok(id),
None => Err(tonic::Status::new(tonic::Code::NotFound, "Path not found")),
None => Err(tonic::Status::not_found("Path not found")),
}
}
proto::signal_id::Signal::Id(id) => match broker.get_metadata(id).await {
Some(_metadata) => Ok(id),
None => Err(tonic::Status::new(tonic::Code::NotFound, "Path not found")),
None => Err(tonic::Status::not_found("Path not found")),
},
}
} else {
Err(tonic::Status::new(
tonic::Code::InvalidArgument,
"No SignalId provided",
))
Err(tonic::Status::invalid_argument("No SignalId provided"))
}
}

Expand Down Expand Up @@ -908,7 +874,7 @@ fn convert_actuation_error_to_status(
broker::ActuationError::OutOfBounds => tonic::Status::out_of_range(message),
broker::ActuationError::UnsupportedType => tonic::Status::invalid_argument(message),
broker::ActuationError::PermissionDenied => tonic::Status::permission_denied(message),
broker::ActuationError::PermissionExpired => tonic::Status::permission_denied(message),
broker::ActuationError::PermissionExpired => tonic::Status::unauthenticated(message),
broker::ActuationError::ProviderNotAvailable => tonic::Status::unavailable(message),
broker::ActuationError::ProviderAlreadyExists => tonic::Status::already_exists(message),
broker::ActuationError::TransmissionFailure => tonic::Status::data_loss(message),
Expand Down

0 comments on commit 48b9f97

Please sign in to comment.