testcontainers-go icon indicating copy to clipboard operation
testcontainers-go copied to clipboard

[Enhancement]: Add sentinel errors for different error root-causes

Open audunmo opened this issue 5 months ago • 1 comments

Proposal

I am working on a case where a test requires a testcontainer to spin up, run, and then exit, with various different input parameters. The success or failure of the test depends in part on when and how the container fails, if it does at all. Right now, when I create and run the container request, I can get multiple different types of errors. Some examples:

  1. I can get errors that the container failed to start at all
  2. The container may have exited, with a non-zero status code
  3. My request may be misconfigured in some way

Currently, my best bet for handling the different possible failure scenarios is to string match against the message from err.Error(). This is fairly brittle

What I propose is that the go-testcontainers package instead ought to return sentinel errors which indicate where and how an error occurred. That is, that it returns errors wrapped in constants exported by the package

So, in stead of simply returning err, you'd get something like:

...
return nil, ErrNonZeroExitCode
...

// or

...
return nil, fmt.Errorf("%w <some-details-here>", ErrNonZeroExitCode)
...

This would allow the consumer to match against particular error cases with errors.Is, as well getting more details/more control with errors.As. You could for example encode information like the specific status code, what part of the lifecycle it failed in, or logs right at the moment of failure, and then extract that with error.As

audunmo avatar Jul 01 '25 12:07 audunmo

Looked a little into it. It looks like this is in large part an inherited issue from docker, whose errors are the ones we're seeing

audunmo avatar Jul 29 '25 07:07 audunmo