Skip to content
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

Make all single-row database calls return an Option. #4617

Merged
merged 1 commit into from
Apr 16, 2024

Conversation

dessalines
Copy link
Member

@dessalines dessalines commented Apr 11, 2024

- Diesel ordinarily throws an error when no results are returned for a
  single fetch, which is a bit confusing. This PR ensures that the
  missing value cases are all caught, and wrapped with new LemmyErrors,
  rather than diesel errors.
- Fixes #4601
@dessalines dessalines changed the title Make all single-fetch database calls return an Option. Make all single-row database calls return an Option. Apr 11, 2024
.filter(community::actor_id.eq(object_id))
.first::<Community>(conn)
.await
.ok()
Copy link
Member Author

@dessalines dessalines Apr 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Nutomic these were the hacks I was referring to. Without this ok and map, it would've thrown an error.

Was able to get rid of this by using .optional()

let community = Community::read(&mut context.pool(), community_id).await?;
let community = Community::read(&mut context.pool(), community_id)
.await?
.ok_or(LemmyErrorType::CouldntFindCommunity)?;
Copy link
Member

@Nutomic Nutomic Apr 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to do this ok_or inside of Community::read, instead of adding it to every single callsite. Same for person, comment etc.

Copy link
Member Author

@dessalines dessalines Apr 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather keep LemmyErrors and diesel errors separate, otherwise we'd have to redo all the functions in db_schema. And one of the main points of this was to allow call sites to do their own handling of the option returned. For 90% of cases, the None / missing value can be turned into a lemmy CouldntFind error, but some of them don't convert it to an error, and might just choose to ignore the None case.

I know its more verbose, but I think its more flexible to move all of the missing-single-row errors out of a diesel error, and give the option to convert them to lemmy errors everywhere else.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need separate errors for CouldntFindCommunity, CouldntFindPost etc? Why dont we use a single error NotFound instead? Then we can add a much shorter conversion method Option.to_err().

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I second @Nutomic's suggestion. There are already a staggering number of error types, so I'd rather avoid adding more than is necessary.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A single NotFound error would be a good idea if api endpoints only try to read one type of thing that could throw the error

Copy link
Member Author

@dessalines dessalines Apr 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There were already a few CouldntFindX errors, probably for cases where you go to a community or person and it can't find it.

Anyway I already did the work, so I don't see the point of going from specific errors to less specific errors.

Also we could simplify them to NotFound in a later PR, when we do breaking changes, because I believe removing the existing ones would be a breaking change.

@Nutomic Nutomic merged commit d075acc into main Apr 16, 2024
2 checks passed
@SleeplessOne1917 SleeplessOne1917 deleted the convert_firsts_to_optionals branch April 16, 2024 13:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add .optional() to database fetches that shouldn't throw an error for zero-result single-row-fetches.
4 participants