Skip to content

Commit 4c39a35

Browse files
asalkeldtjholm
authored andcommitted
fix: improve error messages when waiting for container
1 parent d73464a commit 4c39a35

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

pkg/codeconfig/collect.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030

3131
v1 "github.com/nitrictech/apis/go/nitric/v1"
3232
"github.com/nitrictech/newcli/pkg/containerengine"
33+
"github.com/nitrictech/newcli/pkg/utils"
3334
)
3435

3536
func ImageNameFromExt(ext string) string {
@@ -109,18 +110,20 @@ func Collect(ctx string, handler string, stack *Stack) error {
109110
waitChan, cErrChan := ce.ContainerWait(cID, container.WaitConditionNextExit)
110111
select {
111112
case done := <-waitChan:
112-
if done.Error != nil || done.StatusCode != 0 {
113-
fmt.Printf("error executing container (code %d) %v\n", done.StatusCode, done.Error)
113+
msg := ""
114+
if done.Error != nil {
115+
msg = done.Error.Message
114116
}
115-
case cErr := <-cErrChan:
116-
if err != nil {
117-
fmt.Printf("error waiting for container %v\n", cErr)
117+
if msg != "" || done.StatusCode != 0 {
118+
err = utils.WrapError(err, fmt.Errorf("error executing container (code %d) %s", done.StatusCode, msg))
118119
}
120+
case cErr := <-cErrChan:
121+
err = utils.WrapError(err, cErr)
119122
}
120123

121124
// 3 - When the container exits stop the server
122125
grpcSrv.Stop()
123-
err = <-errChan
126+
err = utils.WrapError(err, <-errChan)
124127

125128
// 4 - Add the function to the stack
126129
stack.AddFunction(fun)

pkg/utils/errors.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright Nitric Pty Ltd.
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at:
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
package utils
18+
19+
import "github.com/pkg/errors"
20+
21+
// WrapError is just a convienance function to prevent checking of nils.
22+
func WrapError(errFirst, errSecond error) error {
23+
if errFirst != nil && errSecond != nil {
24+
return errors.Wrap(errFirst, errSecond.Error())
25+
}
26+
if errFirst != nil {
27+
return errFirst
28+
}
29+
return errSecond
30+
}

0 commit comments

Comments
 (0)