Skip to content

Conversation

@mohankumarelec
Copy link

@mohankumarelec mohankumarelec commented Nov 30, 2025

Problem

Hono RPC client doesn't infer response types from global error handlers like app.onError() or global middlewares like app.use("*"). This means developers lose type safety for common error responses like 500 and so on when using the RPC client.

Solution

Added ApplyGlobalResponse<App, ResponseType, StatusCode, Format> helper that merges global error response types into all routes.

Usage:

const app = new Hono()
  .get('/users', (c) => c.json({ users: [...] }))
  .onError((err, c) => c.json({ error: err.message }, 500))

// Apply global error type
type AppWithErrors = ApplyGlobalResponse<typeof app, { error: string }, 500, 'json'>

const client = hc<AppWithErrors>('http://api.example.com')
// Now client knows about both success and error responses

The author should do the following, if applicable

  • Add tests
  • Run tests
  • bun run format:fix && bun run lint:fix to format the code
  • Add TSDoc/JSDoc to document the code

@mohankumarelec
Copy link
Author

Please let me know if the changes looks good, so that i can create a PR for the honojs/website as well so that the docs are updated. Thanks

@codecov
Copy link

codecov bot commented Dec 1, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.49%. Comparing base (b06005a) to head (580db4b).
⚠️ Report is 7 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4556   +/-   ##
=======================================
  Coverage   91.49%   91.49%           
=======================================
  Files         172      172           
  Lines       11230    11230           
  Branches     3257     3257           
=======================================
  Hits        10275    10275           
  Misses        954      954           
  Partials        1        1           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@yusukebe
Copy link
Member

@mohankumarelec Thank you for the PR!

Hey @NamesMT What do you think of this feature? I think this is an interesting idea. But I don't know if the API is best or not.

@NamesMT
Copy link
Contributor

NamesMT commented Dec 20, 2025

Hi @yusukebe, sorry for the late response, I didn't see it earlier.

I like the util, it is a separate util that does not modify any of the current types and can be used for some edge cases where the user needs to add a global response type to their API server, it is especially useful for process layers before Hono, like the platform's process layer that might exit-early before continuing to the backend function, or another app mounting Hono.

And it could be used by user to temporarily fix the problem we have with some global handlers like onError not included in our type chain, which we can improve later for a better API.

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.

3 participants