pkg/jsonmessage: move JSONError to api/types/jsonstream

Also rename api type JSONError to Error

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Derek McGowan <derek@mcg.dev>
This commit is contained in:
Sebastiaan van Stijn
2025-07-29 14:19:27 +02:00
parent 6453f9d5f4
commit 0515e1c991
10 changed files with 58 additions and 32 deletions

View File

@@ -8,6 +8,7 @@ import (
"time"
"github.com/docker/go-units"
"github.com/moby/moby/api/types/jsonstream"
"github.com/moby/term"
)
@@ -15,17 +16,6 @@ import (
// ensure the formatted time isalways the same number of characters.
const RFC3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"
// JSONError wraps a concrete Code and Message, Code is
// an integer error code, Message is the error message.
type JSONError struct {
Code int `json:"code,omitempty"`
Message string `json:"message,omitempty"`
}
func (e *JSONError) Error() string {
return e.Message
}
// JSONProgress describes a progress message in a JSON stream.
type JSONProgress struct {
// Current is the current status and value of the progress made towards Total.
@@ -148,12 +138,12 @@ type JSONMessage struct {
// ProgressMessage is a pre-formatted presentation of [Progress].
//
// 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.
ProgressMessage string `json:"progress,omitempty"`
ID string `json:"id,omitempty"`
From string `json:"from,omitempty"`
Time int64 `json:"time,omitempty"`
TimeNano int64 `json:"timeNano,omitempty"`
Error *JSONError `json:"errorDetail,omitempty"`
ProgressMessage string `json:"progress,omitempty"`
ID string `json:"id,omitempty"`
From string `json:"from,omitempty"`
Time int64 `json:"time,omitempty"`
TimeNano int64 `json:"timeNano,omitempty"`
Error *jsonstream.Error `json:"errorDetail,omitempty"`
// ErrorMessage contains errors encountered during the operation.
//

View File

@@ -7,16 +7,12 @@ import (
"testing"
"time"
"github.com/moby/moby/api/types/jsonstream"
"github.com/moby/term"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
func TestError(t *testing.T) {
je := JSONError{404, "Not found"}
assert.Assert(t, is.Error(&je, "Not found"))
}
func TestProgressString(t *testing.T) {
type expected struct {
short string
@@ -207,14 +203,14 @@ func TestJSONMessageDisplay(t *testing.T) {
// Test JSONMessage with an Error. It will return an error with the text as error, not the meaning of the HTTP code.
func TestJSONMessageDisplayWithJSONError(t *testing.T) {
data := bytes.NewBuffer([]byte{})
jsonMessage := JSONMessage{Error: &JSONError{404, "Can't find it"}}
jsonMessage := JSONMessage{Error: &jsonstream.Error{Code: 404, Message: "Can't find it"}}
err := jsonMessage.Display(data, true)
if err == nil || err.Error() != "Can't find it" {
t.Fatalf("Expected a JSONError 404, got %q", err)
t.Fatalf("Expected a jsonstream.Error 404, got %q", err)
}
jsonMessage = JSONMessage{Error: &JSONError{401, "Anything"}}
jsonMessage = JSONMessage{Error: &jsonstream.Error{Code: 401, Message: "Anything"}}
err = jsonMessage.Display(data, true)
assert.Check(t, is.Error(err, "Anything"))
}

View File

@@ -9,6 +9,7 @@ import (
"github.com/docker/docker/pkg/jsonmessage"
"github.com/docker/docker/pkg/progress"
"github.com/moby/moby/api/types/jsonstream"
)
const streamNewline = "\r\n"
@@ -31,9 +32,9 @@ func FormatStatus(id, format string, a ...interface{}) []byte {
// FormatError formats the error as a JSON object
func FormatError(err error) []byte {
jsonError, ok := err.(*jsonmessage.JSONError)
jsonError, ok := err.(*jsonstream.Error)
if !ok {
jsonError = &jsonmessage.JSONError{Message: err.Error()}
jsonError = &jsonstream.Error{Message: err.Error()}
}
if b, err := json.Marshal(&jsonmessage.JSONMessage{Error: jsonError, ErrorMessage: err.Error()}); err == nil {
return appendNewline(b)

View File

@@ -10,6 +10,7 @@ import (
"github.com/docker/docker/pkg/jsonmessage"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/moby/moby/api/types/jsonstream"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
@@ -47,7 +48,7 @@ func TestFormatError(t *testing.T) {
}
func TestFormatJSONError(t *testing.T) {
err := &jsonmessage.JSONError{Code: 50, Message: "Json error"}
err := &jsonstream.Error{Code: 50, Message: "Json error"}
res := FormatError(err)
expected := `{"errorDetail":{"code":50,"message":"Json error"},"error":"Json error"}` + streamNewline
assert.Check(t, is.Equal(expected, string(res)))