Allow the client to request errors as an XML document
authorTom Hughes <tom@compton.nu>
Sun, 12 Dec 2010 16:58:34 +0000 (16:58 +0000)
committerTom Hughes <tom@compton.nu>
Wed, 15 Dec 2010 23:58:59 +0000 (23:58 +0000)
app/controllers/application_controller.rb
test/functional/changeset_controller_test.rb
test/test_helper.rb

index f9a5f88e1a8a321fce1901b98b6f43afbcf2a8aa..bc71f275f1e8607d25826963df89f2fe0ba5b9a9 100644 (file)
@@ -162,7 +162,18 @@ class ApplicationController < ActionController::Base
   def report_error(message, status = :bad_request)
     # Todo: some sort of escaping of problem characters in the message
     response.headers['Error'] = message
-    render :text => message, :status => status
+
+    if request.headers['X-Error-Format'] and
+       request.headers['X-Error-Format'].downcase == "xml"
+      result = OSM::API.new.get_xml_doc
+      result.root.name = "osmError"
+      result.root << (XML::Node.new("status") << interpret_status(status))
+      result.root << (XML::Node.new("message") << message)
+
+      render :text => result.to_s, :content_type => "text/xml"
+    else
+      render :text => message, :status => status
+    end
   end
   
   def set_locale
index befa8192a6b6d842e1321fa92e736bd52f135eda..38f301524243a70d468c047c8e6c5744515a45c8 100644 (file)
@@ -1024,6 +1024,32 @@ EOF
     end
   end
 
+  ##
+  # test that the X-Error-Format header works to request XML errors
+  def test_upload_xml_errors
+    basic_authorization users(:public_user).email, "test"
+
+    # try and delete a node that is in use
+    diff = XML::Document.new
+    diff.root = XML::Node.new "osmChange"
+    delete = XML::Node.new "delete"
+    diff.root << delete
+    delete << current_nodes(:node_used_by_relationship).to_xml_node
+
+    # upload it
+    content diff
+    error_format "xml"
+    post :upload, :id => 2
+    assert_response :success, 
+      "failed to return error in XML format"
+
+    # check the returned payload
+    assert_select "osmError[version=#{API_VERSION}][generator=\"OpenStreetMap server\"]", 1
+    assert_select "osmError>status", 1
+    assert_select "osmError>message", 1
+
+  end
+
   ##
   # when we make some simple changes we get the same changes back from the 
   # diff download.
index 4972ee6d6e6b90b1d7fb14c8f2e54ed687fdf9e0..6a618c8d3e6073bf24e1f2c7bbf77fd61269a4ba 100644 (file)
@@ -121,6 +121,10 @@ class ActiveSupport::TestCase
     @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
   end
 
+  def error_format(format)
+    @request.env["HTTP_X_ERROR_FORMAT"] = format
+  end
+
   def content(c)
     @request.env["RAW_POST_DATA"] = c.to_s
   end