| | 353 | class RenderingRescuedController < ActionController::Base |
|---|
| | 354 | around_filter :catch_errors |
|---|
| | 355 | |
|---|
| | 356 | #make sure the controller complains |
|---|
| | 357 | def rescue_action(e); raise e; end |
|---|
| | 358 | |
|---|
| | 359 | private |
|---|
| | 360 | def catch_errors |
|---|
| | 361 | begin |
|---|
| | 362 | yield |
|---|
| | 363 | rescue ErrorToRescue => ex |
|---|
| | 364 | render :text => ex |
|---|
| | 365 | end |
|---|
| | 366 | end |
|---|
| | 367 | end |
|---|
| | 368 | |
|---|
| | 369 | class ExceptionInBeforeFilterController < RenderingRescuedController |
|---|
| | 370 | before_filter :load_user |
|---|
| | 371 | |
|---|
| | 372 | def show |
|---|
| | 373 | render :text => "hello" |
|---|
| | 374 | end |
|---|
| | 375 | |
|---|
| | 376 | private |
|---|
| | 377 | def load_user |
|---|
| | 378 | raise ErrorToRescue.new("Load user failed.") |
|---|
| | 379 | end |
|---|
| | 380 | end |
|---|
| | 381 | |
|---|
| | 382 | class ExceptionInActionController < RenderingRescuedController |
|---|
| | 383 | before_filter :load_user |
|---|
| | 384 | |
|---|
| | 385 | def show |
|---|
| | 386 | raise ErrorToRescue.new("Show action failed.") |
|---|
| | 387 | end |
|---|
| | 388 | |
|---|
| | 389 | private |
|---|
| | 390 | def load_user |
|---|
| | 391 | @ran_filter ||= [] |
|---|
| | 392 | @ran_filter << 'load_user' |
|---|
| | 393 | end |
|---|
| | 394 | end |
|---|
| | 395 | |
|---|
| | 396 | class NonYieldingAroundFilterController < ActionController::Base |
|---|
| | 397 | before_filter :filter_one |
|---|
| | 398 | around_filter :yielding_filter |
|---|
| | 399 | before_filter :filter_two |
|---|
| | 400 | |
|---|
| | 401 | def index |
|---|
| | 402 | render :inline => "index" |
|---|
| | 403 | end |
|---|
| | 404 | |
|---|
| | 405 | #make sure the controller complains |
|---|
| | 406 | def rescue_action(e); raise e; end |
|---|
| | 407 | |
|---|
| | 408 | private |
|---|
| | 409 | def filter_one |
|---|
| | 410 | @filters ||= [] |
|---|
| | 411 | @filters << "filter_one" |
|---|
| | 412 | end |
|---|
| | 413 | |
|---|
| | 414 | def filter_two |
|---|
| | 415 | @filters ||= [] |
|---|
| | 416 | @filters << "filter_two" |
|---|
| | 417 | end |
|---|
| | 418 | |
|---|
| | 419 | def yielding_filter |
|---|
| | 420 | @filters ||= [] |
|---|
| | 421 | @filters << "zomg it didn't yield" |
|---|
| | 422 | end |
|---|
| | 423 | end |
|---|
| | 424 | |
|---|
| | 602 | def test_a_rendering_rescued_around_filter_when_exception_in_before_filter |
|---|
| | 603 | response = nil |
|---|
| | 604 | assert_nothing_raised do |
|---|
| | 605 | response = test_process(ExceptionInBeforeFilterController) |
|---|
| | 606 | end |
|---|
| | 607 | assert_equal "Load user failed.", response.body |
|---|
| | 608 | end |
|---|
| | 609 | |
|---|
| | 610 | def test_a_rendering_rescued_around_filter_when_exception_in_action |
|---|
| | 611 | response = nil |
|---|
| | 612 | assert_nothing_raised do |
|---|
| | 613 | response = test_process(ExceptionInActionController) |
|---|
| | 614 | end |
|---|
| | 615 | assert_equal "Show action failed.", response.body |
|---|
| | 616 | end |
|---|
| | 617 | |
|---|
| | 618 | def test_non_yielding_around_filters |
|---|
| | 619 | controller = NonYieldingAroundFilterController.new |
|---|
| | 620 | assert_nothing_raised do |
|---|
| | 621 | test_process(controller, "index") |
|---|
| | 622 | end |
|---|
| | 623 | end |
|---|
| | 624 | |
|---|