Skip to content

fix: handle synchronous handler/middleware#5270

Merged
schiller-manuel merged 1 commit intomainfrom
sync-handler
Sep 27, 2025
Merged

fix: handle synchronous handler/middleware#5270
schiller-manuel merged 1 commit intomainfrom
sync-handler

Conversation

@schiller-manuel
Copy link
Contributor

@schiller-manuel schiller-manuel commented Sep 27, 2025

Summary by CodeRabbit

  • Bug Fixes

    • Prevents crashes by safely handling special responses during request processing.
    • Ensures downstream results are correctly merged into the request context.
    • Preserves existing behavior for non-special errors.
  • Reliability

    • Improves error handling around request flow to avoid unhandled exceptions and return proper responses.
    • Standardizes how results from subsequent handlers are incorporated, leading to more consistent behavior across requests.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Sep 27, 2025

Walkthrough

Adds try/catch around middleware execution, captures and merges middleware results into context via handleCtxResult, integrates next() results similarly, and handles “special” error-responses by converting them into a response result instead of throwing; non-special errors continue to throw.

Changes

Cohort / File(s) Summary
Middleware error/ctx handling
packages/start-server-core/src/createStartHandler.ts
Wrap middleware chain in try/catch; capture middleware/next results; merge via handleCtxResult; detect special responses on error and convert to result; preserve throw for non-special errors.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant R as Request
  participant H as StartHandler
  participant M as Middleware
  participant N as next()
  participant C as handleCtxResult
  participant S as isSpecialResponse

  R->>H: invoke handler
  H->>M: call middleware(ctx, next)
  rect rgba(200,230,255,0.3)
    note over H,M: New try/catch around middleware chain
    M->>N: invoke next()
    N-->>M: nextResult
    M->>C: handleCtxResult(ctx, nextResult)
    C-->>M: mergedCtx
  end
  M-->>H: result or mergedCtx
  H->>C: handleCtxResult(ctx, result)
  C-->>H: finalCtx/response
  H-->>R: response

  opt Error thrown in middleware/next
    H->>S: isSpecialResponse(error)
    alt Special response
      note over H: Convert error to response result
      H->>C: handleCtxResult(ctx, responseFromError)
      C-->>H: finalCtx/response
      H-->>R: response
    else Non-special error
      H-->>R: throw error
    end
  end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

I hopped through stacks where middlewares play,
Caught tricky throws before they ran away—
If “special,” I tuck them into a tidy reply,
If not, I let them flutter and fly.
Context now grows, neat as a clover bed—
Next hops merge, forward we tread. 🐇✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title “fix: handle synchronous handler/middleware” succinctly captures the core update of adding support for synchronous handlers and middleware via try/catch and result merging logic, clearly labeling it as a bug fix without extraneous detail.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch sync-handler

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

@nx-cloud
Copy link

nx-cloud bot commented Sep 27, 2025

View your CI Pipeline Execution ↗ for commit 07c412e

Command Status Duration Result
nx affected --targets=test:eslint,test:unit,tes... ✅ Succeeded 9m 2s View ↗
nx run-many --target=build --exclude=examples/*... ✅ Succeeded 1m 24s View ↗

☁️ Nx Cloud last updated this comment at 2025-09-27 23:18:17 UTC

@pkg-pr-new
Copy link

pkg-pr-new bot commented Sep 27, 2025

More templates

@tanstack/arktype-adapter

npm i https://pkg.pr.new/TanStack/router/@tanstack/arktype-adapter@5270

@tanstack/directive-functions-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/directive-functions-plugin@5270

@tanstack/eslint-plugin-router

npm i https://pkg.pr.new/TanStack/router/@tanstack/eslint-plugin-router@5270

@tanstack/history

npm i https://pkg.pr.new/TanStack/router/@tanstack/history@5270

@tanstack/nitro-v2-vite-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/nitro-v2-vite-plugin@5270

@tanstack/react-router

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router@5270

@tanstack/react-router-devtools

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router-devtools@5270

@tanstack/react-router-ssr-query

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router-ssr-query@5270

@tanstack/react-start

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start@5270

@tanstack/react-start-client

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-client@5270

@tanstack/react-start-server

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-server@5270

@tanstack/router-cli

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-cli@5270

@tanstack/router-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-core@5270

@tanstack/router-devtools

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-devtools@5270

@tanstack/router-devtools-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-devtools-core@5270

@tanstack/router-generator

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-generator@5270

@tanstack/router-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-plugin@5270

@tanstack/router-ssr-query-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-ssr-query-core@5270

@tanstack/router-utils

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-utils@5270

@tanstack/router-vite-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-vite-plugin@5270

@tanstack/server-functions-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/server-functions-plugin@5270

@tanstack/solid-router

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router@5270

@tanstack/solid-router-devtools

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router-devtools@5270

@tanstack/solid-start

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start@5270

@tanstack/solid-start-client

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-client@5270

@tanstack/solid-start-server

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-server@5270

@tanstack/start-client-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-client-core@5270

@tanstack/start-plugin-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-plugin-core@5270

@tanstack/start-server-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-server-core@5270

@tanstack/start-static-server-functions

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-static-server-functions@5270

@tanstack/start-storage-context

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-storage-context@5270

@tanstack/valibot-adapter

npm i https://pkg.pr.new/TanStack/router/@tanstack/valibot-adapter@5270

@tanstack/virtual-file-routes

npm i https://pkg.pr.new/TanStack/router/@tanstack/virtual-file-routes@5270

@tanstack/zod-adapter

npm i https://pkg.pr.new/TanStack/router/@tanstack/zod-adapter@5270

commit: 07c412e

Copy link
Contributor

@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

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 07a123d and 07c412e.

📒 Files selected for processing (1)
  • packages/start-server-core/src/createStartHandler.ts (1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
**/*.{ts,tsx}

📄 CodeRabbit inference engine (AGENTS.md)

Use TypeScript in strict mode with extensive type safety across the codebase

Files:

  • packages/start-server-core/src/createStartHandler.ts
packages/{*-start,start-*}/**

📄 CodeRabbit inference engine (AGENTS.md)

Name and place Start framework packages under packages/-start/ or packages/start-/

Files:

  • packages/start-server-core/src/createStartHandler.ts
🪛 Biome (2.1.2)
packages/start-server-core/src/createStartHandler.ts

[error] 519-519: Catch clause variable type annotation must be 'any' or 'unknown' if specified.

(parse)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: Preview
  • GitHub Check: Test

Comment on lines +519 to +527
} catch (err: TODO) {
if (isSpecialResponse(err)) {
return {
result = {
response: err,
}
} else {
throw err
}

throw err
})
}
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Fix lint failure in catch clause

Biome reports “Catch clause variable type annotation must be 'any' or 'unknown'” for catch (err: TODO), which breaks lint and will block CI. Please swap the annotation to unknown (or remove it) so the pipeline passes.

-    } catch (err: TODO) {
+    } catch (err: unknown) {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
} catch (err: TODO) {
if (isSpecialResponse(err)) {
return {
result = {
response: err,
}
} else {
throw err
}
throw err
})
}
} catch (err: unknown) {
if (isSpecialResponse(err)) {
result = {
response: err,
}
} else {
throw err
}
}
🧰 Tools
🪛 Biome (2.1.2)

[error] 519-519: Catch clause variable type annotation must be 'any' or 'unknown' if specified.

(parse)

🤖 Prompt for AI Agents
In packages/start-server-core/src/createStartHandler.ts around lines 519 to 527,
the catch clause currently uses an invalid annotation `catch (err: TODO)` which
triggers the Biome lint rule requiring catch variables be typed as 'any' or
'unknown'; change the annotation to `catch (err: unknown)` (or remove the type
entirely) so the catch variable complies with lint, then ensure subsequent uses
of `err` are narrowed (e.g., use type guards like isSpecialResponse(err)) or
properly asserted before treating it as a specific type.

@schiller-manuel schiller-manuel merged commit 478d283 into main Sep 27, 2025
6 checks passed
@schiller-manuel schiller-manuel deleted the sync-handler branch September 27, 2025 23:32
@coderabbitai coderabbitai bot mentioned this pull request Nov 18, 2025
naoya7076 pushed a commit to naoya7076/router that referenced this pull request Feb 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant