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

[Enhancement]: extension method to throw on errors for the MsSqlContainer #1280

Open
riezebosch opened this issue Oct 15, 2024 · 0 comments
Open
Labels
enhancement New feature or request

Comments

@riezebosch
Copy link

riezebosch commented Oct 15, 2024

Problem

When setting up an environment for integration tests I found out that I was lacking feedback because the ExecScriptAsync silently fails.

Solution

I've created this small extension method to enhance the ExecScriptAsync a little:

public static async Task<ExecResult> ThrowOnError(this Task<ExecResult> task)
{
    var result = await task;
    if (result.ExitCode != 0)
    {
        throw new Exception(result.Stderr);
    }
    return result;
}

Benefit

It makes working with query (in your integration tests) a bit easier because you get immediate feedback:

await connection.ExecScriptAsync($"""
                                  CREATE TABLE {table} (
                                    Id int NOT NULL PRIMARY KEY IDENTITY(1, 1),
                                    Data varchar(8001)
                                  );
                                  """).ThrowOnError();

Output:

System.Exception: Msg 131, Level 15, State 2, Server 85186a74865e, Line 3

System.Exception
Msg 131, Level 15, State 2, Server 85186a74865e, Line 3
The size (8001) given to the column 'Data' exceeds the maximum allowed for any data type (8000).

Alternatives

make the ExecScriptAsync fail or have an overload to do so.

Would you like to help contributing this enhancement?

Yes

@riezebosch riezebosch added the enhancement New feature or request label Oct 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant