Capture errors from docker log output
- Refactored logDockerResponse function to remove extra if-else nesting - logDockerResponse func now returns an error if error was detected from the log stream - logDockerResponse will check for msg.ErrorDetail.Message and bail if there's an error
This commit is contained in:
parent
1791f1fd44
commit
5330599c93
3 changed files with 35 additions and 20 deletions
3
Makefile
3
Makefile
|
@ -17,6 +17,9 @@ export GITHUB_TOKEN = $(shell cat ~/.config/github/token)
|
||||||
|
|
||||||
default: check
|
default: check
|
||||||
|
|
||||||
|
test:
|
||||||
|
go test -cover -short ./...
|
||||||
|
|
||||||
check:
|
check:
|
||||||
$(ACT) -ra check
|
$(ACT) -ra check
|
||||||
|
|
||||||
|
|
|
@ -51,7 +51,8 @@ func NewDockerBuildExecutor(input NewDockerBuildExecutorInput) common.Executor {
|
||||||
|
|
||||||
input.Logger.Debugf("Creating image from context dir '%s' with tag '%s'", input.ContextDir, input.ImageTag)
|
input.Logger.Debugf("Creating image from context dir '%s' with tag '%s'", input.ContextDir, input.ImageTag)
|
||||||
resp, err := cli.ImageBuild(input.Ctx, buildContext, options)
|
resp, err := cli.ImageBuild(input.Ctx, buildContext, options)
|
||||||
input.logDockerResponse(resp.Body, err != nil)
|
|
||||||
|
err = input.logDockerResponse(resp.Body, err != nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
@ -64,42 +65,52 @@ func (i *DockerExecutorInput) writeLog(isError bool, format string, args ...inte
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *DockerExecutorInput) logDockerResponse(dockerResponse io.ReadCloser, isError bool) {
|
func (i *DockerExecutorInput) logDockerResponse(dockerResponse io.ReadCloser, isError bool) error {
|
||||||
if dockerResponse == nil {
|
if dockerResponse == nil {
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
defer dockerResponse.Close()
|
defer dockerResponse.Close()
|
||||||
|
|
||||||
scanner := bufio.NewScanner(dockerResponse)
|
scanner := bufio.NewScanner(dockerResponse)
|
||||||
msg := dockerMessage{}
|
msg := dockerMessage{}
|
||||||
|
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
line := scanner.Bytes()
|
line := scanner.Bytes()
|
||||||
|
|
||||||
msg.ID = ""
|
msg.ID = ""
|
||||||
msg.Stream = ""
|
msg.Stream = ""
|
||||||
msg.Error = ""
|
msg.Error = ""
|
||||||
msg.ErrorDetail.Message = ""
|
msg.ErrorDetail.Message = ""
|
||||||
msg.Status = ""
|
msg.Status = ""
|
||||||
msg.Progress = ""
|
msg.Progress = ""
|
||||||
if err := json.Unmarshal(line, &msg); err == nil {
|
|
||||||
if msg.Error != "" {
|
|
||||||
i.writeLog(isError, "%s", msg.Error)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if msg.Status != "" {
|
if err := json.Unmarshal(line, &msg); err != nil {
|
||||||
if msg.Progress != "" {
|
|
||||||
i.writeLog(isError, "%s :: %s :: %s\n", msg.Status, msg.ID, msg.Progress)
|
|
||||||
} else {
|
|
||||||
i.writeLog(isError, "%s :: %s\n", msg.Status, msg.ID)
|
|
||||||
}
|
|
||||||
} else if msg.Stream != "" {
|
|
||||||
i.writeLog(isError, msg.Stream)
|
|
||||||
} else {
|
|
||||||
i.writeLog(false, "Unable to handle line: %s", string(line))
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
i.writeLog(false, "Unable to unmarshal line [%s] ==> %v", string(line), err)
|
i.writeLog(false, "Unable to unmarshal line [%s] ==> %v", string(line), err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if msg.Error != "" {
|
||||||
|
i.writeLog(isError, "%s", msg.Error)
|
||||||
|
return errors.New(msg.Error)
|
||||||
|
}
|
||||||
|
|
||||||
|
if msg.ErrorDetail.Message != "" {
|
||||||
|
i.writeLog(isError, "%s", msg.ErrorDetail.Message)
|
||||||
|
return errors.New(msg.Error)
|
||||||
|
}
|
||||||
|
|
||||||
|
if msg.Status != "" {
|
||||||
|
if msg.Progress != "" {
|
||||||
|
i.writeLog(isError, "%s :: %s :: %s\n", msg.Status, msg.ID, msg.Progress)
|
||||||
|
} else {
|
||||||
|
i.writeLog(isError, "%s :: %s\n", msg.Status, msg.ID)
|
||||||
|
}
|
||||||
|
} else if msg.Stream != "" {
|
||||||
|
i.writeLog(isError, msg.Stream)
|
||||||
|
} else {
|
||||||
|
i.writeLog(false, "Unable to handle line: %s", string(line))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue