chore: use NotFound error for missing components

In this way, the return status could be 404 instead of 500
This commit is contained in:
yongruilin 2025-01-28 14:52:54 -08:00
parent c5c19a6487
commit bde75d6020
2 changed files with 3 additions and 3 deletions

View File

@ -18,7 +18,6 @@ package componentstatus
import (
"context"
"fmt"
"sync"
"k8s.io/apimachinery/pkg/fields"
@ -26,6 +25,7 @@ import (
"k8s.io/apiserver/pkg/registry/generic"
"k8s.io/apiserver/pkg/storage"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
@ -136,7 +136,7 @@ func (rs *REST) Get(ctx context.Context, name string, options *metav1.GetOptions
servers := rs.GetServersToValidate()
if server, ok := servers[name]; !ok {
return nil, fmt.Errorf("Component not found: %s", name)
return nil, apierrors.NewNotFound(api.Resource("componentstatus"), name)
} else {
return rs.getComponentStatus(name, server), nil
}

View File

@ -177,7 +177,7 @@ func TestGet_BadName(t *testing.T) {
if err == nil {
t.Fatalf("Expected error, but did not get one")
}
if !strings.Contains(err.Error(), "Component not found: invalidname") {
if !strings.Contains(err.Error(), `componentstatus "invalidname" not found`) {
t.Fatalf("Got unexpected error: %v", err)
}
}