-
Notifications
You must be signed in to change notification settings - Fork 3
Error<Err>
type pattern prevent building wrapping abstraction
#4
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
Comments
Usually, I'd expect users to choose their own type and stick through it — be it a As for what you could do, the thing I'd do on my end would probably be to also take I'm not sure I understand your question with all its specificities, but hopefully this answers your question? BTW, if you're trying to abstract SQLite and IndexedDb, you may be interested in the project I'm working on right now: sakuhiki, an abstraction layer for KV stores like indexed-db, rocksdb or tikv 😄 |
Oh and also: the reason why I'm using a But… maybe that'd be a lesser evil? I'm actually rethinking it, especially now that I'll have to think about releasing a 0.5 with #5. WDYT about this alternative? It might be easier to handle for you too? |
I don't have enough experience on this part, so I might be missing some important tradeoff here compared to my current needs ^^
I'd rather have each call to This way it would be easier to have per-method business error: async fn add_user(id: u32, name: String) -> AddUserError { ... }
async fn get_user(id: u32) -> Restul<String, GetUserError> {
db.transaction(&["data"]).run(async move |t| {
match t.get(&id).await? {
Some(user) => Ok(user),
None => Err(GetUserError::NotFound),
}
})
.await
.map_err(|e| GetUserError::DatabaseError(e))?
// Typically with `DatabaseError(#[transparent] anyhow::Error)`
} (on top of being able to write middleware wrapper as my original post was showcasing) |
Hmm so I'm still thinking of returning a That's because this way I can have the |
I agree the biggest issue for me is the fact the error type is binded to the Database object. On the other hand, having to do something like is fine: async fn get_user(id: u32) -> Restul<String, GetUserError> {
db.transaction(&["data"]).run(async move |t| {
match t.get(&id).await? {
Some(user) => Ok(user),
None => Err(indexed_db::Error::Custom(GetUserError::NotFound)),
}
})
.await
.map_err(|e| match e {
indexed_db::Error::Custom(e) => e
e => GetUserError::DatabaseError(e)
})
} |
Got it! I'll go forward with this soon then; plus thanks to the |
I just killed as much as possible while still keeping the I also took advantage of that to move object store creation from the Can you confirm b3380b0 looks good to you, and I can close this issue? |
Having to specify the custom error type in the
Database
brings a drawback: it is not possible to have multiple methods using different custom error.This is typically an issue when wrapping the database to abstract it1, consider the following:
A solution to overcome this issue is to rely on type erasing:
However this is rather cumbersome, and force to Box the error...
Do you see any better approach for this ?
Footnotes
I'm doing this to have a codebase using SQLite on native and IndexedDb on web ↩
The text was updated successfully, but these errors were encountered: