Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pkg/api/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ func SendNotFound(w http.ResponseWriter, r *http.Request) {

traceID, _ := logger.GetRequestID(r.Context())
now := time.Now().UTC()
detail := fmt.Sprintf("The requested resource '%s' doesn't exist", r.URL.Path)
detail := fmt.Sprintf("The requested endpoint '%s' does not exist", r.URL.Path)

body := openapi.ProblemDetails{
Type: errors.ErrorTypeNotFound,
Title: "Resource Not Found",
Title: "Endpoint Not Found",
Status: http.StatusNotFound,
Detail: &detail,
Instance: &r.URL.Path,
Code: ptrString(errors.CodeNotFoundGeneric),
Code: ptrString(errors.CodeNotFoundEndpoint),
Timestamp: &now,
TraceId: &traceID,
}
Expand Down
35 changes: 35 additions & 0 deletions pkg/api/error_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package api

import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"

"github.com/openshift-hyperfleet/hyperfleet-api/pkg/api/openapi"
"github.com/openshift-hyperfleet/hyperfleet-api/pkg/errors"

. "github.com/onsi/gomega"
)

func TestSendNotFound_ReturnsEndpointCode(t *testing.T) {
RegisterTestingT(t)

req := httptest.NewRequest(http.MethodGet, "/api/hyperfleet/v1/invalid-path", nil)
rec := httptest.NewRecorder()

SendNotFound(rec, req)

Expect(rec.Code).To(Equal(http.StatusNotFound))
Expect(rec.Header().Get("Content-Type")).To(Equal("application/problem+json"))

var body openapi.ProblemDetails
Expect(json.Unmarshal(rec.Body.Bytes(), &body)).To(Succeed())

Expect(body.Status).To(Equal(http.StatusNotFound))
Expect(body.Title).To(Equal("Endpoint Not Found"))
Expect(body.Code).NotTo(BeNil())
Expect(*body.Code).To(Equal(errors.CodeNotFoundEndpoint))
Expect(body.Detail).NotTo(BeNil())
Expect(*body.Detail).To(Equal("The requested endpoint '/api/hyperfleet/v1/invalid-path' does not exist"))
}
4 changes: 4 additions & 0 deletions pkg/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const (
CodeAuthExpiredToken = "HYPERFLEET-AUT-003" //nolint:gosec // Not actual credentials, just error code names

// Not Found errors (NTF) - 404
CodeNotFoundEndpoint = "HYPERFLEET-NTF-000"
CodeNotFoundGeneric = "HYPERFLEET-NTF-001"
Comment thread
coderabbitai[bot] marked this conversation as resolved.
CodeNotFoundCluster = "HYPERFLEET-NTF-002"
CodeNotFoundNodePool = "HYPERFLEET-NTF-003"
Expand Down Expand Up @@ -136,6 +137,9 @@ var errorDefinitions = map[string]errorDefinition{
},

// Not Found errors (NTF) - 404
CodeNotFoundEndpoint: {
ErrorTypeNotFound, "Endpoint Not Found", "The requested endpoint does not exist", http.StatusNotFound,
},
CodeNotFoundGeneric: {
ErrorTypeNotFound, "Resource Not Found", "Resource not found", http.StatusNotFound,
},
Expand Down
1 change: 1 addition & 0 deletions pkg/errors/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ func TestErrorFind(t *testing.T) {
expectFound bool
}{
{"known code", CodeNotFoundGeneric, ErrorTypeNotFound, true},
{"endpoint not found code", CodeNotFoundEndpoint, ErrorTypeNotFound, true},
{"unknown code", "INVALID-CODE-999", "", false},
}
for _, tt := range tests {
Expand Down