api/types/jsonstream: prevent panic on nil-Error

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2026-02-18 23:41:48 +01:00
parent f222c1ed05
commit d00882aff2
3 changed files with 14 additions and 2 deletions

View File

@@ -8,5 +8,8 @@ type Error struct {
}
func (e *Error) Error() string {
if e == nil {
return "<nil>"
}
return e.Message
}

View File

@@ -1,13 +1,19 @@
package jsonstream
package jsonstream_test
import (
"testing"
"github.com/moby/moby/api/types/jsonstream"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
func TestError(t *testing.T) {
je := Error{404, "Not found"}
je := jsonstream.Error{Code: 404, Message: "Not found"}
assert.Assert(t, is.Error(&je, "Not found"))
}
func TestNilError(t *testing.T) {
var je *jsonstream.Error
assert.Assert(t, is.Error(je, "<nil>"))
}