From: Tom Hughes Date: Sun, 12 Dec 2010 16:58:34 +0000 (+0000) Subject: Allow the client to request errors as an XML document X-Git-Tag: live~6247^2^2~46 X-Git-Url: https://git.openstreetmap.org/rails.git/commitdiff_plain/f20a85a5c5c68f4b87a3f4d9faa1de0ba780ce3b Allow the client to request errors as an XML document --- diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index f9a5f88e1..bc71f275f 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 diff --git a/test/functional/changeset_controller_test.rb b/test/functional/changeset_controller_test.rb index befa8192a..38f301524 100644 --- a/test/functional/changeset_controller_test.rb +++ b/test/functional/changeset_controller_test.rb @@ -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. diff --git a/test/test_helper.rb b/test/test_helper.rb index 4972ee6d6..6a618c8d3 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -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