-
Notifications
You must be signed in to change notification settings - Fork 671
refactor: hide pgx errors behind database.ErrNotFound #2274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
f85c6f0
219d469
72d25d2
5936b36
e239be2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package database | ||
|
|
||
| import "errors" | ||
|
|
||
| // ErrNotFound reports that the requested record does not exist (or is not | ||
| // visible to the given user). Match with errors.Is; implementations wrap it | ||
| // with call-site context. | ||
| var ErrNotFound = errors.New("record not found") |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package handlers_test | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "net/http" | ||
| "testing" | ||
|
|
||
| "github.com/kagent-dev/kagent/go/api/database" | ||
| "github.com/kagent-dev/kagent/go/core/internal/httpserver/handlers" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestRespondNotFoundOrError(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| err error | ||
| wantStatus int | ||
| }{ | ||
| {name: "missing record maps to 404", err: fmt.Errorf("session x: %w", database.ErrNotFound), wantStatus: http.StatusNotFound}, | ||
| {name: "backend failure maps to 500", err: errors.New("connection refused"), wantStatus: http.StatusInternalServerError}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| w := newMockErrorResponseWriter() | ||
| handlers.RespondNotFoundOrError(w, "not found", tt.err) | ||
| require.Equal(t, tt.wantStatus, w.Code) | ||
| }) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,7 @@ import ( | |
| "net" | ||
| "net/http" | ||
|
|
||
| "github.com/jackc/pgx/v5" | ||
| "github.com/kagent-dev/kagent/go/api/database" | ||
| apierrors "github.com/kagent-dev/kagent/go/core/internal/httpserver/errors" | ||
| "github.com/kagent-dev/kagent/go/core/internal/httpserver/handlers" | ||
| ctrllog "sigs.k8s.io/controller-runtime/pkg/log" | ||
|
|
@@ -67,7 +67,7 @@ func (w *errorResponseWriter) RespondWithError(err error) { | |
| underlying = err | ||
| } | ||
|
|
||
| if underlying != nil && !errors.Is(underlying, pgx.ErrNoRows) { | ||
| if underlying != nil && !errors.Is(underlying, database.ErrNotFound) { | ||
| log.Error(underlying, message) | ||
| } else { | ||
| log.Info(message) | ||
|
|
@@ -76,7 +76,9 @@ func (w *errorResponseWriter) RespondWithError(err error) { | |
| w.Header().Set("Content-Type", "application/json") | ||
| w.WriteHeader(statusCode) | ||
| errMsg := message | ||
| if underlying != nil { | ||
| // 5xx bodies carry only the generic message: the underlying error is | ||
| // backend-internal detail (already logged above) and must not reach clients. | ||
| if underlying != nil && statusCode < http.StatusInternalServerError { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this strips the underlying detail from every existing 500 response across the api, not just the new sites, so it deserves a mention in the pr description since clients or tests may rely on that text.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch, that's intentional but I'd under-documented it. I updated the PR description and added a test |
||
| errMsg = message + ": " + underlying.Error() | ||
| } | ||
| json.NewEncoder(w).Encode(map[string]string{"error": errMsg}) //nolint:errcheck | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 8dab542: 5xx bodies now carry only the generic message; the underlying error is logged server-side (that path already ran through log.Error above). Non-5xx responses keep the detail, which callers rely on for 400/403/404 context.