Skip to content

Fix mounted apps using the parent's error handlers (#1205) - #1546

Open
inchang-ing wants to merge 1 commit into
bottlepy:masterfrom
inchang-ing:fix-mounted-error-handler
Open

inchang-ing wants to merge 1 commit into
bottlepy:masterfrom
inchang-ing:fix-mounted-error-handler

Conversation

@inchang-ing

Copy link
Copy Markdown

Summary

Fixes #1205.

A natively mounted Bottle app's routes are merged into the parent's
router, so when a request hits a mounted route the parent's own
_handle/_cast cycle runs. _cast resolved the error handler off
self unconditionally, so an HTTPError raised inside a mounted app's
route was always rendered through the parent's handler, even when the
child had registered its own.

Fix

In Bottle._cast, when out is an HTTPError, look up the matched
route's owning app via request.route.app and prefer its error_handler.
Errors with no attributable route (e.g. an unmatched URL, where routing
never sets request.route) still fall back to the mount host's own
handler — so a parent's own routes are unaffected.

This is the same behavior the previously closed PRs #1523 / #1533 aimed
for, implemented at the error_handler selection point without
touching routing or dispatch otherwise.

Test plan

  • test/test_mount.py: added test_mount_error_handler (mounted child's
    own error(500) handler wins for an error raised in its route) and
    test_mount_error_handler_parent_unaffected (parent's own routes still
    use the parent's handler).
  • Full suite (pytest test/): 358 passed. The only failures are
    pre-existing Windows CRLF / template artifacts unrelated to this change
    (confirmed identical with and without this patch).

Resolves bottlepy#1205.

A natively mounted Bottle app's routes are merged into the parent's
router, so when a request hits a mounted route the parent's own
_handle/_cast cycle runs. _cast resolved the error handler off self
unconditionally, so an HTTPError raised inside a mounted app's route was
always rendered through the parent's handler, even when the child had
registered its own.

Now _cast looks up the matched route's owning app (request.route.app) and
prefers its error_handler. Errors with no attributable route (e.g. an
unmatched URL) still fall back to the mount host's own handler.

Adds test_mount_error_handler and test_mount_error_handler_parent_unaffected
to test/test_mount.py. The full suite shows no new regressions (the only
failing tests are pre-existing Windows CRLF/template artifacts).
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.

1 participant