From 2bbdeec8998b3fd400ed8d892e87ed1d8935e105 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 28 Dec 2018 22:13:27 +0100 Subject: [PATCH] Include old error-message for backward compatibility Commit 2061af766f61f6bc8d3e43d5553fba315f569597 fixed the API returning incorrect status-codes, but also changed the error message for conflicting service-names to be in line with other objects (secrets, configs); "service XX already exists". Unfortunately, there are existing consumers of the API that perform string-matching, and changing the error-message resulted in a breaking change. This patch prepends the `ErrNameConflict` error-message to the error-message, so that those consumers still find the original message, but preserves the enhancement made in 2061af7 (inclusion of the conflicting service name). With this patch applied, the error message will look like this; name conflicts with an existing object: service myservice already exists Signed-off-by: Sebastiaan van Stijn --- manager/controlapi/service.go | 5 ++++- manager/controlapi/service_test.go | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/manager/controlapi/service.go b/manager/controlapi/service.go index a50e6423db..ab4fe38169 100644 --- a/manager/controlapi/service.go +++ b/manager/controlapi/service.go @@ -682,7 +682,10 @@ func (s *Server) CreateService(ctx context.Context, request *api.CreateServiceRe }) switch err { case store.ErrNameConflict: - return nil, status.Errorf(codes.AlreadyExists, "service %s already exists", request.Spec.Annotations.Name) + // Enhance the name-confict error to include the service name. The original + // `ErrNameConflict` error-message is included for backward-compatibility + // with older consumers of the API performing string-matching. + return nil, status.Errorf(codes.AlreadyExists, "%s: service %s already exists", err.Error(), request.Spec.Annotations.Name) case nil: return &api.CreateServiceResponse{Service: service}, nil default: diff --git a/manager/controlapi/service_test.go b/manager/controlapi/service_test.go index ba3b2bae55..641e47c843 100644 --- a/manager/controlapi/service_test.go +++ b/manager/controlapi/service_test.go @@ -623,6 +623,10 @@ func TestCreateService(t *testing.T) { r, err = ts.Client.CreateService(context.Background(), &api.CreateServiceRequest{Spec: spec}) assert.Error(t, err) assert.Equal(t, codes.AlreadyExists, testutils.ErrorCode(err)) + + // Make sure the error contains "name conflicts with an existing object" for + // backward-compatibility with older clients doing string-matching... + assert.Contains(t, err.Error(), "name conflicts with an existing object") } func TestSecretValidation(t *testing.T) {