Permalink
Browse files

handle 404 error responses that are in HTML

  • Loading branch information...
1 parent ed4a0db commit 6208951493731f97f942a4fb667155e83443304f @nuex nuex committed Mar 24, 2012
Showing with 56 additions and 10 deletions.
  1. +15 −6 lib/ability/error.rb
  2. +31 −0 test/fixtures/errors/404.html
  3. +5 −0 test/test_helper.rb
  4. +5 −4 test/unit/error_test.rb
View
@@ -24,12 +24,21 @@ class Error
# Generate an error class from a given hash of parsed error response data
def self.generate(error)
- code = error["code"]
- message = error["message"]
- details = error["details"]["detail"].inject({}) { |hash, detail|
- hash[detail["key"]] = detail["value"]
- hash
- }
+
+ if error["head"]
+ # Handling a 404 HTML error response
+ code = "ResourceNotFound"
+ message = error["body"]["p"]["content"].gsub(" Reason:\n", "")
+ details = nil
+ else
+ code = error["code"]
+ message = error["message"]
+ details = error["details"]["detail"].inject({}) { |hash, detail|
+ hash[detail["key"]] = detail["value"]
+ hash
+ }
+ end
+
new(code, message, details)
end
@@ -0,0 +1,31 @@
+<html>
+<head>
+<meta content='text/html; charset=ISO-8859-1' http-equiv='Content-Type'/>
+<title>Error 404 NOT_FOUND</title>
+</head>
+<body><h2>HTTP ERROR 404</h2>
+<p>Problem accessing /portal/services. Reason:
+<pre> NOT_FOUND</pre></p><hr/><i><small>Powered by
+Jetty://</small></i><br/>
+<br/>
+<br/>
+<br/>
+<br/>
+<br/>
+<br/>
+<br/>
+<br/>
+<br/>
+<br/>
+<br/>
+<br/>
+<br/>
+<br/>
+<br/>
+<br/>
+<br/>
+<br/>
+<br/>
+
+</body>
+</html>
View
@@ -4,6 +4,11 @@
class Test::Unit::TestCase
+ # Load a fixture
+ def load_fixture(relative_path)
+ File.read(File.new(File.expand_path(File.join(File.dirname(__FILE__), "fixtures", relative_path))))
+ end
+
# Load an error response XML fixture
def error_xml(fixture)
load_xml(:errors, nil, fixture)
View
@@ -34,10 +34,11 @@ def test_message_is_passed_along_to_exception
assert_equal @message, e.error.message
end
- private
-
- def xml(raw)
- REXML::Document.new(raw)
+ def test_handles_html_404_messages
+ error = Ability::Error.generate(Ability::Parser.parse(load_fixture("errors/404.html")))
+ assert_equal "ResourceNotFound", error.code
+ assert_equal "Problem accessing /portal/services.", error.message
+ assert_equal nil, error.details
end
end

0 comments on commit 6208951

Please sign in to comment.