From 7151fa05e4d4f73956d7f9a7ad2db1f5b956a0c0 Mon Sep 17 00:00:00 2001 From: Gabriel Ebner Date: Tue, 13 May 2008 12:15:06 +0000 Subject: [PATCH] api06: simplify exception handling and add exception handling to the diff upload. --- app/controllers/changeset_controller.rb | 3 +++ app/controllers/node_controller.rb | 6 ++---- app/controllers/relation_controller.rb | 21 ++++----------------- app/controllers/way_controller.rb | 15 ++++++--------- lib/osm.rb | 8 ++++++++ 5 files changed, 23 insertions(+), 30 deletions(-) diff --git a/app/controllers/changeset_controller.rb b/app/controllers/changeset_controller.rb index db3a26295..322ce79f7 100644 --- a/app/controllers/changeset_controller.rb +++ b/app/controllers/changeset_controller.rb @@ -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 diff --git a/app/controllers/node_controller.rb b/app/controllers/node_controller.rb index f1ee8f66b..1e0deb140 100644 --- a/app/controllers/node_controller.rb +++ b/app/controllers/node_controller.rb @@ -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 diff --git a/app/controllers/relation_controller.rb b/app/controllers/relation_controller.rb index dabf6eaa8..c49ecd4d7 100644 --- a/app/controllers/relation_controller.rb +++ b/app/controllers/relation_controller.rb @@ -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 diff --git a/app/controllers/way_controller.rb b/app/controllers/way_controller.rb index 21f2ea765..cf1634fa5 100644 --- a/app/controllers/way_controller.rb +++ b/app/controllers/way_controller.rb @@ -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 diff --git a/lib/osm.rb b/lib/osm.rb index a8cb103d2..8798866e5 100644 --- a/lib/osm.rb +++ b/lib/osm.rb @@ -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. -- 2.43.2