Skip to content

refactor(daemon): return errors from platform handlers - #1540

Merged
skevetter merged 1 commit into
mainfrom
refactor/daemon-platform-handler-errors
Oct 18, 2026
Merged

skevetter merged 1 commit into
mainfrom
refactor/daemon-platform-handler-errors

Conversation

@skevetter

@skevetter skevetter commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Platform HTTP handlers in pkg/daemon/platform/local_server.go now return error. A small adapter in handler.go (handlerFunc.ServeHTTP) turns the returned error into the response.

  • badRequest, forbidden, missingQueryParam wrap errors in a statusError with a status code.
  • Unwrapped errors default to 500. Wrapped status errors keep their code via errors.AsType.
  • writeJSON replaces tryJSON with the same json.Marshal output, no trailing newline, and the same Content-Type.
  • Routes use mux.Handle(..., handlerFunc(...)). watchWorkspaces and throttle are streaming and unchanged.

Why

Each handler repeated http.Error plus return. Returning errors lets one place set status and text. Status codes, message text, and response bodies are unchanged.

Validation

  • mise run cli:format
  • mise run cli:lint:ci
  • golangci-lint run --new-from-rev=origin/main ./pkg/...: 0 issues
  • go build ./...
  • go test ./pkg/daemon/...
  • GOOS=windows go build ./pkg/daemon/...
  • GOOS=linux go build ./pkg/daemon/...

New tests in handler_test.go cover the adapter status mapping (plain, bad request, forbidden, wrapped, nil) and the missing-query-parameter 400 responses.

CodeRabbit CLI

coderabbit review --agent --committed --base-commit adbf08d32: completed, 0 findings on the 3 changed files.

Auto-merge

Eligible under the auto-refactor policy: 3 files (2 non-test), 406 changed lines (255 added, 151 removed), behaviour-preserving, no existing tests changed.

Summary by CodeRabbit

  • Bug Fixes
    • HTTP endpoints now return consistent error responses: invalid requests receive a 400 status, access-denied responses retain a 403 status, and unexpected errors receive a 500 status.
    • Error messages are included in responses, including when errors are wrapped.
    • Successful JSON responses remain available, and requests for unavailable workspaces or registries retain their existing response behavior.

@skevetter skevetter added the auto-refactor Automated refactor PR label Oct 18, 2026
@netlify

netlify Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for devsydev canceled.

Name Link
🔨 Latest commit 6a49736
🔍 Latest deploy log https://app.netlify.com/projects/devsydev/deploys/6ad463fc759345b1bdde2f46

@coderabbitai

coderabbitai Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 1bea127f-12c1-4ed5-b3c1-b6ebcc19a0bd
📥 Commits

Reviewing files that changed from the base of the PR and between adbf08d and 6a49736.

📒 Files selected for processing: 3
  • pkg/daemon/platform/handler.go
  • pkg/daemon/platform/handler_test.go
  • pkg/daemon/platform/local_server.go

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 0 remain after this review.

📝 Walkthrough

Platform HTTP handlers now return errors to a shared wrapper, which selects the HTTP status and writes the error response. Successful JSON responses use a shared helper. Tests cover error responses and missing required query parameters.

Changes: Platform HTTP handlers

Layer / File(s) Summary
Shared response handling
pkg/daemon/platform/handler.go, pkg/daemon/platform/local_server.go, pkg/daemon/platform/handler_test.go
Adds status error helpers, centralized HTTP error handling, and JSON response writing. Routes wrap ordinary handlers with handlerFunc. Tests check response codes and bodies.
Status, profile, and project handlers
pkg/daemon/platform/local_server.go
These handlers return errors instead of writing most error responses directly. Successful JSON responses use writeJSON.
Workspace handlers
pkg/daemon/platform/local_server.go
Workspace handlers return errors and use writeJSON for successful results. A missing workspace lookup still returns HTTP 200 with an empty body.
Credential handlers and required query parameters
pkg/daemon/platform/local_server.go, pkg/daemon/platform/handler_test.go
Credential handlers return errors and use writeJSON for matching results. Tests check HTTP 400 responses for missing uid, host, or server parameters.

Priority: ⬇️ Low

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Refactor

Sequence Diagram(s)

sequenceDiagram
  participant HTTPClient
  participant handlerFunc
  participant localServerStatus as localServer.status
  participant writeJSON
  participant ResponseWriter
  HTTPClient->>handlerFunc: send request
  handlerFunc->>localServerStatus: invoke wrapped handler
  alt handler succeeds
    localServerStatus->>writeJSON: marshal status response
    writeJSON->>ResponseWriter: set JSON header and write body
  else handler returns error
    handlerFunc->>ResponseWriter: write status and error text
  end
Loading

Suggested reviewers: pascalbreuninger

Merge Risk: ⚪ Minimal · up to 6a497

The reviewed response changes have no established issue blocking merge; proceed with normal checks.

Security architecture risk: ⚪ Minimal · up to 6a497

The shared response handling preserves existing routes, credential access paths, backend operations and response formats. No material security risk introduced or worsened by this refactor was identified.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The changed contract affects ordinary local-server responses, including credential and mutation endpoints. Because route reachability, forwarding and backend inputs are preserved, the inspected change does not expand the independently reachable credential or workspace scope.

Trust Boundaries and Controls

  • observed — The adapter neither replaces caller identity nor introduces an authorization decision. Existing local-server transport assumptions and downstream client authority remain in place, as do the outer panic-recovery and logging wrappers. This does not establish which external processes can access the deployed daemon socket.

Resilience and Maintainability Implications

  • observed — Shutdown cleanup remains owned by localServer.Close. Its buffered stop-channel send and listener-close handling are unchanged, so repetition and concurrency limitations are not introduced by centralized error rendering.
Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 12.50% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 8 functions across 3 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: platform handlers now return errors.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
  • Fix all pre-merge checks with AI
✨ Finishing Touches
✨ Simplify code
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

@netlify

netlify Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for images-devsy-sh canceled.

Name Link
🔨 Latest commit 6a49736
🔍 Latest deploy log https://app.netlify.com/projects/images-devsy-sh/deploys/6ad463fcf48eddc8ebf3f748

@skevetter
skevetter marked this pull request as ready for review October 18, 2026 07:41
@mergify

mergify Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

This pull request does not currently match the merge queue conditions, so it cannot be queued from here. The box comes back if it matches again.

@skevetter
skevetter merged commit db85eb5 into main Oct 18, 2026
286 of 291 checks passed
@skevetter
skevetter deleted the refactor/daemon-platform-handler-errors branch October 18, 2026 07:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

auto-refactor Automated refactor PR size/l

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant