Skip to content

Commit b3e37e8

Browse files
committed
cmd: custom exit codes for internal, resource and canceled errors
Signed-off-by: Tonis Tiigi <[email protected]>
1 parent 1436f93 commit b3e37e8

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

cmd/buildx/main.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ import (
1414
"github.com/docker/cli/cli-plugins/plugin"
1515
"github.com/docker/cli/cli/command"
1616
"github.com/docker/cli/cli/debug"
17-
"github.com/moby/buildkit/solver/errdefs"
17+
solvererrdefs "github.com/moby/buildkit/solver/errdefs"
18+
"github.com/moby/buildkit/util/grpcerrors"
1819
"github.com/moby/buildkit/util/stack"
1920
"github.com/pkg/errors"
2021
"go.opentelemetry.io/otel"
22+
"google.golang.org/grpc/codes"
2123

2224
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
2325

@@ -99,7 +101,7 @@ func main() {
99101
os.Exit(sterr.StatusCode)
100102
}
101103

102-
for _, s := range errdefs.Sources(err) {
104+
for _, s := range solvererrdefs.Sources(err) {
103105
s.Print(cmd.Err())
104106
}
105107
if debug.IsEnabled() {
@@ -113,5 +115,19 @@ func main() {
113115
ebr.Print(cmd.Err())
114116
}
115117

116-
os.Exit(1)
118+
exitCode := 1
119+
switch grpcerrors.Code(err) {
120+
case codes.Internal:
121+
exitCode = 100 // https://github.com/square/exit/blob/v1.3.0/exit.go#L70
122+
case codes.ResourceExhausted:
123+
exitCode = 102
124+
case codes.Canceled:
125+
exitCode = 130
126+
default:
127+
if errors.Is(err, context.Canceled) {
128+
exitCode = 130
129+
}
130+
}
131+
132+
os.Exit(exitCode)
117133
}

0 commit comments

Comments
 (0)