Result in filter_map_async
#697
-
I have the filter for user what makes query to DB. I love that
My current approach looks like this:
But it makes two queries in DB which twice more than I'd feel comfortable. If I use following approach:
It will lead a user to a wrong flow on casual error in db request. It would be nice to have something like this:
Where |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I think this was already answered in telegram, but I'll answer it here too, so other users can find this too. One way to handle these three cases (db error, some data, no data) is to first add it to the context with type FetchRes = Result<Option<User>, Error>;
async fn fetch_user(/* args */) -> FetchRes { ... }
Update::filter_message()
.enter_dialogue::<Message, storage::SqliteStorage, DialogueState>()
.map_async(fetch_user)
// Filter `Ok(Some(_))`
.branch(filter_map(|r: FetchRes| r.ok().flatten()).endpoint(with_user))
// Filter `Ok(None)`
.branch(filter(|r: FetchRes| matches!(r, Ok(None)).endpoint(without_user))
// Fallback for `Err(_)`
.endpoint(db_error) Hope that helps! |
Beta Was this translation helpful? Give feedback.
I think this was already answered in telegram, but I'll answer it here too, so other users can find this too.
One way to handle these three cases (db error, some data, no data) is to first add it to the context with
map
and then filter the result. So it may look something like this: