Allow the client to request errors as an XML document

This commit is contained in:
Tom Hughes 2010-12-12 16:58:34 +00:00
parent 3472c1d429
commit f20a85a5c5
3 changed files with 42 additions and 1 deletions

View file

@ -162,8 +162,19 @@ class ApplicationController < ActionController::Base
def report_error(message, status = :bad_request) def report_error(message, status = :bad_request)
# Todo: some sort of escaping of problem characters in the message # Todo: some sort of escaping of problem characters in the message
response.headers['Error'] = message response.headers['Error'] = message
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 render :text => message, :status => status
end end
end
def set_locale def set_locale
response.header['Vary'] = 'Accept-Language' response.header['Vary'] = 'Accept-Language'

View file

@ -1024,6 +1024,32 @@ EOF
end end
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 # when we make some simple changes we get the same changes back from the
# diff download. # diff download.

View file

@ -121,6 +121,10 @@ class ActiveSupport::TestCase
@request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}") @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
end end
def error_format(format)
@request.env["HTTP_X_ERROR_FORMAT"] = format
end
def content(c) def content(c)
@request.env["RAW_POST_DATA"] = c.to_s @request.env["RAW_POST_DATA"] = c.to_s
end end