Skip to content
This repository
Browse code

Adding a call to logger from params_parser to give detailed debug inf…

…ormation when invalid xml or json is posted

[#2481 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information...
commit 679128da58636f3ec96e24fcb7daac24666b2dad 1 parent 911acc1
authored August 11, 2009 jeremy committed August 15, 2009
6  actionpack/lib/action_dispatch/middleware/params_parser.rb
@@ -47,6 +47,8 @@ def parse_formatted_parameters(env)
47 47
             false
48 48
         end
49 49
       rescue Exception => e # YAML, XML or Ruby code block errors
  50
+        logger.debug "Error occurred while parsing request parameters.\nContents:\n\n#{request.raw_post}"
  51
+
50 52
         raise
51 53
           { "body" => request.raw_post,
52 54
             "content_type" => request.content_type,
@@ -67,5 +69,9 @@ def content_type_from_legacy_post_data_format_header(env)
67 69
 
68 70
         nil
69 71
       end
  72
+
  73
+      def logger
  74
+        defined?(Rails.logger) ? Rails.logger : Logger.new($stderr)
  75
+      end
70 76
   end
71 77
 end
28  actionpack/test/dispatch/request/json_params_parsing_test.rb
@@ -30,16 +30,36 @@ def teardown
30 30
     )
31 31
   end
32 32
 
  33
+  test "logs error if parsing unsuccessful" do
  34
+    with_test_routing do
  35
+      begin
  36
+        $stderr = StringIO.new
  37
+        json = "[\"person]\": {\"name\": \"David\"}}"
  38
+        post "/parse", json, {'CONTENT_TYPE' => 'application/json'}
  39
+        assert_response :error
  40
+        $stderr.rewind && err = $stderr.read
  41
+        assert err =~ /Error occurred while parsing request parameters/
  42
+      ensure
  43
+        $stderr = STDERR
  44
+      end
  45
+    end
  46
+  end
  47
+
33 48
   private
34 49
     def assert_parses(expected, actual, headers = {})
  50
+      with_test_routing do
  51
+        post "/parse", actual, headers
  52
+        assert_response :ok
  53
+        assert_equal(expected, TestController.last_request_parameters)
  54
+      end
  55
+    end
  56
+
  57
+    def with_test_routing
35 58
       with_routing do |set|
36 59
         set.draw do |map|
37 60
           map.connect ':action', :controller => "json_params_parsing_test/test"
38 61
         end
39  
-
40  
-        post "/parse", actual, headers
41  
-        assert_response :ok
42  
-        assert_equal(expected, TestController.last_request_parameters)
  62
+        yield
43 63
       end
44 64
     end
45 65
 end
17  actionpack/test/dispatch/request/xml_params_parsing_test.rb
@@ -38,6 +38,21 @@ def teardown
38 38
     end
39 39
   end
40 40
 
  41
+  test "logs error if parsing unsuccessful" do
  42
+    with_test_routing do
  43
+      begin
  44
+        $stderr = StringIO.new
  45
+        xml = "<person><name>David</name><avatar type='file' name='me.jpg' content_type='image/jpg'>#{ActiveSupport::Base64.encode64('ABC')}</avatar></pineapple>"
  46
+        post "/parse", xml, default_headers
  47
+        assert_response :error
  48
+        $stderr.rewind && err = $stderr.read
  49
+        assert err =~ /Error occurred while parsing request parameters/
  50
+      ensure
  51
+        $stderr = STDERR
  52
+      end
  53
+    end
  54
+  end
  55
+
41 56
   test "parses multiple files" do
42 57
     xml = <<-end_body
43 58
       <person>
@@ -85,4 +100,4 @@ class LegacyXmlParamsParsingTest < XmlParamsParsingTest
85 100
     def default_headers
86 101
       {'HTTP_X_POST_DATA_FORMAT' => 'xml'}
87 102
     end
88  
-end
  103
+end

0 notes on commit 679128d

Please sign in to comment.
Something went wrong with that request. Please try again.