You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi! I Have an endpoint that returns the known sensors of a network switch and I am confused about the outcome.
The docs state that an Resut::Err should expect an Outcome::Erorr from the endpoint, but it's not what I am getting. I tried to use a Responder enum, but I an unsuccessful.
Here is the endpoin:
#[get("/gateway/<gateway_id>/sensors")]
async fn get_nodes(
gateway_id: ObjectIdDto,
gateway_col: &State<Arc<Collection<GatewayModel>>>,
nodes_col: &State<Arc<Collection<NodeModel>>>,
registry_col: &State<Arc<Collection<RegistryModel>>>,
auth: Auth
) -> Result<Json<Vec<NodeDto>>, Error> {
let gateway = match find_gateway_for_user(&gateway_col, &gateway_id.0, auth.id()).await {
Ok(value) => value,
Err(fail_status) => return Err(fail_status), // Propagate error upwards
};
let footprints = match registry_col.distinct("digital_footprint", doc! {"gateway_key": &gateway.key}).await {
Ok(value) => value,
Err(_) => return Err(Error::Internal(ErrorResponse::new("Connection error", "Unable to connect to the database cluster"))),
};
let mut nodes = match nodes_col.find(doc! {"gateway_key": &gateway.key, "digital_footprint": {"$in": &footprints}}).await {
Ok(cursor) => cursor.filter_map(|result| async { match result.ok() {
Some(value) => Some(NodeDto::from(value)),
None => None
}}).collect::<Vec<NodeDto>>().await,
Err(err) => {
error!("{:?}", err);
return Err(Error::Internal(ErrorResponse::new("Connection error", "Unable to connect to the database cluster")));
}
};
let nodes = task::spawn(async move {
let footprints = footprints.into_iter().filter_map(|bson_value|{
if let Bson::String(s) = bson_value {
Some(s)
} else {
None
}
}).collect::<HashSet<String>>();
info!("{:?}", footprints);
let default_nodes: Vec<NodeDto> = footprints
.into_iter()
.filter(|fp| !gateway.known_nodes.contains(fp))
.map(|dfp| NodeDto::from(NodeModel::new(dfp, &gateway.key, None, None)))
.collect();
nodes.extend(default_nodes);
nodes
}).await.unwrap_or_else(|e| {
error!("Series collection failed: {:?}", e);
vec![]
});
Ok(Json(nodes)) // Return a successful JSON response here
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi! I Have an endpoint that returns the known sensors of a network switch and I am confused about the outcome.
The docs state that an
Resut::Err
should expect anOutcome::Erorr
from the endpoint, but it's not what I am getting. I tried to use aResponder
enum, but I an unsuccessful.Here is the endpoin:
And here is the Erorr enum:
What am I doing wrong here?
Beta Was this translation helpful? Give feedback.
All reactions