Skip to content

Commit 0515e1c

Browse files
committed
pkg/jsonmessage: move JSONError to api/types/jsonstream
Also rename api type JSONError to Error Signed-off-by: Sebastiaan van Stijn <[email protected]> Signed-off-by: Derek McGowan <[email protected]>
1 parent 6453f9d commit 0515e1c

10 files changed

Lines changed: 58 additions & 32 deletions

File tree

api/types/jsonstream/json_error.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package jsonstream
2+
3+
// Error wraps a concrete Code and Message, Code is
4+
// an integer error code, Message is the error message.
5+
type Error struct {
6+
Code int `json:"code,omitempty"`
7+
Message string `json:"message,omitempty"`
8+
}
9+
10+
func (e *Error) Error() string {
11+
return e.Message
12+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package jsonstream
2+
3+
import (
4+
"testing"
5+
6+
"gotest.tools/v3/assert"
7+
is "gotest.tools/v3/assert/cmp"
8+
)
9+
10+
func TestError(t *testing.T) {
11+
je := Error{404, "Not found"}
12+
assert.Assert(t, is.Error(&je, "Not found"))
13+
}

daemon/builder/dockerfile/dispatchers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ import (
1919
"github.com/docker/docker/daemon/builder"
2020
"github.com/docker/docker/daemon/internal/image"
2121
"github.com/docker/docker/errdefs"
22-
"github.com/docker/docker/pkg/jsonmessage"
2322
"github.com/docker/go-connections/nat"
2423
"github.com/moby/buildkit/frontend/dockerfile/instructions"
2524
"github.com/moby/buildkit/frontend/dockerfile/parser"
2625
"github.com/moby/buildkit/frontend/dockerfile/shell"
26+
"github.com/moby/moby/api/types/jsonstream"
2727
"github.com/moby/sys/signal"
2828
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
2929
"github.com/pkg/errors"
@@ -386,7 +386,7 @@ func dispatchRun(ctx context.Context, d dispatchRequest, c *instructions.RunComm
386386
if err.Error() != "" {
387387
msg = fmt.Sprintf("%s: %s", msg, err.Error())
388388
}
389-
return &jsonmessage.JSONError{
389+
return &jsonstream.Error{
390390
Message: msg,
391391
Code: err.StatusCode(),
392392
}

daemon/builder/dockerfile/internals_windows.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99

1010
"github.com/containerd/platforms"
1111
"github.com/docker/docker/errdefs"
12-
"github.com/docker/docker/pkg/jsonmessage"
1312
"github.com/moby/moby/api/types/container"
13+
"github.com/moby/moby/api/types/jsonstream"
1414
"github.com/moby/moby/api/types/mount"
1515
"github.com/moby/sys/user"
1616
"golang.org/x/sys/windows"
@@ -106,7 +106,7 @@ func lookupNTAccount(ctx context.Context, builder *Builder, accountName string,
106106

107107
if err := builder.containerManager.Run(ctx, container.ID, stdout, stderr); err != nil {
108108
if err, ok := err.(*statusCodeError); ok {
109-
return identity{}, &jsonmessage.JSONError{
109+
return identity{}, &jsonstream.Error{
110110
Message: stderr.String(),
111111
Code: err.StatusCode(),
112112
}

pkg/jsonmessage/jsonmessage.go

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,14 @@ import (
88
"time"
99

1010
"github.com/docker/go-units"
11+
"github.com/moby/moby/api/types/jsonstream"
1112
"github.com/moby/term"
1213
)
1314

1415
// RFC3339NanoFixed is time.RFC3339Nano with nanoseconds padded using zeros to
1516
// ensure the formatted time isalways the same number of characters.
1617
const RFC3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"
1718

18-
// JSONError wraps a concrete Code and Message, Code is
19-
// an integer error code, Message is the error message.
20-
type JSONError struct {
21-
Code int `json:"code,omitempty"`
22-
Message string `json:"message,omitempty"`
23-
}
24-
25-
func (e *JSONError) Error() string {
26-
return e.Message
27-
}
28-
2919
// JSONProgress describes a progress message in a JSON stream.
3020
type JSONProgress struct {
3121
// Current is the current status and value of the progress made towards Total.
@@ -148,12 +138,12 @@ type JSONMessage struct {
148138
// ProgressMessage is a pre-formatted presentation of [Progress].
149139
//
150140
// Deprecated: this field is deprecated since docker v0.7.1 / API v1.8. Use the information in [Progress] instead. This field will be omitted in a future release.
151-
ProgressMessage string `json:"progress,omitempty"`
152-
ID string `json:"id,omitempty"`
153-
From string `json:"from,omitempty"`
154-
Time int64 `json:"time,omitempty"`
155-
TimeNano int64 `json:"timeNano,omitempty"`
156-
Error *JSONError `json:"errorDetail,omitempty"`
141+
ProgressMessage string `json:"progress,omitempty"`
142+
ID string `json:"id,omitempty"`
143+
From string `json:"from,omitempty"`
144+
Time int64 `json:"time,omitempty"`
145+
TimeNano int64 `json:"timeNano,omitempty"`
146+
Error *jsonstream.Error `json:"errorDetail,omitempty"`
157147

158148
// ErrorMessage contains errors encountered during the operation.
159149
//

pkg/jsonmessage/jsonmessage_test.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,12 @@ import (
77
"testing"
88
"time"
99

10+
"github.com/moby/moby/api/types/jsonstream"
1011
"github.com/moby/term"
1112
"gotest.tools/v3/assert"
1213
is "gotest.tools/v3/assert/cmp"
1314
)
1415

15-
func TestError(t *testing.T) {
16-
je := JSONError{404, "Not found"}
17-
assert.Assert(t, is.Error(&je, "Not found"))
18-
}
19-
2016
func TestProgressString(t *testing.T) {
2117
type expected struct {
2218
short string
@@ -207,14 +203,14 @@ func TestJSONMessageDisplay(t *testing.T) {
207203
// Test JSONMessage with an Error. It will return an error with the text as error, not the meaning of the HTTP code.
208204
func TestJSONMessageDisplayWithJSONError(t *testing.T) {
209205
data := bytes.NewBuffer([]byte{})
210-
jsonMessage := JSONMessage{Error: &JSONError{404, "Can't find it"}}
206+
jsonMessage := JSONMessage{Error: &jsonstream.Error{Code: 404, Message: "Can't find it"}}
211207

212208
err := jsonMessage.Display(data, true)
213209
if err == nil || err.Error() != "Can't find it" {
214-
t.Fatalf("Expected a JSONError 404, got %q", err)
210+
t.Fatalf("Expected a jsonstream.Error 404, got %q", err)
215211
}
216212

217-
jsonMessage = JSONMessage{Error: &JSONError{401, "Anything"}}
213+
jsonMessage = JSONMessage{Error: &jsonstream.Error{Code: 401, Message: "Anything"}}
218214
err = jsonMessage.Display(data, true)
219215
assert.Check(t, is.Error(err, "Anything"))
220216
}

pkg/streamformatter/streamformatter.go

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

1010
"github.com/docker/docker/pkg/jsonmessage"
1111
"github.com/docker/docker/pkg/progress"
12+
"github.com/moby/moby/api/types/jsonstream"
1213
)
1314

1415
const streamNewline = "\r\n"
@@ -31,9 +32,9 @@ func FormatStatus(id, format string, a ...interface{}) []byte {
3132

3233
// FormatError formats the error as a JSON object
3334
func FormatError(err error) []byte {
34-
jsonError, ok := err.(*jsonmessage.JSONError)
35+
jsonError, ok := err.(*jsonstream.Error)
3536
if !ok {
36-
jsonError = &jsonmessage.JSONError{Message: err.Error()}
37+
jsonError = &jsonstream.Error{Message: err.Error()}
3738
}
3839
if b, err := json.Marshal(&jsonmessage.JSONMessage{Error: jsonError, ErrorMessage: err.Error()}); err == nil {
3940
return appendNewline(b)

pkg/streamformatter/streamformatter_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/docker/docker/pkg/jsonmessage"
1111
"github.com/google/go-cmp/cmp"
1212
"github.com/google/go-cmp/cmp/cmpopts"
13+
"github.com/moby/moby/api/types/jsonstream"
1314
"gotest.tools/v3/assert"
1415
is "gotest.tools/v3/assert/cmp"
1516
)
@@ -47,7 +48,7 @@ func TestFormatError(t *testing.T) {
4748
}
4849

4950
func TestFormatJSONError(t *testing.T) {
50-
err := &jsonmessage.JSONError{Code: 50, Message: "Json error"}
51+
err := &jsonstream.Error{Code: 50, Message: "Json error"}
5152
res := FormatError(err)
5253
expected := `{"errorDetail":{"code":50,"message":"Json error"},"error":"Json error"}` + streamNewline
5354
assert.Check(t, is.Equal(expected, string(res)))

vendor/github.com/moby/moby/api/types/jsonstream/json_error.go

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/modules.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,7 @@ github.com/moby/moby/api/types/container
950950
github.com/moby/moby/api/types/events
951951
github.com/moby/moby/api/types/filters
952952
github.com/moby/moby/api/types/image
953+
github.com/moby/moby/api/types/jsonstream
953954
github.com/moby/moby/api/types/mount
954955
github.com/moby/moby/api/types/network
955956
github.com/moby/moby/api/types/registry

0 commit comments

Comments
 (0)