diff --git a/lib/rock_rms/error.rb b/lib/rock_rms/error.rb index 8f7d5d7..06a61e0 100644 --- a/lib/rock_rms/error.rb +++ b/lib/rock_rms/error.rb @@ -46,17 +46,23 @@ def on_complete(env) private def html_body?(body) - body.lstrip.start_with?('') + /()|()/ =~ body end def check_html_error(env) return unless html_body?(env[:body]) - return unless /An error has occurred while processing your request/ =~ env[:body] + if /An error has occurred while processing your request/ =~ env[:body] + raise RockRMS::InternalServerError, error_message( + status: 500, url: env[:url], body: 'Unknown API error.' + ) + end - raise RockRMS::InternalServerError, error_message( - status: 500, url: env[:url], body: 'Unknown API error.' - ) + if /Page Not Found/ =~ env[:body] + raise RockRMS::NotFound, error_message( + status: 404, url: env[:url], body: 'Page not found.' + ) + end end def error_message(env) diff --git a/lib/rock_rms/parse_oj.rb b/lib/rock_rms/parse_oj.rb index 6fd361a..754da19 100644 --- a/lib/rock_rms/parse_oj.rb +++ b/lib/rock_rms/parse_oj.rb @@ -15,7 +15,7 @@ def on_complete(env) private def html_body?(body) - body.start_with?('') + /()|()/ =~ body end def empty_body?(body) diff --git a/spec/rock_rms/error_spec.rb b/spec/rock_rms/error_spec.rb index 31ca5ce..76a71d4 100644 --- a/spec/rock_rms/error_spec.rb +++ b/spec/rock_rms/error_spec.rb @@ -75,10 +75,16 @@ def expect_success end context 'html error responses' do - it 'raises exception for html error' do - body = File.read('spec/rock_rms/fixtures/html_error.html') + it 'raises exception for internal error' do + body = File.read('spec/rock_rms/fixtures/internal_error.html') stub(200, body) expect_failure(500, 'Unknown API error.') end + + it 'raises exception for not found error' do + body = File.read('spec/rock_rms/fixtures/not_found_error.html') + stub(200, body) + expect_failure(404, 'Page not found.') + end end end diff --git a/spec/rock_rms/fixtures/html_error.html b/spec/rock_rms/fixtures/internal_error.html similarity index 100% rename from spec/rock_rms/fixtures/html_error.html rename to spec/rock_rms/fixtures/internal_error.html diff --git a/spec/rock_rms/fixtures/not_found_error.html b/spec/rock_rms/fixtures/not_found_error.html new file mode 100644 index 0000000..c006fa5 --- /dev/null +++ b/spec/rock_rms/fixtures/not_found_error.html @@ -0,0 +1,79 @@ + + + Object moved + + +

Object moved to here.

+ + + + + + + + Rock - Page Not Found + + + + + + + + + + + + + + +
+ + + + +
+ + +
+
+
+
+

We Can't Find That Page

+ +

+ +

+ +

+ Sorry, but the page you are looking for can not be found. + Check the address of the page and see your administrator if + you still need assistance. +

+
+
+
+
+
+
+ +