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

Use rescue to avoid dependency inversion #744

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

carrascoacd
Copy link
Contributor

@carrascoacd carrascoacd commented Jan 13, 2025

Problem

When using Finch is expected to receive a RuntimeError if the connection pool is exhausted, see https://github.com/sneako/finch/blob/main/test/finch_test.exs#L561

This is a problem because in a service that uses a lower tier API than the service itself, a dependency inversion of tiers can happen. For example, a tier 2 service calling a tier 3 service.

The tier inversion happens because we are not rescuing the error and returning it into a tuple, so the operation can work without problems under this situation.

Proposed solution

Encapsulate the error in the error tuple. I'm adding this solution here, but I want to know if you consider that it would be best suited in the Finch code itself. Changing reraise basically.

cc/ @yordis

Copy link
Member

@yordis yordis left a comment

Choose a reason for hiding this comment

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

I am concerned with this one; it would change the public API, I am not saying it is bad, but I must be careful here.

@carrascoacd
Copy link
Contributor Author

What do you mean with changing the public API? As far as I know, the adapter is still returning the error tuple as usual but with a new reason.

Could you provide me with an example?

@yordis
Copy link
Member

yordis commented Jan 24, 2025

{:error, {:runtime_error, e}}

That is a new pattern matching that must be taken into consideration. I just do not know what assumptions people made, which leads me to:

e in RuntimeError ->

What would happen if somebody was expecting such RuntimeError exception btw?

@carrascoacd
Copy link
Contributor Author

Regarding the pattern matching, we could change it to a raw string.

What would happen if somebody was expecting such RuntimeError exception btw?

Good question. In my opinion, returning a RuntimeError is breaking the contract the Adapter defines, that us basically a ok or error tuple. This is already breaking the contract in fact. If you use Mint adapter you should be able to replace it with Finch adapter without changing your codebase. This is basically the typical substitution principle.

But I get your point, that's what I'm no sure if this should be added in the Finch library instead. The fact is that, this adapter is not valid for production usage as it is (without catching the error).

Let me know if you prefer to close this and move this PR yo Finch instead.

Thanks!

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.

2 participants