api06: simplify exception handling and add exception handling to the diff

upload.
This commit is contained in:
Gabriel Ebner 2008-05-13 12:15:06 +00:00
parent 4a9ee4c736
commit 7151fa05e4
5 changed files with 23 additions and 30 deletions

View file

@ -184,5 +184,8 @@ class ChangesetController < ApplicationController
end
render :text => res.to_s, :content_type => "text/xml"
rescue OSM::APIError => ex
render ex.render_opts
end
end

View file

@ -72,10 +72,8 @@ class NodeController < ApplicationController
node.delete_with_history(@user)
rescue ActiveRecord::RecordNotFound
render :nothing => true, :status => :not_found
rescue OSM::APIAlreadyDeletedError
render :text => "", :status => :gone
rescue OSM::APIPreconditionFailedError
render :text => "", :status => :precondition_failed
rescue OSM::APIError => ex
render ex.render_opts
end
end

View file

@ -49,10 +49,6 @@ class RelationController < ApplicationController
begin
relation = Relation.find(params[:id])
new_relation = Relation.from_xml(request.raw_post)
if new_relation.version != relation.version
render :text => "Version mismatch: Provided " + new_relation.version.to_s + ", server had: " + relation.version.to_s, :status => :bad_request
return
end
if new_relation and new_relation.id == relation.id
relation.update_from new_relation, user
@ -62,13 +58,8 @@ class RelationController < ApplicationController
end
rescue ActiveRecord::RecordNotFound
render :nothing => true, :status => :not_found
rescue OSM::APIPreconditionFailedError
render :text => "", :status => :precondition_failed
rescue OSM::APIVersionMismatchError => ex
render :text => "Version mismatch: Provided " + ex.provided.to_s +
", server had: " + ex.latest.to_s, :status => :bad_request
rescue
render :nothing => true, :status => :internal_server_error
rescue OSM::APIError => ex
render ex.render_opts
end
end
@ -77,14 +68,10 @@ class RelationController < ApplicationController
begin
relation = Relation.find(params[:id])
relation.delete_with_history(@user)
rescue OSM::APIAlreadyDeletedError
render :text => "", :status => :gone
rescue OSM::APIPreconditionFailedError
render :text => "", :status => :precondition_failed
rescue OSM::APIError => ex
render ex.render_opts
rescue ActiveRecord::RecordNotFound
render :nothing => true, :status => :not_found
rescue
render :nothing => true, :status => :internal_server_error
end
end

View file

@ -40,6 +40,8 @@ class WayController < ApplicationController
else
render :text => "", :status => :gone
end
rescue OSM::APIError => ex
render ex.render_opts
rescue ActiveRecord::RecordNotFound
render :nothing => true, :status => :not_found
end
@ -56,11 +58,8 @@ class WayController < ApplicationController
else
render :nothing => true, :status => :bad_request
end
rescue OSM::APIPreconditionFailedError
render :text => "", :status => :precondition_failed
rescue OSM::APIVersionMismatchError => ex
render :text => "Version mismatch: Provided " + ex.provided.to_s +
", server had: " + ex.latest.to_s, :status => :bad_request
rescue OSM::APIError => ex
render ex.render_opts
rescue ActiveRecord::RecordNotFound
render :nothing => true, :status => :not_found
end
@ -74,10 +73,8 @@ class WayController < ApplicationController
# if we get here, all is fine, otherwise something will catch below.
render :nothing => true
rescue OSM::APIAlreadyDeletedError
render :text => "", :status => :gone
rescue OSM::APIPreconditionFailedError
render :text => "", :status => :precondition_failed
rescue OSM::APIError => ex
render ex.render_opts
rescue ActiveRecord::RecordNotFound
render :nothing => true, :status => :not_found
end

View file

@ -10,6 +10,7 @@ module OSM
# The base class for API Errors.
class APIError < RuntimeError
def render_opts { :text => "", :status => :internal_server_error } end
end
# Raised when an API object is not found.
@ -18,10 +19,12 @@ module OSM
# Raised when a precondition to an API action fails sanity check.
class APIPreconditionFailedError < APIError
def render_opts { :text => "", :status => :precondition_failed } end
end
# Raised when to delete an already-deleted object.
class APIAlreadyDeletedError < APIError
def render_opts { :text => "", :status => :gone } end
end
# Raised when the provided version is not equal to the latest in the db.
@ -31,6 +34,11 @@ module OSM
end
attr_reader :provided, :latest
def render_opts
{ :text => "Version mismatch: Provided " + ex.provided.to_s +
", server had: " + ex.latest.to_s, :status => :bad_request }
end
end
# Helper methods for going to/from mercator and lat/lng.