Commit 62c5418f3b4a5b7a7d1d7e9979a746e278b79c5b
1 parent
51dcd76c
Exists in
master
and in
29 other branches
Rendering a "not found" message for missing docs
It will also happen when a user tries to browse the documentation but it was not generated by the site administrator.
Showing
7 changed files
with
33 additions
and
4 deletions
Show diff stats
app/controllers/public/doc_controller.rb
app/models/doc_item.rb
app/models/doc_section.rb
... | ... | @@ -20,7 +20,12 @@ class DocSection < DocItem |
20 | 20 | if id.blank? |
21 | 21 | root(language) |
22 | 22 | else |
23 | - all(language, force).find {|item| item.id == id } | |
23 | + section = all(language, force).find {|item| item.id == id } | |
24 | + if section | |
25 | + section | |
26 | + else | |
27 | + raise DocItem::NotFound | |
28 | + end | |
24 | 29 | end |
25 | 30 | end |
26 | 31 | |
... | ... | @@ -50,7 +55,7 @@ class DocSection < DocItem |
50 | 55 | [ |
51 | 56 | File.join(dir, "#{id}#{language_suffix}.xhtml"), |
52 | 57 | File.join(dir, "#{id}.en.xhtml") |
53 | - ].find {|file| File.exist?(file) } | |
58 | + ].find {|file| File.exist?(file) } || raise(DocItem::NotFound) | |
54 | 59 | end |
55 | 60 | |
56 | 61 | def load_items | ... | ... |
app/models/doc_topic.rb
test/functional/doc_controller_test.rb
... | ... | @@ -40,5 +40,11 @@ class DocControllerTest < ActionController::TestCase |
40 | 40 | assert_equal 'pt', assigns(:topic).language |
41 | 41 | end |
42 | 42 | |
43 | + should 'bail out gracefully for unexisting sections or topics' do | |
44 | + assert_nothing_raised do | |
45 | + get :section, :section => 'something-very-unlikely' | |
46 | + get :section, :section => 'something-very-unlikely', :topic => 'other-thing-very-unlikely' | |
47 | + end | |
48 | + end | |
43 | 49 | |
44 | 50 | end | ... | ... |
test/unit/doc_section_test.rb
... | ... | @@ -91,8 +91,10 @@ class DocSectionTest < ActiveSupport::TestCase |
91 | 91 | end |
92 | 92 | end |
93 | 93 | |
94 | - should 'not load null section (the root) for unexisting sections' do | |
95 | - assert_nil DocSection.find('something-very-unlikely') | |
94 | + should 'raise DocItem::NotFound when loading unexisting section' do | |
95 | + assert_raise DocItem::NotFound do | |
96 | + DocSection.find('something-very-unlikely') | |
97 | + end | |
96 | 98 | end |
97 | 99 | |
98 | 100 | end | ... | ... |
test/unit/doc_topic_test.rb
... | ... | @@ -18,4 +18,11 @@ class DocTopicTest < ActiveSupport::TestCase |
18 | 18 | assert_equal 'pt', doc.language |
19 | 19 | assert_equal 'Teste da documentação', doc.title |
20 | 20 | end |
21 | + | |
22 | + should 'raise DocTopic::NotFound when trying to load an unexisting topic' do | |
23 | + assert_raise DocItem::NotFound do | |
24 | + DocTopic.loadfile('/path/to/unexisting/file.en.xhtml') | |
25 | + end | |
26 | + end | |
27 | + | |
21 | 28 | end | ... | ... |