Skip to content

Adds Error envelope#62

Merged
jlpdeveloper merged 6 commits into
mainfrom
error-envelope
May 15, 2026
Merged

Adds Error envelope#62
jlpdeveloper merged 6 commits into
mainfrom
error-envelope

Conversation

@jlpdeveloper
Copy link
Copy Markdown
Contributor

@jlpdeveloper jlpdeveloper commented May 13, 2026

Description

Code Rabbit Summary

Summary by CodeRabbit

  • New Features

    • API error responses now use a standardized problem+json format with consistent type, title, status, detail, and instance fields.
  • Improvements

    • All platform and product endpoints emit structured error responses and handle JSON encoding robustly before sending responses.
  • Tests

    • Added tests verifying structured error responses, defaulting behavior, and various error scenarios.
  • Chores

    • Updated ignore rules for local tooling artifacts.

Review Change Stack

Fixes

Closes #58

Post Deployment Tasks?

- Implement reusable `ErrorEnvelope` struct for standardized error responses.
- Add `HandleHttpError` function to write error details as `application/problem+json`.
- Include unit tests to validate behavior with various scenarios.
…andleHttpError` for standardized responses. Update tests to validate error structure.
…log unknown codes. Update tests with additional cases for zero and negative status codes.
@jlpdeveloper jlpdeveloper added this to the Phase 2: API Endpoints milestone May 13, 2026
@jlpdeveloper jlpdeveloper self-assigned this May 13, 2026
@jlpdeveloper jlpdeveloper added the enhancement New feature or request label May 13, 2026
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 13, 2026

Warning

Rate limit exceeded

@jlpdeveloper has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 44 minutes and 56 seconds before requesting another review.

You’ve run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 36b59e1c-b9e0-4119-a2b5-06f506d1f56e

📥 Commits

Reviewing files that changed from the base of the PR and between 2a9f77e and 39a0fec.

📒 Files selected for processing (1)
  • internal/product/handler.go
📝 Walkthrough

Walkthrough

Product handlers now emit RFC 7807 Problem Details via a new ErrorEnvelope/HandleHttpError and buffer JSON encodes before writing responses; all product CRUD error paths were converted from plain text http.Error to structured JSON envelopes.

Changes

Product Handler Error Integration

Layer / File(s) Summary
Imports and response buffering
internal/product/handler.go
Adds bytes import to support encoding responses into a buffer before writing to http.ResponseWriter.
Create/Update/Delete handlers and validation
internal/product/handler.go
CreateProduct, UpdateProduct, and DeleteProduct now use internal.HandleHttpError(w, internal.ErrorEnvelope{Detail: ...}, <status>) instead of http.Error for JSON decode errors, validation failures, invalid IDs, not-found (pgx.ErrNoRows), and DB failures; status codes unchanged.
List and Get handlers with buffered encoding
internal/product/handler.go
GetProductsByPlatform and GetProductById replace http.Error paths with HandleHttpError, and successful JSON responses are encoded into a bytes.Buffer and written only after successful encoding; encoding failures are handled via HandleHttpError.

Sequence Diagram

sequenceDiagram
  participant Client
  participant ProductHandler
  participant ProductStore
  participant JSONEncoder
  participant HTTPResponse

  Client->>ProductHandler: HTTP request (create/get/list/update/delete)
  ProductHandler->>ProductStore: DB query/command
  alt DB returns error (pgx.ErrNoRows or other)
    ProductStore-->>ProductHandler: error
    ProductHandler->>HTTPResponse: HandleHttpError(ErrorEnvelope, status)
  else DB returns data
    ProductStore-->>ProductHandler: data
    ProductHandler->>JSONEncoder: encode data into bytes.Buffer
    alt encode success
      JSONEncoder-->>ProductHandler: buf.Bytes()
      ProductHandler->>HTTPResponse: set Content-Type and Write(buf.Bytes())
    else encode failure
      JSONEncoder-->>ProductHandler: encode error
      ProductHandler->>HTTPResponse: HandleHttpError(ErrorEnvelope, 500)
    end
  end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Poem

🐰 I nibble at bugs with a tidy sight,
Problem Details now wrapped up tight,
From product create to the product list,
Errors are sandwiched in JSON bliss,
Hop, code—standards make responses right.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Out of Scope Changes check ⚠️ Warning Changes to .gitignore (liquibase_libs and .junie patterns) are out of scope relative to the RFC 7807 error envelope implementation objective. Remove .gitignore changes unrelated to the error envelope feature, or move them to a separate PR focused on build/environment configuration.
Title check ❓ Inconclusive The title 'Adds Error envelope' is vague and does not clearly convey the primary change of implementing RFC 7807 standardized error responses. Improve the title to be more specific, such as 'Implement RFC 7807 Problem Details error envelope' or 'Add RFC 7807-compliant error responses'.
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Linked Issues check ✅ Passed The PR implements RFC 7807 Problem Details with ErrorEnvelope struct, HandleHttpError handler, and updates all error paths in handlers to use the standard envelope with correct Content-Type and matching status codes.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch error-envelope

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@jlpdeveloper jlpdeveloper added the skip-changelog Won't be added to the release notes label May 13, 2026
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 5

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
internal/platform/handler_test.go (1)

62-126: ⚠️ Potential issue | 🟠 Major | 🏗️ Heavy lift

Insufficient test coverage for ErrorEnvelope structure.

Tests for CreatePlatform, UpdatePlatform, GetPlatform, and GetPlatforms verify HTTP status codes but do not verify the RFC 7807 ErrorEnvelope structure for error responses. This leaves the error envelope integration untested for these handlers.

Each error test case should verify:

  • Content-Type: application/problem+json header
  • ErrorEnvelope fields: type, title, status, detail
  • status field matches HTTP status code

As per coding guidelines: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"

Also applies to: 128-189, 191-275, 371-527

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@internal/platform/handler_test.go` around lines 62 - 126, The tests in
TestCreatePlatform (and analogous tests for
UpdatePlatform/GetPlatform/GetPlatforms) only assert HTTP status codes but not
the RFC 7807 ErrorEnvelope; update the error-case branches in TestCreatePlatform
(and the other test functions) to, after calling platformHandler.CreatePlatform
via rr, assert that for error responses rr.Header().Get("Content-Type") ==
"application/problem+json" and unmarshal rr.Body into the ErrorEnvelope struct
and assert its fields type, title, status, and detail exist and that
envelope.Status equals rr.Code (use the mockPlatformQuerier error cases and the
"Invalid JSON"/"Missing Name"/"DB Error" scenarios to drive these assertions);
reference the platformHandler.CreatePlatform handler, the tests' rr
(httptest.ResponseRecorder), and the ErrorEnvelope type when locating where to
add the checks.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@internal/error_envelope_test.go`:
- Around line 11-18: The table-driven tests initialize ErrorEnvelope values but
never assert the Detail and Instance fields; extend the test struct (the tests
variable) to include expectedDetail and expectedInstance fields, populate them
in each case, and add assertions in the test loop that compare the envelope's
Detail and Instance (from the ErrorEnvelope under test/and/or the rendered
output) against expectedDetail and expectedInstance so the RFC7807 fields are
covered; locate the tests slice and the loop that checks
expectedBody/expectedTitle/expectedType and add the two new checks there
referencing ErrorEnvelope.Detail and ErrorEnvelope.Instance.

In `@internal/platform/handler_test.go`:
- Around line 355-365: The test currently only checks ErrorEnvelope.Detail;
extend the assertion block that runs when rr.Code >= 400 to also validate the
Content-Type header equals "application/problem+json" and assert envelope.Type
== "about:blank", envelope.Title is non-empty (or matches expected short
summary), and envelope.Status equals rr.Code (or tt.expectedStatus); locate the
checks around rr, the ErrorEnvelope type and variable envelope in
handler_test.go and add header check using rr.Header().Get("Content-Type") and
the additional field assertions for Type, Title and Status so the test fully
conforms to RFC 7807.

In `@internal/platform/handler.go`:
- Around line 118-122: The handler currently sets w.Header().Set("Content-Type",
"application/json") then calls json.NewEncoder(w).Encode(platforms), which can
write partial data and prevent internal.HandleHttpError from changing headers to
application/problem+json; fix by encoding into a temporary buffer (e.g.,
bytes.Buffer or json.Marshal) inside the same function (the scope where
platforms is available), check for encoding error before writing any
headers/body, and only on success set Content-Type to application/json and write
the status and buffer to w; keep calls to internal.HandleHttpError and
ErrorEnvelope for the error path so the handler can set the proper
application/problem+json content type and status.
- Around line 145-149: The response currently sets Content-Type and streams json
directly with json.NewEncoder(w).Encode(platform) which can leave Content-Type
set to application/json even if encoding fails; change to encode into a
temporary buffer first (e.g., bytes.Buffer) inside the same handler (the
function containing the json.NewEncoder call), check for encoding error, call
internal.HandleHttpError(w, internal.ErrorEnvelope{...}) on failure, and only if
encoding succeeds set w.Header().Set("Content-Type", "application/json") and
write the buffer to w so HandleHttpError can set the correct RFC 7807 headers;
update the code around json.NewEncoder(w).Encode(platform) and the Content-Type
header accordingly.

In `@internal/product/handler.go`:
- Around line 127-129: The current code calls
json.NewEncoder(w).Encode(products) (and json.NewEncoder(w).Encode(product))
which may write response bytes before errors are detected so
internal.HandleHttpError cannot set headers/status; instead, encode into an
in-memory buffer first (e.g., bytes.Buffer or bytes.NewBuffer), check the Encode
error, and only if successful set Content-Type and write the status and buffer
bytes to w; on encode error call internal.HandleHttpError with the error detail.
Target fixes around the json.NewEncoder(w).Encode(products)/Encode(product)
sites and ensure you perform the buffer-then-write pattern before writing any
headers or body.

---

Outside diff comments:
In `@internal/platform/handler_test.go`:
- Around line 62-126: The tests in TestCreatePlatform (and analogous tests for
UpdatePlatform/GetPlatform/GetPlatforms) only assert HTTP status codes but not
the RFC 7807 ErrorEnvelope; update the error-case branches in TestCreatePlatform
(and the other test functions) to, after calling platformHandler.CreatePlatform
via rr, assert that for error responses rr.Header().Get("Content-Type") ==
"application/problem+json" and unmarshal rr.Body into the ErrorEnvelope struct
and assert its fields type, title, status, and detail exist and that
envelope.Status equals rr.Code (use the mockPlatformQuerier error cases and the
"Invalid JSON"/"Missing Name"/"DB Error" scenarios to drive these assertions);
reference the platformHandler.CreatePlatform handler, the tests' rr
(httptest.ResponseRecorder), and the ErrorEnvelope type when locating where to
add the checks.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: ef1dbb21-691c-4633-bb06-bbe659d4c58d

📥 Commits

Reviewing files that changed from the base of the PR and between 486becc and 3c2715c.

📒 Files selected for processing (5)
  • internal/error_envelope.go
  • internal/error_envelope_test.go
  • internal/platform/handler.go
  • internal/platform/handler_test.go
  • internal/product/handler.go

Comment thread internal/error_envelope_test.go
Comment thread internal/platform/handler_test.go
Comment thread internal/platform/handler.go Outdated
Comment thread internal/platform/handler.go Outdated
Comment thread internal/product/handler.go Outdated
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

♻️ Duplicate comments (1)
internal/product/handler.go (1)

158-165: ⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Same errcheck finding at line 165.

Apply the same fix as for GetProductsByPlatform: capture or explicitly discard the w.Write return value to satisfy errcheck.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@internal/product/handler.go` around lines 158 - 165, The w.Write call's error
return is ignored; update the response write in the same way as
GetProductsByPlatform by capturing or explicitly discarding the returned (int,
error) from w.Write(buf.Bytes()) — e.g., assign to _ , err or _ , _ — and if err
!= nil call internal.HandleHttpError with a suitable internal.ErrorEnvelope and
http.StatusInternalServerError; target the write after encoding (bytes.Buffer /
json.NewEncoder.Encode and the w.Write call) to satisfy errcheck.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@internal/product/handler.go`:
- Around line 127-134: The call to w.Write(buf.Bytes()) in the response path
(inside the product list handler in internal/product/handler.go) currently
ignores its returned error; update that code to capture and handle the error
(either assign to _ if you intentionally want to ignore it or, preferably, check
the error and log it via existing logging/error handling utilities or call
internal.HandleHttpError for unexpected write failures). Locate the block that
encodes products with json.NewEncoder(&buf).Encode(products) and the subsequent
w.Header().Set(...); w.Write(buf.Bytes()) line and modify the w.Write call to
capture the returned (written, err) result and handle/log the err appropriately.

---

Duplicate comments:
In `@internal/product/handler.go`:
- Around line 158-165: The w.Write call's error return is ignored; update the
response write in the same way as GetProductsByPlatform by capturing or
explicitly discarding the returned (int, error) from w.Write(buf.Bytes()) —
e.g., assign to _ , err or _ , _ — and if err != nil call
internal.HandleHttpError with a suitable internal.ErrorEnvelope and
http.StatusInternalServerError; target the write after encoding (bytes.Buffer /
json.NewEncoder.Encode and the w.Write call) to satisfy errcheck.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: e1f24a65-16d8-437c-9de6-c51782904559

📥 Commits

Reviewing files that changed from the base of the PR and between 3c2715c and 2a9f77e.

📒 Files selected for processing (3)
  • .gitignore
  • internal/platform/handler.go
  • internal/product/handler.go
✅ Files skipped from review due to trivial changes (1)
  • .gitignore

Comment thread internal/product/handler.go Outdated
…ers using `HandleHttpError` for consistent error reporting
@jlpdeveloper jlpdeveloper merged commit ef21d47 into main May 15, 2026
3 checks passed
@jlpdeveloper jlpdeveloper deleted the error-envelope branch May 15, 2026 00:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request skip-changelog Won't be added to the release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement RFC 7807 Problem Details error envelope

1 participant