#ExitCode of script always returns 0

1 messages · Page 1 of 1 (latest)

outer anchor
#

I have the following code

        execCtr := ctr.
            // WithExec([]string{"bash", "-c", script.String()})
            WithExec([]string{"bash", "-c", "set -euo pipefail; \n" + "exit 3"})

        code, _ = execCtr.ExitCode(rt.Ctx)
        // Seems this is not accurate, the error holds the exit code for sonar
        // This error is for failures in the dagger engine itself, not the command exit code.
        // if err != nil {
        //     return fmt.Errorf("while getting sonar exit code: %w", err)
        // }

        fmt.Println("Sonar exit code:", code)

Even though this script only exit 3 the last print always has 0

What am I doing wrong here?

elfin minnow
#

for exec errors you need to cast them to ExecError in go.

    var execErr *dagger.ExecError
    _, err := execCtr.ExitCode(context.Background())
    if errors.As(err, &execErr) {
        return execErr.ExitCode, nil
    }
#

cc @outer anchor

#

it used to be documented but I couldn't find it in the docs anymore. cc @boreal magnet

elfin minnow