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

RFC: add fallible result-based API #606

Open
DDtKey opened this issue Apr 28, 2024 · 3 comments · May be fixed by #636
Open

RFC: add fallible result-based API #606

DDtKey opened this issue Apr 28, 2024 · 3 comments · May be fixed by #636
Assignees
Milestone

Comments

@DDtKey
Copy link
Collaborator

DDtKey commented Apr 28, 2024

At the moment, testcontainers panics in case of any error.
It makes it easier to use, but complicates the flexibility of the code and the ability to customize errors.

I suggest refactoring this and returning meaningful errors. This greatly expands the possibilities of use.

We can preserve panicking API by using a common pattern: providing fallible methods with a try_ prefix and keep existing ones as delegates that unwraps the Result.

For example:

// current api without changes:
let container = GenericImage::new("redis", "7.2.4")
        .with_exposed_port(6379)
        .start()
        .await;
        
// new fallible api
let container = GenericImage::new("redis", "7.2.4")
        .with_exposed_port(6379)
        .try_start()
        .await?; // you can handle the error any way you want
@mervyn-mccreight
Copy link
Contributor

mervyn-mccreight commented May 13, 2024

I'd say we should let the users decide whether they want to handle errors or not.
Meaning that the fallible API could be the only existing API IMO and if the users want to just panic on error they are free to do so.

WDYT?

We might need to find a way to be able to do that API change gracefully with deprecations though.

@DDtKey
Copy link
Collaborator Author

DDtKey commented May 13, 2024

That was just a proposal to start with try_ methods, quite common way to move towards fallible API.
I definitely prefer to have a single aligned fallible API.

Graceful switch is a bit complicated if we want to preserve method names, however I see several ways to do that, mostly by using wrappers, like: FallibleContainer and etc, where the same methods returns Results

I can provide more examples a bit later, but that's definitely possible. However I'm not totally sure if we really need to be crazy about compatibility, because it's anyway breaking change at the end. Probably, good migration guide is enough.
Moreover, it will be perfectly highlighted by linters and compiler - methods will start to return a Result which is unused.

After some major changes we can release version 1.0 and provide more guarantees about compatibilities

@mervyn-mccreight
Copy link
Contributor

However I'm not totally sure if we really need to be crazy about compatibility, because it's anyway breaking change at the end. Probably, good migration guide is enough.

That sounds about right now that I think about it again.

DDtKey added a commit that referenced this issue May 18, 2024
@DDtKey DDtKey self-assigned this May 19, 2024
@DDtKey DDtKey added this to the 0.17.0 milestone May 19, 2024
DDtKey added a commit that referenced this issue May 25, 2024
Closes #606

## Migration guide
- The easiest way to use `unwrap` or `expect` for all `testcontainers` operations.
  - or you can cast error if your tests are already `Result` based (for `anyhow::Result` it's enough to use `?`)
- The `Image::exec_after_start` method returns a `Result`, so if you have an implementation of `Image` that uses `exec_after_start`, it's important to handle possible errors.
@DDtKey DDtKey linked a pull request May 25, 2024 that will close this issue
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 a pull request may close this issue.

2 participants