Skip to content

fix: inherit the sub app's errorHandler in app.Route - #142

Open
poteto0 wants to merge 1 commit into
mainfrom
fix/141-route-inherits-error-handler
Open

fix: inherit the sub app's errorHandler in app.Route#142
poteto0 wants to merge 1 commit into
mainfrom
fix/141-route-inherits-error-handler

Conversation

@poteto0

@poteto0 poteto0 commented Jul 22, 2026

Copy link
Copy Markdown
Owner

closes #141

やったこと

Route(basePath, app) はサブアプリのルートとミドルウェアだけを親にコピーしていたため、サブアプリが OnError で設定したエラーハンドラは黙って捨てられていた。Hono の route() に合わせて、サブアプリが OnError を呼んでいた場合のみ、サブアプリ由来のハンドラをそのエラーハンドラで包んでから親に登録する。

  • サブアプリのエラーハンドラが返したエラーは、Hono と同様さらに親のエラーハンドラへ流れる
  • blowErrorHandler は引き継がない(Route はサブアプリの tasks をマージしないため対象が存在しない)
  • native / wasm の両方に適用

デフォルト判定のしかた

Go では関数値を == で比較できないので、errorHandlerコンストラクタでは nil のままにし、OnError が呼ばれたときだけセットするようにした。デフォルトはディスパッチ時に handleError で適用する。sub.errorHandler != nil がそのまま「カスタムされたか」の判定になり、フラグを別に持たずに済む。native と wasm でデフォルトの本文が違う("Internal Server Error" / err.Error())ので、そこは既存挙動のまま各ビルドに残してある。

ついでに

  • 完全に同一だった Route の実装を takibi_native.go / takibi_wasm.go から共有の route.go に移動(今回の変更を 2 箇所に書かずに済む)
  • route.Middleware が空のときは router.Use を呼ばないようにした(Use は毎回サブツリーを再合成するため、無駄な再合成を省ける)

docs

  • /docs の Sub-Routing ページに「errorHandler は引き継ぐ / Bindings と Blow タスクは引き継がない」を追記
  • ITakibi.Route の doc comment も同様に更新

検討したが今回やっていないこと

サブアプリのミドルウェアが返したエラーは、サブアプリのエラーハンドラを経由せず親に届く。ミドルウェアは親の trie に Use で登録され、合成時にラップの外側に来るため。単体で動かしたときとマウント後で挙動が変わる点なので気になるなら別途対応したい(エラー境界をミドルウェアとして表現する案がある)が、Issue の方針どおりハンドラのラップに留めた。

テスト

just ci (ut / lint / lint-wasm / build-wasm / fmt) 通過。Route のエラーハンドラ継承についてテーブルドリブンで 3 ケース追加。

🤖 Generated with Claude Code

Route() merged only the sub app's routes and middlewares, so an error
handler registered on the sub app via OnError was silently dropped and
errors from its handlers always hit the parent's handler.

Following hono's route(), a sub app's handlers are now wrapped with its
own error handler, and only when it actually called OnError. An error
returned by that handler still falls through to the parent's handler.

To tell "customized" from "default" without comparing func values,
errorHandler is left nil until OnError sets it and the default is
applied at dispatch time (handleError). Route is also moved out of
takibi_native.go / takibi_wasm.go into the shared route.go, where the
two copies were identical.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

Code Metrics Report

Coverage Code to Test Ratio Test Execution Time
96.5% 1:1.4 1s

Code coverage of files in pull request scope (95.0%)

Files Coverage
error_handler.go 85.7%
route.go 96.4%
takibi_native.go 94.9%

Reported by octocov

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.

app.Route() がサブアプリの errorHandler を引き継がない (Hono は引き継ぐ)

1 participant