Skip to content

Refactor handler Fastify internals for clearer route/error flow and safer response assertions - #19

Draft
mike-joel with Copilot wants to merge 3 commits into
mar2026-run/WEBINY-3/copilot_web+gpt_5.3_codexfrom
copilot/refactor-fastify-handler-code
Draft

Refactor handler Fastify internals for clearer route/error flow and safer response assertions#19
mike-joel with Copilot wants to merge 3 commits into
mar2026-run/WEBINY-3/copilot_web+gpt_5.3_codexfrom
copilot/refactor-fastify-handler-code

Conversation

Copilot AI commented Mar 6, 2026

Copy link
Copy Markdown

packages/handler/src/fastify.ts had high local complexity from repeated route bookkeeping and duplicated error-response serialization paths. This change streamlines those internals to improve readability/maintainability while preserving behavior, and adds focused assertions for sanitized error output.

  • Route bookkeeping cleanup

    • Extracted route table initialization into a dedicated helper (createDefinedRoutes).
    • Introduced small predicates (hasRoute, hasRouteInAnyMethod) to simplify duplicate route checks and reduce inline branching noise in throwOnDefinedRoute.
  • Error response path consolidation

    • Centralized shared Cache-Control: no-store headers.
    • Consolidated repeated 500-response serialization into reusable helpers (stringifyPublicError, sendErrorResponse).
    • Retained explicit auth/tenant branches while reducing duplication in the general error path.
  • Targeted test tightening for sanitized responses

    • Extended customErrorHandler coverage with auth and tenant-disabled cases.
    • Switched payload assertions to strict object equality on parsed JSON to verify only public fields are returned (no accidental data leakage).
const NO_STORE_HEADERS = { "Cache-Control": "no-store" } as const;

const sendErrorResponse = (reply: Reply.Interface, status: number, error: PublicErrorShape) => {
    return reply.status(status).headers(NO_STORE_HEADERS).send(stringifyPublicError(error));
};

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • download.cypress.io
    • Triggering command: /opt/hostedtoolcache/node/24.14.0/x64/bin/node /opt/hostedtoolcache/node/24.14.0/x64/bin/node dist/index.js --exec install (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits March 6, 2026 13:51
Co-authored-by: mike-joel <23259716+mike-joel@users.noreply.github.com>
Co-authored-by: mike-joel <23259716+mike-joel@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor fastify handler code for better quality Refactor handler Fastify internals for clearer route/error flow and safer response assertions Mar 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants