From 49200e0d6c6918cbac0165eb70462fdaf24373ba Mon Sep 17 00:00:00 2001 From: Fiete Ostkamp Date: Thu, 4 Dec 2025 16:40:28 +0100 Subject: [PATCH] Respond with 404 when instance not found - respond with 404 not found on /instance/{instID} endpoint - this replaces a 500 internal error response which is misleading Issue-ID: MULTICLOUD-1526 Change-Id: Ibbe49cbb3c1286546f7dd1d9c24834291b14d9be Signed-off-by: Fiete Ostkamp --- src/k8splugin/api/instancehandler.go | 4 ++++ src/k8splugin/api/instancehandler_test.go | 10 +++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/k8splugin/api/instancehandler.go b/src/k8splugin/api/instancehandler.go index b5ef6d39..5ce52f27 100644 --- a/src/k8splugin/api/instancehandler.go +++ b/src/k8splugin/api/instancehandler.go @@ -186,6 +186,10 @@ func (i instanceHandler) getHandler(w http.ResponseWriter, r *http.Request) { } if err != nil { + if err.Error() == "Get Instance: Error finding master table: mongo: no documents in result" { + http.Error(w, err.Error(), http.StatusNotFound) + return + } log.Error("Error getting Instance", log.Fields{ "error": err, "id": id, diff --git a/src/k8splugin/api/instancehandler_test.go b/src/k8splugin/api/instancehandler_test.go index f1ef8808..37b307cb 100644 --- a/src/k8splugin/api/instancehandler_test.go +++ b/src/k8splugin/api/instancehandler_test.go @@ -242,7 +242,15 @@ func TestInstanceGetHandler(t *testing.T) { }, }, { - label: "Succesful get an Instance", + label: "Not found Instance", + input: "HaKpys8e", + expectedCode: http.StatusNotFound, + instClient: &mockInstanceClient{ + err: pkgerrors.New("Get Instance: Error finding master table: mongo: no documents in result"), + }, + }, + { + label: "Successful get an Instance", input: "HaKpys8e", expectedCode: http.StatusOK, expectedResponse: &app.InstanceResponse{ -- 2.16.6